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

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

一、系统环境

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

名称

版本

Kubernetes 版本

1.22.15

网络插件类型/版本

calico/3.24

操作系统类型/版本

centos/7.9

使用k8s部署zookeeper集群 k8s部署calico_kubernetes

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

二、安装过程遇到的问题

现象

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

使用k8s部署zookeeper集群 k8s部署calico_ico_02

分析

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

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

使用k8s部署zookeeper集群 k8s部署calico_使用k8s部署zookeeper集群_03

通过日志可以看到,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

使用k8s部署zookeeper集群 k8s部署calico_运维_04

观察日志

修改配置后,pod会重新初始化,观察pod日志:

使用k8s部署zookeeper集群 k8s部署calico_IP_05


IP地址已经回到172.16.10.0/24 网络。节点状态恢复正常。

四、扩展

关于 IP_AUTODETECTION_METHOD 配置

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

使用k8s部署zookeeper集群 k8s部署calico_IP_06


默认的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指定网卡