- MACVLAN
- CNI WITH MACVLAN
- MULTUS WITH MACVLAN
- Multus CNI usage guide
- Setup CRDs (daemonset automatically does)
- Create network attachment definition
- Run pod with network annotation
- Launch pod with text annotation
- Launch pod with text annotation for NetworkAttachmentDefinition in different namespace
- Launch pod with text annotation with interface name
- Launch pod with json annotation
- Launch pod with json annotation for NetworkAttachmentDefinition in different namespace
- Launch pod with json annotation with interface
- Verifying pod network
- Specifying a default route for a specific attachment
- Entrypoint Parameters
# Kubernetes CNI MACVLAN# 时间:20210822-[14:00-17:00]# 主题:MACVLAN# 文档:https://www.yuque.com/docs/share/5d02c43b-82ff-430f-859c-ce46fc22020d?# 《20210822-MACVLAN》# 作业:MACVLAN Bridge Mode
# FAQ:
MACVLAN
$ modprobe macvlan$ lsmod | grep macvlanmacvlan 19046 0macvlan 允许你在主机的一个网络接口上配置多个虚拟的网络接口,这些网络 interface 有自己独立的 mac 地址,也可以配置上 ip 地址进行通信。macvlan 下的虚拟机或者容器网络和主机在同一个网段中,共享同一个广播域。macvlan 和 bridge 比较相似,但因为它省去了 bridge 的存在,所以配置和调试起来比较简单,而且效率也相对高。除此之外,macvlan 自身也完美支持 VLAN。Macvlan allows you to configure sub-interfaces (also termed slave devices) of a parent, physical Ethernet interface (also termed upper device), each with its own unique MAC address, and consequently its own IP address. Applications, VMs and containers can then bind to a specific sub-interface to connect directly to the physical network, using their own MAC and IP address.# Macvlan is a near-ideal solution to natively connect VMs and containers to a physical network, but it has its shortcomings:- The switch the host is connected to may have a policy that limits the number of different MAC addresses on a physical port. Although you should really work with your network administrator to change the policy, there are times when this might not be possible (or you just need to set up a quick PoC).- Many NICs have a limit on the number of MAC addresses they support in hardware. Exceeding the limit may affect the performance.- IEEE 802.11 doesn’t like multiple MAC addresses on a single client. It is likely macvlan sub-interfaces will be blocked by your wireless interface driver, AP or both. There are somehow complex ways around that limitation, but why not stick to a simple solution?Macvlan, MACVLAN or MAC-VLAN allows you to configure multiple Layer 2 (i.e. Ethernet MAC) addresses on a single physical interface. Macvlan allows you to configure sub-interfaces (also termed slave devices) of a parent, physical Ethernet interface (also termed upper device), each with its own unique (randomly generated) MAC address, and consequently its own IP address. Applications, VMs and containers can then bind to a specific sub-interface to connect directly to the physical network, using their own MAC and IP address.Mavlan sub-interfaces are not able to directly communicate with the parent interface, i.e. VMs cannot directly communicate with the host. If you require VM-host communication, you should add another macvlan sub-interface and assign it to the host.Macvlan sub-interfaces use a mac0@eth0 notation, to clearly identify the sub-interface and it’s parent interface. Sub-interface state is bound to its parent’s state – if eth0 is down, so is the mac0@eth0.

# MACVLAN Bridge Mode:# 0.prepare env:modprobe macvlanifconfig ens33 promisc# 1. Add nsip netns add net1ip netns add net2# 2. add macvlanip link add link ens33 name macv1 type macvlan mode bridgeip link add link ens33 name macv2 type macvlan mode bridge# 3.add interface to nsip link set macv1 netns net1ip link set macv2 netns net2# config ip addressip netns exec net1 ifconfig macv1 172.12.2.5/24 upip netns exec net2 ifconfig macv2 172.12.2.7/24 up# ping:ip netns exec ns1 ping 172.12.2.716:21:55.463344 1c:69:7a:45:1e:5a > 01:00:5e:7f:ff:fa, ethertype IPv4 (0x0800), length 179: 192.168.2.11.57357 > 239.255.255.250.ssdp: UDP, length 13716:21:55.931734 ee:eb:f3:05:0e:21 > a2:4d:6d:0b:27:79, ethertype IPv4 (0x0800), length 98: 172.12.2.5 > 172.12.2.7: ICMP echo request, id 18386, seq 1, length 6416:21:55.931773 a2:4d:6d:0b:27:79 > ee:eb:f3:05:0e:21, ethertype IPv4 (0x0800), length 98: 172.12.2.7 > 172.12.2.5: ICMP echo reply, id 18386, seq 1, length 6416:21:56.932077 ee:eb:f3:05:0e:21 > a2:4d:6d:0b:27:79, ethertype IPv4 (0x0800), length 98: 172.12.2.5 > 172.12.2.7: ICMP echo request, id 18386, seq 2, length 6416:21:56.932119 a2:4d:6d:0b:27:79 > ee:eb:f3:05:0e:21, ethertype IPv4 (0x0800), length 98: 172.12.2.7 > 172.12.2.5: ICMP echo reply, id 18386, seq 2, length 64
CNI WITH MACVLAN
# 作为默认CNI使用:# CNI Config File:cat net.d/10-macvlan.conf{"name": "macvlannet","type": "macvlan","master": "ens33","ipam": {"type": "host-local", # 推荐使用whereabouts"subnet": "10.52.101.0/24","rangeStart": "10.52.101.40","rangeEnd": "10.52.101.250","gateway": "10.52.101.254","routes": [{ "dst": "0.0.0.0/0" }]}}# bin File:[root@k8s-1 bin]# pwd/opt/cni/bin[root@k8s-1 bin]# lltotal 167524-rwxr-xr-x 1 root root 4159518 Aug 13 18:28 bandwidth-rwxr-xr-x. 1 root root 3581192 Sep 10 2020 bridge-rwxr-xr-x 1 root root 36564992 Aug 13 18:28 calico-rwxr-xr-x 1 root root 36564992 Aug 13 18:28 calico-ipam-rwxr-xr-x. 1 root root 9837552 Sep 10 2020 dhcp-rwxr-xr-x. 1 root root 4699824 Sep 10 2020 firewall-rwxr-xr-x 1 root root 3069556 Aug 13 18:28 flannel-rwxr-xr-x. 1 root root 3274160 Sep 10 2020 host-device-rwxr-xr-x 1 root root 3614480 Aug 13 18:28 host-local-rwxr-xr-x 1 root root 36564992 Aug 13 18:28 install-rwxr-xr-x. 1 root root 3377272 Sep 10 2020 ipvlan-rwxr-xr-x 1 root root 3209463 Aug 13 18:28 loopback-rwxr-xr-x. 1 root root 3440168 Sep 10 2020 macvlan # this one-rwxr-xr-x 1 root root 3939867 Aug 13 18:28 portmap-rwxr-xr-x. 1 root root 3528800 Sep 10 2020 ptp-rwxr-xr-x. 1 root root 2849328 Sep 10 2020 sbr-rwxr-xr-x. 1 root root 2503512 Sep 10 2020 static-rwxr-xr-x 1 root root 3356587 Aug 13 18:28 tuning-rwxr-xr-x. 1 root root 3377120 Sep 10 2020 vlan[root@k8s-1 bin]## Pod resource template:# cat cni_demo_template.yaml[root@k8s-1 ~]# cat cni_demo_template.yamlapiVersion: apps/v1kind: DaemonSetmetadata:labels:app: cniname: cnispec:selector:matchLabels:app: cnitemplate:metadata:annotations:cni: "macvlannet"labels:app: cnispec:containers:- image: burlyluo/nettoolboxname: nettoolbox[root@k8s-1 ~]## Pod list:[root@k8s-1 cni]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATEScni-4n7cm 1/1 Running 0 136d 10.52.101.43 k8s-2 <none> <none>cni-gs49t 1/1 Running 0 136d 10.52.101.40 k8s-1 <none> <none>[root@k8s-1 cni]#[root@k8s-1 ~]# kubectl exec -it cni-b8njx -- ping 10.52.101.44PING 10.52.101.44 (10.52.101.44): 56 data bytes64 bytes from 10.52.101.44: seq=0 ttl=64 time=0.690 ms64 bytes from 10.52.101.44: seq=1 ttl=64 time=0.450 ms^C--- 10.52.101.44 ping statistics ---2 packets transmitted, 2 packets received, 0% packet lossround-trip min/avg/max = 0.450/0.570/0.690 ms[root@k8s-1 ~]#
MULTUS WITH MACVLAN
# 使用MULTUS加载MACVLAN模式(推荐):# Enable MACVLAN# MacVLAN type definitionCreate Network Attach-Definition macvlan typeapiVersion: "k8s.cni.cncf.io/v1"kind: NetworkAttachmentDefinitionmetadata:name: macvlan-conf-1spec:config: '{"cniVersion": "0.3.0","name": "macvlan-conf-1","type": "macvlan","master": "eth1","mode": "bridge","ipam": {"type": "whereabouts","range": "192.168.60.0/24","range_start": "192.168.60.20","range_end": "192.168.60.50","gateway": "192.168.60.1"}}'The master key value is the reference to the *_second nic *_in our worker nodes.$ kubectl apply -f networkattachdefinition.yamlCopyNetwork Attach Definition Validation macvlan typeLet’s validate our work by listing and describing our new Network Attach Definition$ $ kubectl get net-attach-defNAME AGEmacvlan-conf-1 134m# kubectl describe net-attach-def macvlan-conf-1Name: macvlan-conf-1Namespace: defaultLabels: <none>Annotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"k8s.cni.cncf.io/v1","kind":"NetworkAttachmentDefinition","metadata":{"annotations":{},"name":"macvlan-conf-1","namespace":"...API Version: k8s.cni.cncf.io/v1Kind: NetworkAttachmentDefinitionMetadata:Creation Timestamp: 2020-09-18T13:38:22ZGeneration: 2Resource Version: 6958535Self Link: /apis/k8s.cni.cncf.io/v1/namespaces/default/network-attachment-definitions/macvlan-conf-1UID: 99a902e1-8fd1-4f37-81d7-229361029455Spec:Config: { "cniVersion": "0.3.0", "type": "macvlan", "master": "eth1", "mode": "bridge", "ipam": { "type": "whereabouts", "range": "192.168.60.0/24", "range_start": "192.168.60.20", "range_end": "192.168.60.50", "gateway": "192.168.60.1", "routes": [{"dst": "0.0.0.0/0"}], "gateway": "192.168.60.1" } }Events: <none>Pod Definitions with 2 interfacesIn order for pods to be created with an additional nic, and the same principle applies for adding more than two NICs to the pod, the pod definition should be called with network annotations making reference to the Network Attach Definition(s) please use the following pods definitions to create a testbed.Create Pods with mcvlan interfacesapiVersion: v1kind: Podmetadata:name: pod0-case-01annotations:k8s.v1.cni.cncf.io/networks: macvlan-conf-1spec:containers:- name: pod0-case-01image: docker.io/centos/tools:latestcommand:- /sbin/initapiVersion: v1kind: Podmetadata:name: pod1-case-01annotations:k8s.v1.cni.cncf.io/networks: macvlan-conf-1spec:containers:- name: pod1-case-01image: docker.io/centos/tools:latestcommand:- /sbin/initDeploy the new pods$ kubectl apply -f pod1-case1.yaml$ kubectl apply -f pod0-case1.yamlCopyValidate Pods Creation with macvlan interfacesLet’s validate your work by confirming that the pods got created with an additional interface by doing the following commands:$ kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESpod0-case-01 1/1 Running 0 142m 10.135.41.90 192.168.50.16 <none> <none>pod1-case-01 1/1 Running 0 140m 10.135.41.91 192.168.50.16 <none> <none>$ kubectl exec -it pod0-case-01 -- ip -d address1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever3: eth0@if585: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1472 qdisc noqueue state UP group defaultlink/ether 16:66:b2:53:e4:f8 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0veth numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 10.135.41.90/24 brd 10.135.41.255 scope global eth0valid_lft forever preferred_lft forever4: net1@if401: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group defaultlink/ether 02:5b:b8:96:0a:d7 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0macvlan mode bridge numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 192.168.60.20/24 brd 192.168.60.255 scope global net1valid_lft forever preferred_lft forever$ kubectl exec -it pod1-case-01 -- ip -d address1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever3: eth0@if586: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1472 qdisc noqueue state UP group defaultlink/ether fa:61:88:fb:53:da brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0veth numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 10.135.41.91/24 brd 10.135.41.255 scope global eth0valid_lft forever preferred_lft forever4: net1@if401: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group defaultlink/ether ca:c3:73:0e:f3:e5 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0macvlan mode bridge numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 192.168.60.21/24 brd 192.168.60.255 scope global net1valid_lft forever preferred_lft foreverConnectivity TestingEast-West macvlan traffic$ kubectl exec -it pod0-case-01 -- ping -c 5 192.168.60.21PING 192.168.60.21 (192.168.60.21) 56(84) bytes of data.64 bytes from 192.168.60.21: icmp_seq=1 ttl=64 time=0.289 ms64 bytes from 192.168.60.21: icmp_seq=2 ttl=64 time=0.061 ms64 bytes from 192.168.60.21: icmp_seq=3 ttl=64 time=0.059 ms64 bytes from 192.168.60.21: icmp_seq=4 ttl=64 time=0.062 ms64 bytes from 192.168.60.21: icmp_seq=5 ttl=64 time=0.058 ms--- 192.168.60.21 ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 4001msrtt min/avg/max/mdev = 0.058/0.105/0.289/0.092 ms$ kubectl exec -it pod1-case-01 -- ping -c 5 192.168.60.20PING 192.168.60.20 (192.168.60.20) 56(84) bytes of data.64 bytes from 192.168.60.20: icmp_seq=1 ttl=64 time=0.253 ms64 bytes from 192.168.60.20: icmp_seq=2 ttl=64 time=0.044 ms64 bytes from 192.168.60.20: icmp_seq=3 ttl=64 time=0.050 ms64 bytes from 192.168.60.20: icmp_seq=4 ttl=64 time=0.047 ms64 bytes from 192.168.60.20: icmp_seq=5 ttl=64 time=0.069 ms--- 192.168.60.20 ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 4000msrtt min/avg/max/mdev = 0.044/0.092/0.253/0.081 msNorth-South macvlan traffic:$ kubectl exec -it pod0-case-01 -- ping -c 5 192.168.60.1PING 192.168.60.1 (192.168.60.1) 56(84) bytes of data.64 bytes from 192.168.60.1: icmp_seq=1 ttl=64 time=0.925 ms64 bytes from 192.168.60.1: icmp_seq=2 ttl=64 time=0.536 ms64 bytes from 192.168.60.1: icmp_seq=3 ttl=64 time=0.483 ms64 bytes from 192.168.60.1: icmp_seq=4 ttl=64 time=0.640 ms64 bytes from 192.168.60.1: icmp_seq=5 ttl=64 time=0.488 ms$ kubectl exec -it pod1-case-01 -- ping -c 5 192.168.60.1PING 192.168.60.1 (192.168.60.1) 56(84) bytes of data.64 bytes from 192.168.60.1: icmp_seq=1 ttl=64 time=1.31 ms64 bytes from 192.168.60.1: icmp_seq=2 ttl=64 time=0.567 ms64 bytes from 192.168.60.1: icmp_seq=3 ttl=64 time=0.473 ms64 bytes from 192.168.60.1: icmp_seq=4 ttl=64 time=0.605 ms64 bytes from 192.168.60.1: icmp_seq=5 ttl=64 time=0.647 ms--- 192.168.60.1 ping statistics ---5 packets transmitted, 5 received, 0% packet loss, time 4002msrtt min/avg/max/mdev = 0.473/0.721/1.313/0.301 ms
Multus CNI usage guide
Prerequisites
- Kubelet configured to use CNI
- Kubernetes version with CRD support (generally )
Your Kubelet(s) must be configured to run with the CNI network plugin. Please see Kubernetes document for CNI for more details.
Install Multus
Generally we recommend two options: Manually place a Multus binary in your /opt/cni/bin, or use our quick-start method — which creates a daemonset that has an opinionated way of how to install & configure Multus CNI (recommended).
Copy Multus Binary into place
You may acquire the Multus binary via compilation (see the developer guide) or download the a binary from the GitHub releases page. Copy multus binary into CNI binary directory, usually /opt/cni/bin. Perform this on all nodes in your cluster (master and nodes).
$ cp multus /opt/cni/bin
Via Daemonset method
As a quickstart, you may apply these YAML files (included in the clone of this repository). Run this command (typically you would run this on the master, or wherever you have access to the kubectl command to manage your cluster).
$ cat ./images/{multus-daemonset.yml,flannel-daemonset.yml} | kubectl apply -f -
If you need more comprehensive detail, continue along with this guide, otherwise, you may wish to either follow the quickstart guide or skip to the ‘Create network attachment definition’ section.
Set up conf file in /etc/cni/net.d/ (Installed automatically by Daemonset)
If you use daemonset to install multus, skip this section and go to “Create network attachment”
You put CNI config file in /etc/cni/net.d. Kubernetes CNI runtime uses the alphabetically first file in the directory. ("NOTE1", "NOTE2" are just comments, you can remove them at your configuration)
Execute following commands at all Kubernetes nodes (i.e. master and minions)
$ mkdir -p /etc/cni/net.d$ cat >/etc/cni/net.d/00-multus.conf <<EOF{"name": "multus-cni-network","type": "multus","readinessindicatorfile": "/var/run/flannel/subnet.env","delegates": [{"NOTE1": "This is example, wrote your CNI config in delegates","NOTE2": "If you use flannel, you also need to run flannel daemonset before!","type": "flannel","name": "flannel.1","delegate": {"isDefaultGateway": true}}],"kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig"}EOF
For the detail, please take a look into Configuration Reference
NOTE: You can use “clusterNetwork”/“defaultNetworks” instead of “delegates”, see for the detail
As above config, you need to set "kubeconfig" in the config file for NetworkAttachmentDefinition(CRD).
Which network will be used for “Pod IP”?
In case of “delegates”, the first delegates network will be used for “Pod IP”. Otherwise, “clusterNetwork” will be used for “Pod IP”.
Create ServiceAccount, ClusterRole and its binding
Create resources for multus to access CRD objects as following command:
# Execute following commands at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: v1kind: ServiceAccountmetadata:name: multusnamespace: kube-system---kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1beta1metadata:name: multusrules:- apiGroups: ["k8s.cni.cncf.io"]resources:- '*'verbs:- '*'- apiGroups:- ""resources:- pods- pods/statusverbs:- get- update---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1beta1metadata:name: multusroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: multussubjects:- kind: ServiceAccountname: multusnamespace: kube-systemEOF
Set up kubeconfig file
Create kubeconfig at master node as following commands:
# Execute following command at Kubernetes master$ mkdir -p /etc/cni/net.d/multus.d$ SERVICEACCOUNT_CA=$(kubectl get secrets -n=kube-system -o json | jq -r '.items[]|select(.metadata.annotations."kubernetes.io/service-account.name"=="multus")| .data."ca.crt"')$ SERVICEACCOUNT_TOKEN=$(kubectl get secrets -n=kube-system -o json | jq -r '.items[]|select(.metadata.annotations."kubernetes.io/service-account.name"=="multus")| .data.token' | base64 -d )$ KUBERNETES_SERVICE_PROTO=$(kubectl get all -o json | jq -r .items[0].spec.ports[0].name)$ KUBERNETES_SERVICE_HOST=$(kubectl get all -o json | jq -r .items[0].spec.clusterIP)$ KUBERNETES_SERVICE_PORT=$(kubectl get all -o json | jq -r .items[0].spec.ports[0].port)$ cat > /etc/cni/net.d/multus.d/multus.kubeconfig <<EOF# Kubeconfig file for Multus CNI plugin.apiVersion: v1kind: Configclusters:- name: localcluster:server: ${KUBERNETES_SERVICE_PROTOCOL:-https}://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}certificate-authority-data: ${SERVICEACCOUNT_CA}users:- name: multususer:token: "${SERVICEACCOUNT_TOKEN}"contexts:- name: multus-contextcontext:cluster: localuser: multuscurrent-context: multus-contextEOF
Copy /etc/cni/net.d/multus.d/multus.kubeconfig into other Kubernetes nodes
NOTE: Recommend to exec ‘chmod 600 /etc/cni/net.d/multus.d/multus.kubeconfig’ to keep secure
$ scp /etc/cni/net.d/multus.d/multus.kubeconfig ...
Setup CRDs (daemonset automatically does)
If you use daemonset to install multus, skip this section and go to “Create network attachment”
Create CRD definition in Kubernetes as following command at master node:
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: apiextensions.k8s.io/v1beta1kind: CustomResourceDefinitionmetadata:name: network-attachment-definitions.k8s.cni.cncf.iospec:group: k8s.cni.cncf.ioversion: v1scope: Namespacednames:plural: network-attachment-definitionssingular: network-attachment-definitionkind: NetworkAttachmentDefinitionshortNames:- net-attach-defvalidation:openAPIV3Schema:properties:spec:properties:config:type: stringEOF
Create network attachment definition
The ‘NetworkAttachmentDefinition’ is used to setup the network attachment, i.e. secondary interface for the pod, There are two ways to configure the ‘NetworkAttachmentDefinition’ as following:
- NetworkAttachmentDefinition with json CNI config
- NetworkAttachmentDefinition with CNI config file
NetworkAttachmentDefinition with json CNI config:
Following command creates NetworkAttachmentDefinition. CNI config is inconfig:field.# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: "k8s.cni.cncf.io/v1"kind: NetworkAttachmentDefinitionmetadata:name: macvlan-conf-1spec:config: '{"cniVersion": "0.3.0","type": "macvlan","master": "eth1","mode": "bridge","ipam": {"type": "host-local","ranges": [[ {"subnet": "10.10.0.0/16","rangeStart": "10.10.1.20","rangeEnd": "10.10.3.50","gateway": "10.10.0.254"} ]]}}'EOF
NetworkAttachmentDefinition with CNI config file:
If NetworkAttachmentDefinition has no spec, multus find a file in defaultConfDir (‘/etc/cni/multus/net.d’, with same name in the ‘name’ field of CNI config.# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: "k8s.cni.cncf.io/v1"kind: NetworkAttachmentDefinitionmetadata:name: macvlan-conf-2EOF
# Execute following commands at all Kubernetes nodes (i.e. master and minions)$ cat <<EOF > /etc/cni/multus/net.d/macvlan2.conf{"cniVersion": "0.3.0","type": "macvlan","name": "macvlan-conf-2","master": "eth1","mode": "bridge","ipam": {"type": "host-local","ranges": [[ {"subnet": "11.10.0.0/16","rangeStart": "11.10.1.20","rangeEnd": "11.10.3.50"} ]]}}
Run pod with network annotation
Launch pod with text annotation
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: pod-case-01annotations:k8s.v1.cni.cncf.io/networks: macvlan-conf-1, macvlan-conf-2spec:containers:- name: pod-case-01image: docker.io/centos/tools:latestcommand:- /sbin/initEOF
Launch pod with text annotation for NetworkAttachmentDefinition in different namespace
You can also specify NetworkAttachmentDefinition with its namespace as adding <namespace>/
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: "k8s.cni.cncf.io/v1"kind: NetworkAttachmentDefinitionmetadata:name: macvlan-conf-3namespace: testns1spec:config: '{"cniVersion": "0.3.0","type": "macvlan","master": "eth1","mode": "bridge","ipam": {"type": "host-local","ranges": [[ {"subnet": "12.10.0.0/16","rangeStart": "12.10.1.20","rangeEnd": "12.10.3.50"} ]]}}'EOF$ cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: pod-case-02annotations:k8s.v1.cni.cncf.io/networks: testns1/macvlan-conf-3spec:containers:- name: pod-case-02image: docker.io/centos/tools:latestcommand:- /sbin/initEOF
Launch pod with text annotation with interface name
You can also specify interface name as adding @<ifname>.
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: pod-case-03annotations:k8s.v1.cni.cncf.io/networks: macvlan-conf-1@macvlan1spec:containers:- name: pod-case-03image: docker.io/centos/tools:latestcommand:- /sbin/initEOF
Launch pod with json annotation
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: pod-case-04annotations:k8s.v1.cni.cncf.io/networks: '[{ "name" : "macvlan-conf-1" },{ "name" : "macvlan-conf-2" }]'spec:containers:- name: pod-case-04image: docker.io/centos/tools:latestcommand:- /sbin/initEOF
Launch pod with json annotation for NetworkAttachmentDefinition in different namespace
You can also specify NetworkAttachmentDefinition with its namespace as adding "namespace": "<namespace>".
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: pod-case-05annotations:k8s.v1.cni.cncf.io/networks: '[{ "name" : "macvlan-conf-1","namespace": "testns1" }]'spec:containers:- name: pod-case-05image: docker.io/centos/tools:latestcommand:- /sbin/initEOF
Launch pod with json annotation with interface
You can also specify interface name as adding "interface": "<ifname>".
# Execute following command at Kubernetes master$ cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: pod-case-06annotations:k8s.v1.cni.cncf.io/networks: '[{ "name" : "macvlan-conf-1","interface": "macvlan1" },{ "name" : "macvlan-conf-2" }]'spec:containers:- name: pod-case-06image: docker.io/centos/tools:latestcommand:- /sbin/initEOF
Verifying pod network
Following the example of ip -d address output of above pod, “pod-case-06”:
# Execute following command at Kubernetes master$ kubectl exec -it pod-case-06 -- ip -d address1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 promiscuity 0 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever3: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group defaultlink/ether 0a:58:0a:f4:02:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0veth numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 10.244.2.6/24 scope global eth0valid_lft forever preferred_lft foreverinet6 fe80::ac66:45ff:fe7c:3a19/64 scope linkvalid_lft forever preferred_lft forever4: macvlan1@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group defaultlink/ether 4e:6d:7a:4e:14:87 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0macvlan mode bridge numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 10.10.1.22/16 scope global macvlan1valid_lft forever preferred_lft foreverinet6 fe80::4c6d:7aff:fe4e:1487/64 scope linkvalid_lft forever preferred_lft forever5: net2@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group defaultlink/ether 6e:e3:71:7f:86:f7 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0macvlan mode bridge numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535inet 11.10.1.22/16 scope global net2valid_lft forever preferred_lft foreverinet6 fe80::6ce3:71ff:fe7f:86f7/64 scope linkvalid_lft forever preferred_lft forever
| Interface name | Description |
|---|---|
| lo | loopback |
| eth0 | Default network interface (flannel) |
| macvlan1 | macvlan interface (macvlan-conf-1) |
| net2 | macvlan interface (macvlan-conf-2) |
Specifying a default route for a specific attachment
Typically, the default route for a pod will route traffic over the eth0 and therefore over the cluster-wide default network. You may wish to specify that a different network attachment will have the default route.
You can achieve this by using the JSON formatted annotation and specifying a default-route key.
NOTE: It’s important that you consider that this may impact some functionality of getting traffic to route over the cluster-wide default network.
For example, we have a this configuration for macvlan:
cat <<EOF | kubectl create -f -apiVersion: "k8s.cni.cncf.io/v1"kind: NetworkAttachmentDefinitionmetadata:name: macvlan-confspec:config: '{"cniVersion": "0.3.0","type": "macvlan","master": "eth0","mode": "bridge","ipam": {"type": "host-local","subnet": "192.168.2.0/24","rangeStart": "192.168.2.200","rangeEnd": "192.168.2.216","routes": [{ "dst": "0.0.0.0/0" }],"gateway": "192.168.2.1"}}'EOF
We can then create a pod which uses the default-route key in the JSON formatted k8s.v1.cni.cncf.io/networks annotation.
cat <<EOF | kubectl create -f -apiVersion: v1kind: Podmetadata:name: samplepodannotations:k8s.v1.cni.cncf.io/networks: '[{"name": "macvlan-conf","default-route": ["192.168.2.1"]}]'spec:containers:- name: samplepodcommand: ["/bin/bash", "-c", "trap : TERM INT; sleep infinity & wait"]image: dougbtv/centos-networkEOF
This will set 192.168.2.1 as the default route over the net1 interface, such as:
$ kubectl exec -it samplepod -- ip routedefault via 192.168.2.1 dev net110.244.0.0/24 dev eth0 proto kernel scope link src 10.244.0.16910.244.0.0/16 via 10.244.0.1 dev eth0
Entrypoint Parameters
Multus CNI, when installed using the daemonset-style installation uses an entrypoint script which copies the Multus binary into place, places CNI configurations. This entrypoint takes a variety of parameters for customization.
Typically, you’d modified the daemonset YAML itself to specify these parameters.
For example, the command and args parameters in the containers section of the DaemonSet may look something like:
command: ["/entrypoint.sh"]args:- "--multus-conf-file=auto"- "--namespace-isolation=true"- "--multus-log-level=verbose"
Note that some of the defaults have directories inside the root directory named /host/, this is because it is deployed as a container and we have host file system locations mapped into this directory inside the container. If you use other directories, you may have to change the mounted volumes.
Entrypoint script parameters
Each parameter is shown with the default as the value.
--cni-conf-dir=/host/etc/cni/net.d
This is the configuration directory where Multus will write its configuration file.
--cni-bin-dir=/host/opt/cni/bin
This the directory in which the Multus binary will be installed.
--namespace-isolation=false
Setting this option to true enables the Namespace isolation feature, which insists that custom resources must be created in the same namespace as the pods, otherwise it will refuse to attach those definitions as additional interfaces. See (the configuration guide for more information)[configuration.md].
--global-namespaces=default,foo,bar
The --global-namespaces works only when --namespace-isolation=true. This takes a comma-separated list of namespaces which can be referred to globally when namespace isolation is enabled. See (the configuration guide for more information)[configuration.md].
--multus-bin-file=/usr/src/multus-cni/bin/multus
This option lets you set which binary executable to copy from the container onto the host (into the directory specified by --cni-bin-dir), allowing one to copy an alternate version or build of Multus CNI.
--multus-conf-file=/usr/src/multus-cni/images/70-multus.conf
The --multus-conf-file is one of two options; it can be set to a source file to be copied into the location specified by --cni-conf-dir. Or, to a value of auto, that is: --multus-conf-file=auto.
The automatic configuration option is used to automatically generate Multus configurations given existing on-disk CNI configurations for your default network.
In the case that --multus-conf-file=auto — The entrypoint script will look at the --multus-autoconfig-dir (by default, the same as the --cni-conf-dir). Multus will wait (600 seconds) until there’s a CNI configuration file there, and it will take the alphabetically first configuration there, and it will wrap that configuration into a Multus configuration.
--multus-autoconfig-dir=/host/etc/cni/net.d
Used only with --multus-conf-file=auto. This option allows one to set which directory will be used to generate configuration files.
This can be used if you have your CNI configuration stored in an alternate location, or, you have constraints on race conditions where you’d like to generate your default network configuration first, and then only have Multus write its configuration when it finds that configuration — allowing only Multus to write the CNI configuration in the --cni-conf-dir, therefore notifying the Kubelet that the node is in a ready state.
--multus-kubeconfig-file-host=/etc/cni/net.d/multus.d/multus.kubeconfig
Used only with --multus-conf-file=auto. Allows you to specify an alternate path to the Kubeconfig.
--multus-master-cni-file-name=
The --multus-master-cni-file-name can be used to select the cni file as the master cni, rather than the first file in cni-conf-dir. For example, --multus-master-cni-file-name=10-calico.conflist.
--multus-log-level=--multus-log-file=
Used only with --multus-conf-file=auto. See the documentation for logging for which values are permitted.
Used only with --multus-conf-file=auto. Allows you to specify CNI spec version. Please set if you need to speicfy CNI spec version.
--cni-version=
In some cases, the original CNI configuration that the Multus configuration was generated from (using --multus-conf-file=auto) may be used as a sort of semaphor for network readiness — as this model is used by the Kubelet itself. If you need to disable Multus’ availablity, you may wish to clean out the generated configuration file when the source file for autogeneration of the config file is no longer present. You can use this functionality by setting:
--cleanup-config-on-exit=true
When using CRIO, you may need to restart CRIO to get the Multus configuration file to take — this is rarely necessary.
--restart-crio=false
Additionally when using CRIO, you may wish to have the CNI config file that’s used as the source for --multus-conf-file=auto renamed. This boolean option when set to true automatically renames the file with a .old suffix to the original filename.
--rename-conf-file=true
When using --multus-conf-file=auto you may also care to specify a binDir in the configuration, this can be accomplished using the --additional-bin-dir option.
--additional-bin-dir=/opt/multus/bin
Sometimes, you may wish to not have the entrypoint copy the binary file onto the host. Potentially, you have another way to copy in a specific version of Multus, for example. By default, it’s always copied, but you may disable the copy with:
--skip-multus-binary-copy=true
If you wish to have auto configuration use the readinessindicatorfile in the configuration, you can use the --readiness-indicator-file to express which file should be used as the readiness indicator.
--readiness-indicator-file=/path/to/file
# Above datae:# Latest commit ec30b99 on Jun 2
