1. # Kubernetes CNI Flannel GuideLine
  2. # 时间:20210804-[20:00-22:00]
  3. # 主题:Flannel IPAM
  4. # 文档:https://www.yuque.com/docs/share/256c4707-36e8-49e1-9fa6-5a8b689efdf1?# 《20210804-Flannel IPAM(二)》
  5. # 作业:CNI Flannel设计理念
  1. # FAQ:

Flannel 网络通路创建和IPAM
a9b5fd90033ff2160a04f58bd4b60bc5.jpg

  1. # Flannel 过程分析:
  2. # 0.Flanneld启动的时候,Flannel会拉取pod的CIDR,from apiServer。然后写在subnet.env 文件中:
  3. [root@k8s-2 netns]# cat /run/flannel/subnet.env
  4. FLANNEL_NETWORK=10.244.0.0/16
  5. FLANNEL_SUBNET=10.244.1.1/24
  6. FLANNEL_MTU=1500
  7. FLANNEL_IPMASQ=true
  8. [root@k8s-2 netns]#
  9. # 1.此时Kubernetes调用container runtime通过CRI plugin
  10. # 2.然后CRI plugin创建network namespace,然后依据CNI 的config file调用CNI plugin。
  11. # CNI config file 为:
  12. [root@k8s-2 netns]# cat /etc/cni/net.d/10-flannel.conflist
  13. {
  14. "name": "cbr0",
  15. "cniVersion": "0.3.1",
  16. "plugins": [
  17. {
  18. "type": "flannel",
  19. "delegate": {
  20. "hairpinMode": true,
  21. "isDefaultGateway": true
  22. }
  23. },
  24. {
  25. "type": "portmap",
  26. "capabilities": {
  27. "portMappings": true
  28. }
  29. }
  30. ]
  31. }
  32. [root@k8s-2 netns]#
  33. # network namespace 放在目录: /var/run/docker/netns/下:
  34. # [root@k8s-2 netns]# pwd
  35. /var/run/docker/netns
  36. [root@k8s-2 netns]# ls
  37. 0b3ff82b1c94 46755a4b7b2d 5f7eae9b78b4 c5568ce529e7 default
  38. [root@k8s-2 netns]#
  39. [root@k8s-2 netns]# docker ps
  40. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  41. bcd96a894721 burlyluo/nettoolbox "/sbin/tini -g -- /e…" 30 hours ago Up 30 hours 80/tcp c2
  42. 8e696b106c2c fcfb8414a293 "/sbin/tini -g -- /e…" 31 hours ago Up 31 hours k8s_nettoolbox_cni-b9kmz_default_7193ba8e-ea32-4f72-bddd-6686c2218411_1
  43. b3c3124d47d8 bfe3a36ebd25 "/coredns -conf /etc…" 31 hours ago Up 31 hours k8s_coredns_coredns-7f89b7bc75-qmx68_kube-system_460287e7-7010-4159-8d78-7198d6f9277b_1
  44. 73bbe941e480 bfe3a36ebd25 "/coredns -conf /etc…" 31 hours ago Up 31 hours k8s_coredns_coredns-7f89b7bc75-pbq88_kube-system_0d69400d-8f0d-4062-b671-ee0872d57559_1
  45. ba25114a4499 dee1cac4dd20 "/opt/bin/flanneld -…" 31 hours ago Up 31 hours k8s_kube-flannel_kube-flannel-ds-n48gr_kube-system_26174d63-0874-4396-a288-bb4e20eacedb_1
  46. c6cd36de8028 5384b1650507 "/usr/local/bin/kube…" 31 hours ago Up 31 hours k8s_kube-proxy_kube-proxy-rd8vv_kube-system_fde205f0-4f4b-4cd1-a55e-37cf9b46a4b7_3
  47. [root@k8s-2 netns]# for i in $(docker ps |awk -F " " '{print $1}'|grep -v CONTAINER);do echo $i;docker inspect $i|grep SandboxKey;done # 我们可以通过次查看各自Pod所在的NS。
  48. "SandboxKey": "/var/run/docker/netns/c5568ce529e7",
  49. bcd96a894721
  50. "SandboxKey": "",
  51. 8e696b106c2c
  52. "SandboxKey": "",
  53. b3c3124d47d8
  54. "SandboxKey": "",
  55. 73bbe941e480
  56. "SandboxKey": "/var/run/docker/netns/46755a4b7b2d",
  57. a7501f920b34
  58. "SandboxKey": "/var/run/docker/netns/0b3ff82b1c94",
  59. 6db3dc106e4f
  60. "SandboxKey": "/var/run/docker/netns/5f7eae9b78b4",
  61. 998a88c30756
  62. "SandboxKey": "",
  63. ba25114a4499
  64. "SandboxKey": "",
  65. c6cd36de8028
  66. "SandboxKey": "/var/run/docker/netns/default",
  67. 2c36a7ab7f1c
  68. "SandboxKey": "/var/run/docker/netns/default",
  69. 5f94cb7c58a1
  70. [root@k8s-2 netns]#
  71. # 3.Flannel CNI Plugin配置和调用Bridge CNI plugin,从这里我们可以看出,Flannel实际上设计了一个二层调用,去调用下层的Bridge。
  72. # cat /var/lib/cni/flannel/b75233adc93b128d2d49ec0ed6e1919825f5c0062251d88aac3bad129e72df33 | python -m json.tool
  73. 查看该调用Bridge的详情:
  74. {
  75. "cniVersion": "0.3.1",
  76. "hairpinMode": true,
  77. "ipMasq": false,
  78. "ipam": {
  79. "routes": [
  80. {
  81. "dst": "10.244.0.0/16" # 路由信息,到10.244.0.0/16的目的网络
  82. }
  83. ],
  84. "subnet": "10.244.1.0/24", # 使用的子网,采用的每一个节点上有一个单独的24位掩码的子网。
  85. "type": "host-local" # 使用的IPAM的类型。通常cni.dev中介绍有三种:1.static。2.dhcp。3.host-local
  86. },
  87. "isDefaultGateway": true, # 是否是默认路由。
  88. "isGateway": true,
  89. "mtu": 1500, # 本环境是host-gw的环境,所以mtu是1500,在overlay的网络中,此时应该是小于1500的。比如1450
  90. "name": "cbr0", # name:实际上我们通常使用的是cni0
  91. "type": "bridge" # type
  92. }
  93. # 4.然后Bridge Plugin会按照上边的配置文件创建cni0的bridge。此时会创建veth pair,一段在Pod所在的ns,一端在cni0上,然后调用host-local 的IPAM。
  94. # 5.host-local的IPAM形式:
  95. "ipam": {
  96. "routes": [
  97. {
  98. "dst": "10.244.0.0/16" # 路由信息,到10.244.0.0/16的目的网络
  99. }
  100. ],
  101. "subnet": "10.244.1.0/24", # 使用的子网,采用的每一个节点上有一个单独的24位掩码的子网。
  102. "type": "host-local" # 使用的IPAM的类型。通常cni.dev中介绍有三种:1.static。2.dhcp。3.host-local
  103. },
  104. host-local的IPAM此时如果有Pod创建的话,会返回一个ip地址,和gateway信息给到Pod中。
  105. IP地址信息以文件的形式存储在本地的文件中:
  106. [root@k8s-2 cbr0]# ls
  107. 10.244.1.5 10.244.1.6 10.244.1.7 last_reserved_ip.0 lock
  108. [root@k8s-2 cbr0]#
  1. 过程总结:
  2. This plugin is designed to work in conjunction with flannel , a network fabric for containers. When flannel daemon is started, it outputs a /run/flannel/subnet.env file that looks like this:
  3. FLANNEL_NETWORK=10.1.0.0/16
  4. FLANNEL_SUBNET=10.1.17.1/24
  5. FLANNEL_MTU=1472
  6. FLANNEL_IPMASQ=true
  7. This information reflects the attributes of flannel network on the host. The flannel CNI plugin uses this information to configure another CNI plugin, such as bridge plugin.
  8. Given the following network configuration file and the contents of /run/flannel/subnet.env above:
  9. {
  10. "name": "mynet",
  11. "type": "flannel"
  12. }
  13. the flannel plugin will generate another network configuration file:
  14. {
  15. "name": "mynet",
  16. "type": "bridge",
  17. "mtu": 1472,
  18. "ipMasq": false,
  19. "isGateway": true,
  20. "ipam": {
  21. "type": "host-local",
  22. "subnet": "10.1.17.0/24"
  23. }
  24. }
  25. It will invoke bridge plugin, passing it the generated configuration. As can be seen from above, the flannel plugin, default, will delegate to the bridge plugin. If additional configuration values need to be passed to bridge plugin, it can be done via the delegate field:
  26. {
  27. "name": "mynet",
  28. "type": "flannel",
  29. "delegate": {
  30. "bridge": "mynet0",
  31. "mtu": 1400
  32. }
  33. }
  34. This supplies a configuration parameter to the bridge plugin – the created bridge will now be named mynet0. Notice that mtu has also been specified and this value will not be overwritten by flannel plugin.
  35. Additionally, the delegate field can be used to select a different kind of plugin altogether. To use ipvlan instead of bridge, the following configuration can be specified:
  36. {
  37. "name": "mynet",
  38. "type": "flannel",
  39. "delegate": {
  40. "type": "ipvlan",
  41. "master": "eth0"
  42. }
  43. }
  44. The ipam part of the network configuration generated for the delegated plugin, can also be customized by adding a base ipam section to the input flannel network configuration. This ipam element is then updated with the flannel subnet, a route to the flannel network and, unless provided, an ipam type set to host-local before being provided to the delegated plugin.
  45. cat /etc/cni/net.d/10-flannel.conflist
  46. {
  47. "name": "cbr0",
  48. "cniVersion": "0.3.1",
  49. "plugins": [
  50. {
  51. "type": "flannel",
  52. "delegate": {
  53. "hairpinMode": true,
  54. "isDefaultGateway": true
  55. }
  56. },
  57. {
  58. "type": "portmap",
  59. "capabilities": {
  60. "portMappings": true
  61. }
  62. }
  63. ]
  64. }
  1. # IPAM:
  2. host-local IPAM allocates IPv4 and IPv6 addresses out of a specified address range. Optionally, it can include a DNS configuration from a resolv.conf file on the host.
  3. host-local IPAM plugin allocates ip addresses out of a set of address ranges. It stores the state locally on the host filesystem, therefore ensuring uniqueness of IP addresses on a single host.
  4. The allocator can allocate multiple ranges, and supports sets of multiple (disjoint) subnets. The allocation strategy is loosely round-robin within each range set.
  5. Note that the key ranges is a list of range sets. That is to say, the length of the top-level array is the number of addresses returned. The second-level array is a set of subnets to use as a pool of possible addresses.
  6. Supported arguments
  7. The following CNI_ARGS are supported:
  8. ip: request a specific IP address from a subnet.
  9. The following args conventions are supported:
  10. ips (array of strings): A list of custom IPs to attempt to allocate
  11. The following Capability Args are supported:
  12. ipRanges: The exact same as the ranges array - a list of address pools
  13. Custom IP allocation
  14. For every requested custom IP, the host-local allocator will request that IP if it falls within one of the range objects. Thus it is possible to specify multiple custom IPs and multiple ranges.
  15. If any requested IPs cannot be reserved, either because they are already in use or are not part of a specified range, the plugin will return an error.
  16. Example configurations:
  17. Note that the key ranges is a list of range sets. That is to say, the length of the top-level array is the number of addresses returned. The second-level array is a set of subnets to use as a pool of possible addresses.
  18. {
  19. "ipam": {
  20. "type": "host-local",
  21. "ranges": [
  22. [
  23. {
  24. "subnet": "10.10.0.0/16",
  25. "rangeStart": "10.10.1.20",
  26. "rangeEnd": "10.10.3.50",
  27. "gateway": "10.10.0.254"
  28. },
  29. {
  30. "subnet": "172.16.5.0/24"
  31. }
  32. ],
  33. [
  34. {
  35. "subnet": "3ffe:ffff:0:01ff::/64",
  36. "rangeStart": "3ffe:ffff:0:01ff::0010",
  37. "rangeEnd": "3ffe:ffff:0:01ff::0020"
  38. }
  39. ]
  40. ],
  41. "routes": [
  42. { "dst": "0.0.0.0/0" },
  43. { "dst": "192.168.0.0/16", "gw": "10.10.5.1" },
  44. { "dst": "3ffe:ffff:0:01ff::1/64" }
  45. ],
  46. "dataDir": "/run/my-orchestrator/container-ipam-state"
  47. }
  48. }
  49. [root@k8s-1 bin]# echo '{ "cniVersion": "0.3.1", "name": "examplenet", "ipam": { "type": "host-local", "ranges": [ [{"subnet": "203.0.113.0/24"}], [{"subnet": "2001:db8:1::/64"}]], "dataDir": "/tmp/cni-example" } }' | CNI_COMMAND=ADD CNI_CONTAINERID=example CNI_NETNS=/dev/null CNI_IFNAME=dummy0 CNI_PATH=. ./host-local
  50. {
  51. "cniVersion": "0.3.1",
  52. "ips": [
  53. {
  54. "version": "4",
  55. "address": "203.0.113.2/24",
  56. "gateway": "203.0.113.1"
  57. },
  58. {
  59. "version": "6",
  60. "address": "2001:db8:1::2/64",
  61. "gateway": "2001:db8:1::1"
  62. }
  63. ],
  64. "dns": {}
  65. }[root@k8s-1 bin]# pwd
  66. /opt/cni/bin
  67. [root@k8s-1 bin]# echo '{ "cniVersion": "0.3.1", "name": "examplenet", "ipam": { "type": "host-local", "ranges": [ [{"subnet": "203.0.115.0/24"}], [{"subnet": "2002:db8:1::/64"}]], "dataDir": "/tmp/cni-example" } }' | CNI_COMMAND=ADD CNI_CONTAINERID=example CNI_NETNS=/dev/null CNI_IFNAME=dummy0 CNI_PATH=. ./host-local
  68. {
  69. "cniVersion": "0.3.1",
  70. "ips": [
  71. {
  72. "version": "4",
  73. "address": "203.0.115.2/24",
  74. "gateway": "203.0.115.1"
  75. },
  76. {
  77. "version": "6",
  78. "address": "2002:db8:1::2/64",
  79. "gateway": "2002:db8:1::1"
  80. }
  81. ],
  82. "dns": {}
  83. }[root@k8s-1 bin]#
  84. Network configuration reference:
  85. type (string, required): “host-local”.
  86. routes (string, optional): list of routes to add to the container namespace. Each route is a dictionary with “dst” and optional “gw” fields. If “gw” is omitted, value of “gateway” will be used.
  87. resolvConf (string, optional): Path to a resolv.conf on the host to parse and return as the DNS configuration
  88. dataDir (string, optional): Path to a directory to use for maintaining state, e.g. which IPs have been allocated to which containers
  89. ranges, (array, required, nonempty) an array of arrays of range objects:
  90. subnet (string, required): CIDR block to allocate out of.
  91. rangeStart (string, optional): IP inside of “subnet” from which to start allocating addresses. Defaults to “.2” IP inside of the “subnet” block.
  92. rangeEnd (string, optional): IP inside of “subnet” with which to end allocating addresses. Defaults to “.254” IP inside of the “subnet” block for ipv4, “.255” for IPv6
  93. gateway (string, optional): IP inside of “subnet” to designate as the gateway. Defaults to “.1” IP inside of the “subnet” block.
  94. Older versions of the host-local plugin did not support the ranges array. Instead, all the properties in the range object were top-level. This is still supported but deprecated.
  95. Allocated IP addresses are stored as files in /var/lib/cni/networks/$NETWORK_NAME. The path can be customized with the dataDir option listed above. Environments where IPs are released automatically on reboot (e.g. running containers are not restored) may wish to specify /var/run/cni or another tmpfs mounted directory instead.

IPAM Calico VS Multus

  1. # Demo for the CNI Config:
  2. [root@master1 net.d]# cat 00-multus.conf
  3. {
  4. "cniVersion": "0.3.1",
  5. "kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig",
  6. "name": "multus-cni-network",
  7. "type": "multus",
  8. "confDir": "/etc/cni/net.d",
  9. "binDir": "/opt/cni/bin",
  10. "logFile": "/var/log/multus.log",
  11. "logLevel": "debug",
  12. "multusNamespace": "kube-system",
  13. "capabilities": {
  14. "portMappings": true
  15. },
  16. "delegates": [
  17. {
  18. "cniVersion": "0.3.1",
  19. "name": "calico",
  20. "plugins": [
  21. {
  22. "datastore_type": "kubernetes", # this parameter.||||||kubernetes or etcd's address.
  23. "ipam": {
  24. "type": "whereabouts"
  25. },
  26. "kubernetes": {
  27. "kubeconfig": "/etc/cni/net.d/calico-kubeconfig"
  28. },
  29. "log_level": "info",
  30. "mtu": 1500,
  31. "nodename": "k8s-1",
  32. "policy": {
  33. "type": "k8s"
  34. },
  35. "type": "calico"
  36. },
  37. {
  38. "type": "tuning",
  39. "sysctl": {
  40. "net.ipv6.conf.all.accept_ra": "0",
  41. "net.ipv6.conf.default.accept_ra": "0",
  42. "net.ipv6.conf.lo.accept_ra": "0",
  43. "net.ipv6.conf.all.accept_dad": "0",
  44. "net.ipv6.conf.default.accept_dad": "0",
  45. "net.ipv6.conf.all.ndisc_notify": "1",
  46. "net.ipv6.conf.default.ndisc_notify": "1"
  47. }
  48. },
  49. {
  50. "capabilities": {
  51. "portMappings": true
  52. },
  53. "snat": true,
  54. "type": "portmap"
  55. }
  56. ]
  57. }
  58. ]
  59. }
  60. ############
  61. multus
  62. ############
  63. VS
  64. ############
  65. Calico
  66. ############
  67. [root@k8s-1 net.d]# cat 10-calico.conflist
  68. {
  69. "name": "k8s-pod-network",
  70. "cniVersion": "0.3.1",
  71. "plugins": [
  72. {
  73. "type": "calico",
  74. "log_level": "info",
  75. "log_file_path": "/var/log/calico/cni/cni.log",
  76. "datastore_type": "kubernetes",
  77. "nodename": "k8s-1",
  78. "mtu": 0,
  79. "ipam": {
  80. "type": "calico-ipam"
  81. },
  82. "policy": {
  83. "type": "k8s"
  84. },
  85. "kubernetes": {
  86. "kubeconfig": "/etc/cni/net.d/calico-kubeconfig"
  87. }
  88. },
  89. {
  90. "type": "portmap",
  91. "snat": true,
  92. "capabilities": {"portMappings": true}
  93. },
  94. {
  95. "type": "bandwidth",
  96. "capabilities": {"bandwidth": true}
  97. }
  98. ]
  99. }
  100. # Calico VS Multus
  101. ||
  102. \/
  103. # NAD #

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).

  1. $ 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).

  1. $ 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)

  1. $ mkdir -p /etc/cni/net.d
  2. $ cat >/etc/cni/net.d/00-multus.conf <<EOF
  3. {
  4. "name": "multus-cni-network",
  5. "type": "multus",
  6. "readinessindicatorfile": "/var/run/flannel/subnet.env",
  7. "delegates": [
  8. {
  9. "NOTE1": "This is example, wrote your CNI config in delegates",
  10. "NOTE2": "If you use flannel, you also need to run flannel daemonset before!",
  11. "type": "flannel",
  12. "name": "flannel.1",
  13. "delegate": {
  14. "isDefaultGateway": true
  15. }
  16. }
  17. ],
  18. "kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig"
  19. }
  20. 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:

  1. # Execute following commands at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: v1
  4. kind: ServiceAccount
  5. metadata:
  6. name: multus
  7. namespace: kube-system
  8. ---
  9. kind: ClusterRole
  10. apiVersion: rbac.authorization.k8s.io/v1beta1
  11. metadata:
  12. name: multus
  13. rules:
  14. - apiGroups: ["k8s.cni.cncf.io"]
  15. resources:
  16. - '*'
  17. verbs:
  18. - '*'
  19. - apiGroups:
  20. - ""
  21. resources:
  22. - pods
  23. - pods/status
  24. verbs:
  25. - get
  26. - update
  27. ---
  28. kind: ClusterRoleBinding
  29. apiVersion: rbac.authorization.k8s.io/v1beta1
  30. metadata:
  31. name: multus
  32. roleRef:
  33. apiGroup: rbac.authorization.k8s.io
  34. kind: ClusterRole
  35. name: multus
  36. subjects:
  37. - kind: ServiceAccount
  38. name: multus
  39. namespace: kube-system
  40. EOF

Set up kubeconfig file

Create kubeconfig at master node as following commands:

  1. # Execute following command at Kubernetes master
  2. $ mkdir -p /etc/cni/net.d/multus.d
  3. $ 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"')
  4. $ 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 )
  5. $ KUBERNETES_SERVICE_PROTO=$(kubectl get all -o json | jq -r .items[0].spec.ports[0].name)
  6. $ KUBERNETES_SERVICE_HOST=$(kubectl get all -o json | jq -r .items[0].spec.clusterIP)
  7. $ KUBERNETES_SERVICE_PORT=$(kubectl get all -o json | jq -r .items[0].spec.ports[0].port)
  8. $ cat > /etc/cni/net.d/multus.d/multus.kubeconfig <<EOF
  9. # Kubeconfig file for Multus CNI plugin.
  10. apiVersion: v1
  11. kind: Config
  12. clusters:
  13. - name: local
  14. cluster:
  15. server: ${KUBERNETES_SERVICE_PROTOCOL:-https}://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}
  16. certificate-authority-data: ${SERVICEACCOUNT_CA}
  17. users:
  18. - name: multus
  19. user:
  20. token: "${SERVICEACCOUNT_TOKEN}"
  21. contexts:
  22. - name: multus-context
  23. context:
  24. cluster: local
  25. user: multus
  26. current-context: multus-context
  27. EOF

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

  1. $ 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:

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: apiextensions.k8s.io/v1beta1
  4. kind: CustomResourceDefinition
  5. metadata:
  6. name: network-attachment-definitions.k8s.cni.cncf.io
  7. spec:
  8. group: k8s.cni.cncf.io
  9. version: v1
  10. scope: Namespaced
  11. names:
  12. plural: network-attachment-definitions
  13. singular: network-attachment-definition
  14. kind: NetworkAttachmentDefinition
  15. shortNames:
  16. - net-attach-def
  17. validation:
  18. openAPIV3Schema:
  19. properties:
  20. spec:
  21. properties:
  22. config:
  23. type: string
  24. EOF

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 in config: field.
    1. # Execute following command at Kubernetes master
    2. $ cat <<EOF | kubectl create -f -
    3. apiVersion: "k8s.cni.cncf.io/v1"
    4. kind: NetworkAttachmentDefinition
    5. metadata:
    6. name: macvlan-conf-1
    7. spec:
    8. config: '{
    9. "cniVersion": "0.3.0",
    10. "type": "macvlan",
    11. "master": "eth1",
    12. "mode": "bridge",
    13. "ipam": {
    14. "type": "host-local",
    15. "ranges": [
    16. [ {
    17. "subnet": "10.10.0.0/16",
    18. "rangeStart": "10.10.1.20",
    19. "rangeEnd": "10.10.3.50",
    20. "gateway": "10.10.0.254"
    21. } ]
    22. ]
    23. }
    24. }'
    25. 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.
    1. # Execute following command at Kubernetes master
    2. $ cat <<EOF | kubectl create -f -
    3. apiVersion: "k8s.cni.cncf.io/v1"
    4. kind: NetworkAttachmentDefinition
    5. metadata:
    6. name: macvlan-conf-2
    7. EOF
  1. # Execute following commands at all Kubernetes nodes (i.e. master and minions)
  2. $ cat <<EOF > /etc/cni/multus/net.d/macvlan2.conf
  3. {
  4. "cniVersion": "0.3.0",
  5. "type": "macvlan",
  6. "name": "macvlan-conf-2",
  7. "master": "eth1",
  8. "mode": "bridge",
  9. "ipam": {
  10. "type": "host-local",
  11. "ranges": [
  12. [ {
  13. "subnet": "11.10.0.0/16",
  14. "rangeStart": "11.10.1.20",
  15. "rangeEnd": "11.10.3.50"
  16. } ]
  17. ]
  18. }
  19. }

Run pod with network annotation

Launch pod with text annotation

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6. name: pod-case-01
  7. annotations:
  8. k8s.v1.cni.cncf.io/networks: macvlan-conf-1, macvlan-conf-2
  9. spec:
  10. containers:
  11. - name: pod-case-01
  12. image: docker.io/centos/tools:latest
  13. command:
  14. - /sbin/init
  15. EOF

Launch pod with text annotation for NetworkAttachmentDefinition in different namespace

You can also specify NetworkAttachmentDefinition with its namespace as adding <namespace>/

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: "k8s.cni.cncf.io/v1"
  4. kind: NetworkAttachmentDefinition
  5. metadata:
  6. name: macvlan-conf-3
  7. namespace: testns1
  8. spec:
  9. config: '{
  10. "cniVersion": "0.3.0",
  11. "type": "macvlan",
  12. "master": "eth1",
  13. "mode": "bridge",
  14. "ipam": {
  15. "type": "host-local",
  16. "ranges": [
  17. [ {
  18. "subnet": "12.10.0.0/16",
  19. "rangeStart": "12.10.1.20",
  20. "rangeEnd": "12.10.3.50"
  21. } ]
  22. ]
  23. }
  24. }'
  25. EOF
  26. $ cat <<EOF | kubectl create -f -
  27. apiVersion: v1
  28. kind: Pod
  29. metadata:
  30. name: pod-case-02
  31. annotations:
  32. k8s.v1.cni.cncf.io/networks: testns1/macvlan-conf-3
  33. spec:
  34. containers:
  35. - name: pod-case-02
  36. image: docker.io/centos/tools:latest
  37. command:
  38. - /sbin/init
  39. EOF

Launch pod with text annotation with interface name

You can also specify interface name as adding @<ifname>.

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6. name: pod-case-03
  7. annotations:
  8. k8s.v1.cni.cncf.io/networks: macvlan-conf-1@macvlan1
  9. spec:
  10. containers:
  11. - name: pod-case-03
  12. image: docker.io/centos/tools:latest
  13. command:
  14. - /sbin/init
  15. EOF

Launch pod with json annotation

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6. name: pod-case-04
  7. annotations:
  8. k8s.v1.cni.cncf.io/networks: '[
  9. { "name" : "macvlan-conf-1" },
  10. { "name" : "macvlan-conf-2" }
  11. ]'
  12. spec:
  13. containers:
  14. - name: pod-case-04
  15. image: docker.io/centos/tools:latest
  16. command:
  17. - /sbin/init
  18. EOF

Launch pod with json annotation for NetworkAttachmentDefinition in different namespace

You can also specify NetworkAttachmentDefinition with its namespace as adding "namespace": "<namespace>".

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6. name: pod-case-05
  7. annotations:
  8. k8s.v1.cni.cncf.io/networks: '[
  9. { "name" : "macvlan-conf-1",
  10. "namespace": "testns1" }
  11. ]'
  12. spec:
  13. containers:
  14. - name: pod-case-05
  15. image: docker.io/centos/tools:latest
  16. command:
  17. - /sbin/init
  18. EOF

Launch pod with json annotation with interface

You can also specify interface name as adding "interface": "<ifname>".

  1. # Execute following command at Kubernetes master
  2. $ cat <<EOF | kubectl create -f -
  3. apiVersion: v1
  4. kind: Pod
  5. metadata:
  6. name: pod-case-06
  7. annotations:
  8. k8s.v1.cni.cncf.io/networks: '[
  9. { "name" : "macvlan-conf-1",
  10. "interface": "macvlan1" },
  11. { "name" : "macvlan-conf-2" }
  12. ]'
  13. spec:
  14. containers:
  15. - name: pod-case-06
  16. image: docker.io/centos/tools:latest
  17. command:
  18. - /sbin/init
  19. EOF

Verifying pod network

Following the example of ip -d address output of above pod, “pod-case-06”:

  1. # Execute following command at Kubernetes master
  2. $ kubectl exec -it pod-case-06 -- ip -d address
  3. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
  4. link/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 65535
  5. inet 127.0.0.1/8 scope host lo
  6. valid_lft forever preferred_lft forever
  7. inet6 ::1/128 scope host
  8. valid_lft forever preferred_lft forever
  9. 3: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
  10. link/ether 0a:58:0a:f4:02:06 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0
  11. veth numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
  12. inet 10.244.2.6/24 scope global eth0
  13. valid_lft forever preferred_lft forever
  14. inet6 fe80::ac66:45ff:fe7c:3a19/64 scope link
  15. valid_lft forever preferred_lft forever
  16. 4: macvlan1@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
  17. link/ether 4e:6d:7a:4e:14:87 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0
  18. macvlan mode bridge numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
  19. inet 10.10.1.22/16 scope global macvlan1
  20. valid_lft forever preferred_lft forever
  21. inet6 fe80::4c6d:7aff:fe4e:1487/64 scope link
  22. valid_lft forever preferred_lft forever
  23. 5: net2@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default
  24. link/ether 6e:e3:71:7f:86:f7 brd ff:ff:ff:ff:ff:ff link-netnsid 0 promiscuity 0
  25. macvlan mode bridge numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
  26. inet 11.10.1.22/16 scope global net2
  27. valid_lft forever preferred_lft forever
  28. inet6 fe80::6ce3:71ff:fe7f:86f7/64 scope link
  29. valid_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:

  1. cat <<EOF | kubectl create -f -
  2. apiVersion: "k8s.cni.cncf.io/v1"
  3. kind: NetworkAttachmentDefinition
  4. metadata:
  5. name: macvlan-conf
  6. spec:
  7. config: '{
  8. "cniVersion": "0.3.0",
  9. "type": "macvlan",
  10. "master": "eth0",
  11. "mode": "bridge",
  12. "ipam": {
  13. "type": "host-local",
  14. "subnet": "192.168.2.0/24",
  15. "rangeStart": "192.168.2.200",
  16. "rangeEnd": "192.168.2.216",
  17. "routes": [
  18. { "dst": "0.0.0.0/0" }
  19. ],
  20. "gateway": "192.168.2.1"
  21. }
  22. }'
  23. EOF

We can then create a pod which uses the default-route key in the JSON formatted k8s.v1.cni.cncf.io/networks annotation.

  1. cat <<EOF | kubectl create -f -
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: samplepod
  6. annotations:
  7. k8s.v1.cni.cncf.io/networks: '[{
  8. "name": "macvlan-conf",
  9. "default-route": ["192.168.2.1"]
  10. }]'
  11. spec:
  12. containers:
  13. - name: samplepod
  14. command: ["/bin/bash", "-c", "trap : TERM INT; sleep infinity & wait"]
  15. image: dougbtv/centos-network
  16. EOF

This will set 192.168.2.1 as the default route over the net1 interface, such as:

  1. $ kubectl exec -it samplepod -- ip route
  2. default via 192.168.2.1 dev net1
  3. 10.244.0.0/24 dev eth0 proto kernel scope link src 10.244.0.169
  4. 10.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:

  1. command: ["/entrypoint.sh"]
  2. args:
  3. - "--multus-conf-file=auto"
  4. - "--namespace-isolation=true"
  5. - "--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.

  1. --cni-conf-dir=/host/etc/cni/net.d

This is the configuration directory where Multus will write its configuration file.

  1. --cni-bin-dir=/host/opt/cni/bin

This the directory in which the Multus binary will be installed.

  1. --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].

  1. --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].

  1. --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.

  1. --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.

  1. --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.

  1. --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.

  1. --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.

  1. --multus-log-level=
  2. --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.

  1. --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:

  1. --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.

  1. --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.

  1. --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.

  1. --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:

  1. --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.

  1. --readiness-indicator-file=/path/to/file