Multus-CNI与whereabouts的简单运用 电脑版发表于:2022/12/22 16:00  ># Multus-CNI [TOC] ## Multus-CNI 简介 tn2>简单来讲,这玩意可以对一个pod插入多张网卡进行通信。同时也支持多种cni的插件,什么Flannle、calico、macvlan都是可以的。  tn2>它的工作流程如下图所示:  tn2>这样看不清晰流程可以看创建的pod信息。  tn2>执行如下命令进行安装。 ```bash git clone https://github.com/k8snetworkplumbingwg/multus-cni.git cd multus-cni cat ./deployments/multus-daemonset.yml | kubectl apply -f - ``` tn2>安装完成后,会多一个名为`NetworkAttachmentDefinition`资源来定义cni的配置。 ## whereabouts 简介  tn2>在以前的博客中,使用`local-host`需要为每台主机分配不同的ip地址范围才能实现,而whereabouts会自动进行分配ip地址且不会发生两台主机同一个ip的情况。 可通过如下命令进行安装: ```bash git clone https://github.com/k8snetworkplumbingwg/whereabouts && cd whereabouts kubectl apply \ -f doc/crds/daemonset-install.yaml \ -f doc/crds/whereabouts.cni.cncf.io_ippools.yaml \ -f doc/crds/whereabouts.cni.cncf.io_overlappingrangeipreservations.yaml ``` ## ipvlan 实践 tn2>首先我们定义ipvlan的配置文件。 ```bash vim networkad.yaml ``` ```yaml apiVersion: "k8s.cni.cncf.io/v1" kind: NetworkAttachmentDefinition metadata: name: whereabouts-ipvlan-conf-1 spec: config: '{ "cniVersion": "0.3.0", "name": "ipvlan-conf-1", "type": "ipvlan", "master": "eth0", "mode": "l2", "ipam": { "type": "whereabouts", "range": "10.211.55.0/24", "range_start": "10.211.55.129", "range_end": "10.211.55.159", "gateway": "10.211.55.1" } }' ``` tn2>然后apply一下。 ```bash kubectl apply -f networkad.yaml ``` tn2>创建一个指定使用`whereabouts-ipvlan-conf-1`配置的pod,并将该网卡设置为`eth1`。 ```bash vim ipvlan.yaml ``` ```yaml apiVersion: v1 kind: Pod metadata: name: pod0-case-02 annotations: k8s.v1.cni.cncf.io/networks: whereabouts-ipvlan-conf-1@eth1 spec: containers: - name: pod0-case-02 image: burlyluo/nettoolbox command: - /sbin/init ``` ```bash kubectl apply -f ipvlan.yaml kubectl get pod kubectl exec pod/pod0-case-02 -- ifconfig ```  tn2>外网仍然可以访问。  ## macvlan 实践 tn2>首先定义macvlan配置文件。 ```bash vim networkwhereabouts.yaml ``` ```yaml apiVersion: "k8s.cni.cncf.io/v1" kind: NetworkAttachmentDefinition metadata: name: whereabouts-conf spec: config: '{ "cniVersion": "0.3.0", "name": "whereaboutsexample", "type": "macvlan", "master": "eth0", "mode": "bridge", "ipam": { "type": "whereabouts", "range": "192.168.2.225/28" } }' ``` ```bash kubectl apply -f networkwhereabouts.yaml kubectl get net-attach-def kubectl get net-attach-def whereabouts-conf ``` tn2>创建一个指定使用`whereabouts-conf`配置的pod,并将该网卡设置为`eth3`。 ```yaml cat <<EOF | kubectl create -f - apiVersion: v1 kind: Pod metadata: name: pod-macvlan annotations: k8s.v1.cni.cncf.io/networks: whereabouts-conf@eth3 spec: containers: - name: pod image: burlyluo/nettoolbox EOF ``` ```bash kubectl get pods -o wide kubectl exec pod/pod-macvlan -- ifconfig ``` 