安装环境
cat <<EOF | kind create cluster --name test --config -
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
kubeProxyMode: iptables
disableDefaultCNI: false
ipFamily: ipv4
apiServerAddress: 127.0.0.1
apiServerPort: 0
podSubnet: "10.16.0.0/16"
serviceSubnet: "10.96.0.0/12"
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
metadata:
name: config
etcd:
local:
dataDir: "/root/etcd"
nodes:
- role: control-plane
image: kindest/node:v1.26.0
EOF
git clone https://github.com/intel/multus-cni.git && cd multus-cni
git checkout v4.0.0
kubectl create -f ./deployments/multus-daemonset.yml
cat <<EOF | kubectl create -f -
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
name: kindnet
spec:
config: '{"cniVersion":"0.3.1","name":"kindnet","type":"ptp","ipMasq":false,"ipam":{"type":"host-local","dataDir":"/run/cni-ipam-state","routes":[{"dst":"0.0.0.0/0"}],"ranges":[[{"subnet":"10.16.1.0/24"}]]}}'
EOF
cat <<EOF | kubectl create -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deploy
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx-deploy
template:
metadata:
annotations:
k8s.v1.cni.cncf.io/networks: '[{"name":"kindnet","interface":"eth100"}]'
labels:
app: nginx-deploy
spec:
containers:
- name: nginx-deploy
image: nginx:1.27.0
EOF
确认容器内存在eth100网卡。
源码分析
multus
pkg/multus/multus.go
CmdAdd函数
处理cni add请求
pkg/multus/multus.go
getIfname函数
获取cni网卡名称
pkg/k8sclient/k8sclient.go
parsePodNetworkAnnotation函数
delegate.IfnameRequest来自pod注解解析json。
pkg/types/types.go
结构体NetworkSelectionElement