multus实现容器多网卡
k8s v1.19.0
目的
multus支持不同容器网络插件实现容器多网卡,也支持相同容器网络插件实现容器多网卡。
实操
git clone https://github.com/intel/multus-cni.git && cd multus-cni
git checkout v4.0.0
# 新增crd network-attachment-definitions、rbac、configmap和daemonset
kubectl create -f ./deployments/multus-daemonset.yml
# 查看flannel cni version
cat /etc/cni/net.d/10-flannel.conflist | grep cniVersion
# 创建flannel NetworkAttachmentDefinition
cat <<EOF | kubectl create -f -
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
name: flannel-conf0
spec:
config: '{
"cniVersion": "0.3.1",
"type": "flannel"
}'
EOF
# 部署sts
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
ports:
- port: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx
spec:
podManagementPolicy: Parallel
serviceName: nginx
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
annotations:
k8s.v1.cni.cncf.io/networks: flannel-conf0
spec:
containers:
- name: nginx
image: nginx:1.9.1
在原来基础上增加一张网卡,总共2张网卡。
基本原理
eth0是容器中默认路由所在的网卡。
kubelet
pkg/kubelet/dockershim/network/cni/cni.go
syncNetworkConfig方法
kubelet读取/etc/cni/net.d/00-multus.conf作为默认cniNetwork。
kubelet
pkg/kubelet/dockershim/network/cni/cni.go
addToNetwork方法
multus-cni
pkg/multus/multus.go
CmdAdd函数
multus收到kubelet的cni请求,根据pod定义注解拆分成多个cni请求,逐个同步发送给对应的cni插件,失败时回退之前成功的,kubelet还是只发一次cni add,除了前面的cni-loopback cni add请求。