Kubernetes集群使用calico多网卡问题

生产环境下,通常会将业务网和存储网进行分离,因此服务器通常会有多块网卡,在裸金属服务器初始化Kubernetes环境后,在安装网卡插件时需要指定业务网网卡名称,才能使集群应用正确的网络地址。

本文介绍使用Calico插件作为cni实现,在多网卡环境下,指定集群使用的网络。

一、系统环境

系统安装了多块网卡并配置了多个IP,管理网、业务网和存储网分离。Kubernetes已经完成了初始化。相关工作节点已加入集群。

名称版本
Kubernetes 版本1.22.15
网络插件类型/版本calico/3.24
操作系统类型/版本centos/7.9

在这里插入图片描述

其中 172.16.16.0/23 是存储网络,172.16.10.0/24 是业务网,172.16.4.0/24 是管理网。其中,kubernetes使用业务网搭建。

二、安装过程遇到的问题

现象

使用官方默认配置脚本部署完Calico网络插件后,部分节点calico-node pod无法通过健康检查,现象如下:
在这里插入图片描述

分析

在管理节点上执行命令观察pod日志:

kubectl logs -f calico-node-f458s -n kube-syste

在这里插入图片描述

通过日志可以看到,calico自动检测到IPv4的地址在接口bond3网卡: 172.16.16.26/23 上, 而不是我们期望的业务网络地址。

三、解决方法

通过一dun乱搜索,在这里找到了解决方法, kubernetes集群节点多网卡 , 于是开始尝试。

指定calico使用的网卡名称

calico-node 是 DaemonSet 管理的 Pod,默认部署在 kube-system 命名空间, 修改环境变量配置,指定网卡名称:

# 编辑calico-node ds
kubectl edit ds calico-node -n kube-system

# 新增环境变量(指定使用网卡interface=bond4)
- name: IP_AUTODETECTION_METHOD
  value: interface=bond4

在这里插入图片描述

观察日志

修改配置后,pod会重新初始化,观察pod日志:
在这里插入图片描述
IP地址已经回到172.16.10.0/24 网络。节点状态恢复正常。

四、扩展

关于 IP_AUTODETECTION_METHOD 配置

翻阅官方文档: 配置默认的IP池; 可以看到:

在这里插入图片描述
默认的IP自动检测方法是 first-found, 从字面意思看就是 首先找到的,那这有很强的随机性,也就能解释之前的现象,部分节点calico-node pod是正常的,部分异常。

同时,官方文档还指导了其它几种配置方法,理解和配置起来比较简单。

kubernetes-internal-ip (内网IP)

The kubernetes-internal-ip method will select the first internal IP address listed in the Kubernetes node’s Status.Addresses field

Example:

IP_AUTODETECTION_METHOD=kubernetes-internal-ip
IP6_AUTODETECTION_METHOD=kubernetes-internal-ip

can-reach=DESTINATION (网络可达)

The can-reach method uses your local routing to determine which IP address will be used to reach the supplied destination. Both IP addresses and domain names may be used.

Example using IP addresses:

IP_AUTODETECTION_METHOD=can-reach=8.8.8.8
IP6_AUTODETECTION_METHOD=can-reach=2001:4860:4860::8888

Example using domain names:

IP_AUTODETECTION_METHOD=can-reach=www.google.com
IP6_AUTODETECTION_METHOD=can-reach=www.google.com

interface=INTERFACE-REGEX (网卡名称正则表达式匹配)

The interface method uses the supplied interface regular expression to enumerate matching interfaces and to return the first IP address on the first matching interface. The order that both the interfaces and the IP addresses are listed is system dependent.

Example with valid IP address on interface eth0, eth1, eth2 etc.:

IP_AUTODETECTION_METHOD=interface=eth.*
IP6_AUTODETECTION_METHOD=interface=eth.*

skip-interface=INTERFACE-REGEX (排除某些网卡)

The skip-interface method uses the supplied interface regular expression to exclude interfaces and to return the first IP address on the first interface that does not match. The order that both the interfaces and the IP addresses are listed is system dependent.

Example with valid IP address on interface exclude enp6s0f0, eth0, eth1, eth2 etc.:

IP_AUTODETECTION_METHOD=skip-interface=enp6s0f0,eth.*
IP6_AUTODETECTION_METHOD=skip-interface=enp6s0f0,eth.*

cidr=CIDR (指定网络地址)

The cidr method will select any IP address from the node that falls within the given CIDRs. For example:

Example:

IP_AUTODETECTION_METHOD=cidr=10.0.1.0/24,10.0.2.0/24
IP6_AUTODETECTION_METHOD=cidr=2001:4860::0/64

参考:

  1. 官方calico配置文档 (打开后看manifest)
  2. kubernetes集群节点多网卡,calico指定网卡
当你在Kubernetes集群中尝试通过命令`ping baidu.com`却收到"Name or service not known"这样的错误时,这通常意味着你的Pod无法访问外部网络,可能是由于以下几个原因: 1. **DNS配置**:确认你的Pod是否配置了正确的DNS服务器地址,如果没有,你可以通过修改`pod.yaml`文件,在`spec.containers`部分添加`dnsPolicy`字段,并设置为如`ClusterFirstWithHostNet`。 ```yaml apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: ... dnsPolicy: ClusterFirstWithHostNet ``` 2. **服务发现**:检查你的应用是否依赖于Service来进行端口映射。如果Service没有正确地将外部域名指向Pod,`ping`请求将失败。确保Service已正确创建并且暴露了外部IP。 3. **安全组规则**:如果是运行在有安全组控制的环境(如AWS Kops、GKE),检查Pod的安全组规则是否允许出站访问互联网。 4. **网络插件**:某些网络插件(如Calico、Flannel等)可能需要额外配置才能让Pod访问外部资源。查阅网络插件文档进行调整。 5. **Node网络问题**:确认节点是否能够访问外部网络,因为Pod通常是挂载到某个节点的网络环境中。 为了解决这个问题,你需要查看Pod的日志、检查相关配置文件以及Kubernetes的网络配置,以找出导致访问限制的具体原因。一旦找到问题,相应地调整配置即可。如果你需要更具体的帮助,请提供更多的上下文信息,比如你的集群配置和操作步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值