Skip to content
LF Cert Trainer

Lesson 112 · Section 9: Networking

Lesson 112: CNI in Kubernetes: who calls the network plugin, and where it lives

How Kubernetes hands Pod networking to a CNI plugin: the container runtime that calls it, the two directories on every node that decide which plugin runs, how plugins get installed, and how to read the errors when the network is not ready. · 20 min read · 3 min video

1/9CNI in Kubernetes
0:00 / 2:57

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why Kubernetes needs a network plugin

Kubernetes makes three promises about networking: every Pod has its own IP address, every Pod can reach every other Pod without NAT, and agents on a node can reach the Pods on that node. Then it does something surprising: it ships no code that fulfils them. A cluster straight out of kubeadm init cannot give a Pod an address.

The reason is that there is no single right way to build that network. A laptop cluster wants a simple bridge. A cloud wants Pods to use real cloud addresses. A security team wants encryption and network policies. So Kubernetes only states the rules and leaves the building to a plugin that follows the Container Network Interface (CNI), the standard you met in the CNI lesson: a plugin is a program that is asked to add a container to a network or delete it from one.

This lesson answers the questions that standard leaves open inside a cluster: who runs the plugin, where it finds it, when, and what you see when it is missing.

What it is made of

From a scheduled Pod to a wired networkCRIcreatesreads first fileruns ADDwireskubeletsees a new PodcontainerdRunPodSandboxNetwork namespaceempty, only lo/etc/cni/net.dwhich plugin/opt/cni/binplugin binariesPod networketh0 + IP + routes
From a scheduled Pod to a wired network. The kubelet never touches the network. It asks the container runtime for a Pod sandbox, and the runtime runs the CNI plugin to connect it.

Parts:

  • kubelet (sees a new Pod)
  • containerd (RunPodSandbox)
  • Network namespace (empty, only lo)
  • /etc/cni/net.d (which plugin)
  • /opt/cni/bin (plugin binaries)
  • Pod network (eth0 + IP + routes)

Connections:

  • kubelet to containerd: CRI
  • containerd to Network namespace: creates
  • containerd to /etc/cni/net.d: reads first file
  • containerd to /opt/cni/bin: runs ADD
  • /opt/cni/bin to Pod network: wires
PartWhereJob
kubeletevery nodeSees a Pod assigned to its node and asks the runtime for a Pod sandbox
Container runtime (containerd, CRI-O)every nodeCreates the Pod's network namespace and calls the CNI plugin
CNI configuration/etc/cni/net.d/Says which plugin to run and with which settings
CNI binaries/opt/cni/bin/The plugin programs: bridge, host-local, loopback, portmap, plus the one your network add-on brings
Network add-onPods in the cluster, usually a DaemonSetInstalls the two items above on each node and keeps routes, tunnels and policies up to date

How it works

The two directories

Two directories decide everythingholdsbeforeholdstype names a binarycalls/etc/cni/net.dconfiguration10-flannel.conflistfirst file: used99-other.confignored/opt/cni/binbinariesflannel, bridgenamed by typehost-localnamed by ipam.type
Two directories decide everything. The configuration directory says which plugin to use and how. The binary directory holds the programs. The runtime takes the first configuration file in alphabetical order.

Parts:

  • /etc/cni/net.d (configuration)
  • 10-flannel.conflist (first file: used)
  • 99-other.conf (ignored)
  • /opt/cni/bin (binaries)
  • flannel, bridge (named by type)
  • host-local (named by ipam.type)

Connections:

  • /etc/cni/net.d to 10-flannel.conflist: holds
  • 10-flannel.conflist to 99-other.conf: before
  • /opt/cni/bin to flannel, bridge: holds
  • 10-flannel.conflist to flannel, bridge: type names a binary
  • flannel, bridge to host-local: calls

/etc/cni/net.d/ holds configuration files ending in .conf, .conflist or .json. If there are several, the runtime sorts them by name and uses the first one only. That is why the files start with a number: 10-flannel.conflist, 10-calico.conflist, 05-cilium.conflist. A leftover file from an old plugin with a smaller number silently wins.

/opt/cni/bin/ holds one executable per plugin. The type field in the configuration is nothing more than the file name of a binary in this directory.

ls /etc/cni/net.d/
ls /opt/cni/bin/
10-flannel.conflist
bandwidth  bridge  dhcp  firewall  flannel  host-local  ipvlan  loopback  macvlan  portmap  ptp  static  tuning  vlan

The standard binaries (bridge, host-local, loopback, portmap and friends) come from the kubernetes-cni package that is installed together with the kubelet. flannel here was added by the add-on.

Reading a configuration file

{
  "cniVersion": "1.0.0",
  "name": "mynet",
  "plugins": [
    {
      "type": "bridge",
      "bridge": "cni0",
      "isGateway": true,
      "ipMasq": true,
      "ipam": {
        "type": "host-local",
        "subnet": "10.244.1.0/24",
        "routes": [{ "dst": "0.0.0.0/0" }]
      }
    },
    { "type": "portmap", "capabilities": { "portMappings": true } }
  ]
}
FieldMeaning
nameA label for this network
pluginsA chain. Each entry is run in order for every Pod (a .conf file holds a single plugin without this list)
typeThe binary to run from /opt/cni/bin
bridge, isGatewaySettings of that plugin: attach Pods to the Linux bridge cni0 and give the bridge an address so it can act as the Pods' gateway
ipMasqHide Pod addresses behind the node's address for traffic that leaves the Pod network
ipamWho picks the Pod's IP address. type is again a binary, here host-local, and subnet is the range it hands out on this node
portmapA second plugin in the chain that implements hostPort

The ipam section is the topic of the next lesson.

What happens when a Pod starts

  1. Scheduler picks a node
  2. kubelet asks the runtime for a sandbox
  3. Runtime creates the network namespace
  4. Runtime runs the plugin with ADD
  5. Plugin returns the IP
  6. kubelet reports it in status.podIP
From schedule to Pod IP
  1. The kubelet calls RunPodSandbox on the runtime through the CRI.
  2. The runtime creates a new network namespace for the Pod. All containers of the Pod will share it, which is why they share one IP address and can talk over localhost.
  3. The runtime reads the first file in /etc/cni/net.d and runs each plugin of the chain. It passes the settings on standard input and the facts about this Pod in environment variables: CNI_COMMAND=ADD, CNI_CONTAINERID, CNI_NETNS (the path of the namespace) and CNI_IFNAME=eth0.
  4. The main plugin creates a pair of virtual interfaces, moves one end into the Pod as eth0, attaches the other to the node's network, asks the IPAM plugin for an address, and sets the routes.
  5. The plugin prints the result as JSON. The runtime hands the address to the kubelet, which writes it to the Pod's status. That is the IP you see in kubectl get pods -o wide.

When the Pod is deleted, the runtime runs the same chain with CNI_COMMAND=DEL, and the address goes back to the pool.

How the plugin gets onto the node

How a network plugin gets onto every nodecreatesrunshostPathhostPaththenkubectl applyplugin manifestDaemonSetone Pod per nodePlugin PodhostNetwork/opt/cni/binbinary copied/etc/cni/net.dconfig writtenNode ReadyCoreDNS can start
How a network plugin gets onto every node. You apply one manifest. Its DaemonSet runs a Pod on each node that copies the binary and the configuration file onto the host. Only then does the node become Ready.

Parts:

  • kubectl apply (plugin manifest)
  • DaemonSet (one Pod per node)
  • Plugin Pod (hostNetwork)
  • /opt/cni/bin (binary copied)
  • /etc/cni/net.d (config written)
  • Node Ready (CoreDNS can start)

Connections:

  • kubectl apply to DaemonSet: creates
  • DaemonSet to Plugin Pod: runs
  • Plugin Pod to /etc/cni/net.d: hostPath
  • Plugin Pod to /opt/cni/bin: hostPath
  • /opt/cni/bin to Node Ready: then

You do not copy files by hand. A network add-on (Flannel, Calico, Cilium and others) comes as a manifest whose core is a DaemonSet. On each node its Pod:

  • runs with hostNetwork: true, so it does not need the network it is about to create;
  • mounts /opt/cni/bin and /etc/cni/net.d from the host with hostPath volumes, and an init container copies the binary and writes the configuration file there;
  • keeps running as the plugin's agent: it programs routes or tunnels to the other nodes, and with some plugins it enforces network policies.

The runtime watches the configuration directory. As soon as a valid file appears, it reports the network as ready, the kubelet sets the node condition Ready, and the Pods that were waiting, CoreDNS first of all, get scheduled and started.

Choosing a plugin

PluginHow Pods on different nodes reach each otherNetworkPolicyDefault Pod range in its manifest
FlannelVXLAN tunnel between nodesNo10.244.0.0/16
CalicoRouting (BGP) or tunnels (IP-in-IP, VXLAN)Yes192.168.0.0/16
CiliumeBPF, tunnels or native routingYes10.0.0.0/8 (cluster pool)
Weave NetIts own mesh between nodesYes10.32.0.0/12

Weave Net appears in many CKA courses, but the project is no longer maintained. The exam lets you pick, and a task that mentions network policies needs a plugin from the "Yes" rows.

Under the hood · Details worth knowing
  • Where the runtime is told the paths. In containerd they are in /etc/containerd/config.toml, in the CRI plugin's cni section, as bin_dir (or bin_dirs in newer versions) and conf_dir. The section name changed between containerd 1.x and 2.x, so search for cni instead of memorising it: containerd config dump | grep -A 6 'cni\]'. In CRI-O they are plugin_dirs and network_dir in /etc/crio/crio.conf.
  • The loopback interface. The runtime sets up lo in each Pod itself, using the loopback plugin. If that binary is missing, even Pod creation on a working network fails.
  • Pod range and node ranges. kubeadm init --pod-network-cidr=10.244.0.0/16 tells the controller manager to give each node a slice of that range (node.spec.podCIDR). Flannel reads that slice. Calico and Cilium manage their own pools and only need the range to match.
  • What the runtime status says. crictl info prints the runtime's view, including the condition NetworkReady and the loaded CNI configuration. It is the fastest proof of which file the runtime chose.
  • Changing plugins. Replacing one plugin with another on a live cluster means removing the old DaemonSet, its files in /etc/cni/net.d and its interfaces on each node, then recreating every Pod. It is not an exam task.

Using it

Example · Find out which plugin a cluster uses

Three places agree with each other. Start on any node:

ls /etc/cni/net.d/
cat /etc/cni/net.d/*.conflist | grep '"type"'
10-flannel.conflist
      "type": "flannel",
      "type": "portmap",

The first type is the main plugin. The binary exists:

ls /opt/cni/bin/ | grep flannel
flannel

And its agent runs on every node:

kubectl get daemonsets -A
NAMESPACE      NAME              DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
kube-flannel   kube-flannel-ds   3         3         3       3            3           <none>                   3d
kube-system    kube-proxy        3         3         3       3            3           kubernetes.io/os=linux   3d

Typical questions: "Which binary is run after a container and its namespace are created?" The value of type. "Where are the plugin binaries?" /opt/cni/bin.

Example · Install a network plugin on a new cluster

Task: "The cluster was created with kubeadm init --pod-network-cidr=10.244.0.0/16. Nodes are NotReady. Install a network plugin."

Confirm the cause before you install anything:

kubectl get nodes
kubectl describe node controlplane | grep -i -A 1 networkready
kubectl -n kube-system get pods | grep coredns
NAME           STATUS     ROLES           AGE   VERSION
controlplane   NotReady   control-plane   2m    v1.34.1
  Ready  False  ...  KubeletNotReady  container runtime network not ready: NetworkReady=false
  reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized
coredns-6f6b679f8f-4kq2n   0/1   Pending   0   2m
coredns-6f6b679f8f-x9wlb   0/1   Pending   0   2m

Check the Pod range the cluster was built with:

kubectl -n kube-system get cm kubeadm-config -o yaml | grep podSubnet
      podSubnet: 10.244.0.0/16

10.244.0.0/16 is Flannel's default, so its manifest fits without edits. The exam gives you the URL or the file:

kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl -n kube-flannel get pods -o wide
kubectl get nodes
NAME                    READY   STATUS    RESTARTS   AGE   IP             NODE
kube-flannel-ds-7xk2p   1/1     Running   0          25s   192.168.1.10   controlplane
NAME           STATUS   ROLES           AGE   VERSION
controlplane   Ready    control-plane   4m    v1.34.1

If the cluster's range differs from the manifest's default, download the manifest first and change the range in it (Flannel: Network in the kube-flannel-cfg ConfigMap; Calico: cidr in the Installation resource).

Example · A Pod stuck in ContainerCreating

kubectl get pods -o wide
kubectl describe pod web | tail -n 4
NAME   READY   STATUS              RESTARTS   AGE   IP       NODE
web    0/1     ContainerCreating   0          3m    <none>   node01
  Warning  FailedCreatePodSandBox  12s (x9 over 3m)  kubelet  Failed to create pod sandbox: rpc error:
  code = Unknown desc = failed to setup network for sandbox "5c1e...": plugin type="bridge" failed (add):
  failed to find plugin "bridge" in path [/opt/cni/bin]

The event names the stage (network setup), the plugin (bridge) and the place (/opt/cni/bin). Go to the node the Pod is on:

ssh node01
ls /opt/cni/bin/
ls /etc/cni/net.d/

An empty or incomplete binary directory means the standard plugins are gone: reinstall the kubernetes-cni package, or restart the add-on's Pod on that node so its init container copies the files again:

kubectl -n kube-flannel delete pod -l app=flannel --field-selector spec.nodeName=node01

The kubelet retries the sandbox by itself, so the Pod moves to Running without being recreated.

Tricks

Common mistakes

  • Looking for CNI flags on the kubelet. They were removed in 1.24. The runtime holds the paths.
  • Checking the control plane node when the stuck Pod is on a worker. The two directories exist on every node, and each node can be broken on its own.
  • Leaving two configuration files. Only the first by name is used, so an old file can override the new plugin.
  • Installing a plugin whose Pod range differs from the cluster's. Pods get addresses that the rest of the cluster does not route.
  • Picking Flannel for a task about network policies. The policies are accepted and never enforced.
  • Waiting for CoreDNS to become Ready before installing the plugin. It is the other way round.

Recap

  • Kubernetes states the network model. A CNI plugin implements it.
  • The container runtime, not the kubelet, runs the plugin with ADD when a Pod sandbox is created and DEL when it is removed.
  • /etc/cni/net.d holds the configuration (first file wins), /opt/cni/bin holds the binaries named by type.
  • Add-ons install themselves with a DaemonSet on the host network.
  • No plugin means NotReady nodes and Pending CoreDNS. A broken plugin means Pods stuck in ContainerCreating with FailedCreatePodSandBox events.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • New pods on one node never get past ContainerCreating. Follow the trail from the pod's events to the node's CNI configuration, and find out which plugin really wires the pods of this cluster.

    Fix · Level 2 · standard · target 15 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.