Calico 在多网卡kubernetes节点上有的时候会选择错误的网卡,导致容器间通信异常。
因此配置Calico选择正确的网卡十分重要。这个配置项困扰我达一年之久,之前的做法一直是使用ifdown关停除目标网卡外的其他网卡。因为Calico的默认行为是选择第一个可用的网卡。

网卡自动检测规则方法,有如下4种:

Kubernetes Node IP

Calico 会默认选择node Status.Address第一个内部ip.

如何查看 Status.Addresss 字段?

[root@k8s-master ~]# kubectl get node k8s-master -o yaml
apiVersion: v1
kind: Node
status:
addresses:
- address: 172.16.15.12
type: InternalIP
- address: k8s-master
type: Hostname

配置时机是两种:

yaml 方式
第一种方法是在部署Calico DeamonSet时,设置其Pod环境变量:

# Source: calico/templates/calico-node.yaml
# This manifest installs the calico-node container, as well
# as the CNI plugins and network config on
# each master and worker node in a Kubernetes cluster.
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: calico-node
namespace: kube-system
labels:
k8s-app: calico-node
spec:
selector:
matchLabels:
k8s-app: calico-node
spec:
containers:
# Runs calico-node container on each Kubernetes node. This
# container programs network policy and routes on each
# host.
- name: calico-node
image: registry.geoway.com/calico/node:v3.19.1
env:
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
- name: IP_AUTODETECTION_METHOD
value: "cidr=172.16.15.0/24"

然后执行:

kubectl apply -f calico.yaml
bash patch

另一种方式是apply之后,使用patch命令手动添加:

kubectl set env daemonset/calico-node -n kube-system IP_AUTODETECTION_METHOD=kubernetes-internal-ip

1、目标IP或域名可达

选择可以ping通目标IP或者域名的网卡:
yaml 方式

        - name: calico-node
image: registry.geoway.com/calico/node:v3.19.1
env:
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
- name: IP_AUTODETECTION_METHOD
value: "can-reach=www.google.com"

bash patch

kubectl set env daemonset/calico-node -n kube-system IP_AUTODETECTION_METHOD=can-reach=www.google.com

2、匹配目标网卡

使用正则表达式匹配ip a命令输出的网卡名,可使用正则

- name: calico-node
image: registry.geoway.com/calico/node:v3.19.1
env:
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
- name: IP_AUTODETECTION_METHOD
value: "interface=eth.*"

3、排除匹配网卡

和匹配目标网卡相反,不会选择符合匹配规则的网卡

- name: calico-node
image: registry.geoway.com/calico/node:v3.19.1
env:
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
- name: IP_AUTODETECTION_METHOD
value: "skip-interface=eth.*"

4、CIDR

指定CIDR地址,多个子网使用逗号","分隔。

- name: calico-node
image: registry.geoway.com/calico/node:v3.19.1
env:
# Auto-detect the BGP IP address.
- name: IP
value: "autodetect"
- name: IP_AUTODETECTION_METHOD
value: "cidr=192.168.200.0/24,172.15.0.0/24"

总结

Calico 配置网卡可使用IP_AUTODETECTION_METHOD环境变量, 有两种方式。
Calico 相关配置的官方文档在Change the autodetection method

作者:oneslide