Skip to content
Opsfolk
Kubernetes

Kubernetes Networking Deep Dive: CNI, Overlays and eBPF

How Kubernetes Pod networking works: the network model, CNI plugins and IPAM, VXLAN and BGP between nodes, MTU, eBPF data planes and NetworkPolicy.

admin38 min read

Kubernetes Networking: CNI, overlays and eBPF. A Pod's packet, wrapped in VXLAN by flannel.1, crosses from worker-1 to worker-2 in a tunnel above a switch that only knows the nodes.
On this page

In the third part of this series, the kubelet asked the container runtime for a Pod sandbox, and one line of that story did a lot of work: the runtime "calls the CNI plugin, which gives the Pod its IP address". In the fourth, a Service rewrote a packet's destination to a Pod on another node, and the packet "is routed to the chosen Pod by the Pod network that the CNI plugin set up". Both times, the network itself was a black box. Pod web on worker-1 has the address 10.244.1.5, Pod api on worker-2 has 10.244.2.17, and a packet from one reaches the other. Yet the machines between them have never heard of 10.244.0.0/16.

This part of Kubernetes Internals: How a Cluster Really Works opens the box. We start with the rules Kubernetes sets for every cluster's network, and the Container Network Interface (CNI) that plugins implement to follow them. We follow a Pod onto the network, then a packet from one Pod to another: inside a node, through a bridge or a route, and across the cluster, by native routing or inside an overlay such as VXLAN, with the MTU it costs. Then we look at eBPF, which lets plugins such as Cilium and Calico replace iptables with programs in the kernel, and at NetworkPolicy, which only the plugin can enforce. At the end, as in every part, we turn it around: when two Pods cannot reach each other, where to look.

New to the series? Start with Kubelet and Container Runtimes, which ends where a Pod gets its network, and Kubernetes Services, DNS and Ingress Explained, which sends traffic across it.

What Is the Kubernetes Network Model?

Definition: The Kubernetes network model is the set of rules every cluster's Pod network must follow: each Pod gets its own IP address, unique in the cluster; every Pod can reach every other Pod at that address, on any node, without NAT; and the agents on a node, such as the kubelet, can reach every Pod on that node. Kubernetes states the rules, and a network plugin implements them.

The rules are short, and they rule out a lot. A Pod does not share its node's address or map ports onto it, as containers on a single Docker host did. A Pod sees its own address on its own interface, and that is the address every other Pod sees as the source of its packets. A program can register its own IP, or put it in a protocol, and others can connect to it. That is why a Pod behaves like a small machine on a flat network.

Three facts frame the rest of this article:

  • Kubernetes does not implement its network. No component of the control plane, and not the kubelet, creates an interface or a route for a Pod. Since Kubernetes 1.24 removed the kubelet's network flags along with the dockershim, the container runtime calls a CNI plugin, and that plugin does all of it. A cluster without one has nodes that stay NotReady.
  • The model says nothing about how. Pods on different nodes may be reached by routes the network knows, by packets wrapped in packets, or by programs in the kernel. Each plugin picks its own way, and the choice decides the MTU, what the network between the nodes must allow, and what a packet capture shows.
  • Pod IPs are not stable, and Services exist for that. The model gives a Pod an address for its life, not longer. Part 4 built Services, kube-proxy and DNS on top of it. This part is the layer underneath: how a packet to a Pod's IP gets there.

Four Problems, Four Answers

The Kubernetes documentation splits cluster networking into four problems, and this series has now met all of them:

Problem Solved by Where it is in the series
Container to container, in one Pod A shared network namespace, held by the pause container: they talk over localhost Part 3
Pod to Pod The CNI plugin: an address per Pod, and a way to every other Pod This part
Pod to Service A virtual IP, kube-proxy or an eBPF data plane, and DNS Part 4
Outside to Service NodePorts, load balancers, Ingress and the Gateway API Part 4

Three Address Ranges

A cluster uses three ranges of addresses. They must not overlap each other, nor the network the nodes are on:

Range Example Set by Used for
Node network 192.168.1.0/24 Your network, or your cloud's VPC The nodes' own interfaces
Pod CIDR 10.244.0.0/16 --cluster-cidr on kube-controller-manager, or the plugin's own setting Pod addresses, cut into a block per node
Service CIDR 10.96.0.0/12 --service-cluster-ip-range on the API server Virtual IPs, which never appear on an interface

The examples in this article use the ranges above. worker-1, at 192.168.1.11, owns the Pod block 10.244.1.0/24 and runs Pod web at 10.244.1.5. worker-2, at 192.168.1.12, owns 10.244.2.0/24 and runs Pod api at 10.244.2.17.

How a Pod Joins the Network, at a Glance

How a new Pod gets its interface and its address, step by step:

  1. Sandbox: the kubelet asks the runtime, containerd or CRI-O, for a Pod sandbox with RunPodSandbox, as in part 3.
  2. Namespace: the runtime creates a new network namespace and starts the pause container in it. The namespace holds nothing but a loopback interface.
  3. Config: the runtime reads the network configuration in /etc/cni/net.d/, the first file in lexical order, and finds the plugin binaries it names in /opt/cni/bin/.
  4. ADD: it runs the first plugin binary with CNI_COMMAND=ADD, the container's ID, the namespace's path and CNI_IFNAME=eth0 in its environment, and the configuration on its standard input.
  5. Interface: the plugin creates a veth pair, two virtual interfaces joined like the ends of a cable. It moves one end into the Pod's namespace as eth0, and leaves the other on the host.
  6. Address: it calls its IPAM plugin, which picks a free address from the node's block, 10.244.1.5, and configures it on eth0 with a default route.
  7. Chain: the other plugins in the list run next, each given the result so far: port mappings, bandwidth limits, tuning.
  8. Result: the last plugin prints a JSON result with the interfaces, addresses and routes it made. The runtime returns the IP to the kubelet, which writes it into the Pod's status.podIP.
  9. Reach: the plugin's agent, a DaemonSet on every node, has already made 10.244.1.0/24 reachable from the other nodes, by routes, a tunnel or eBPF programs. Nothing more is done per Pod.

When the Pod is deleted, the runtime runs the same plugins with DEL, in reverse order, and the address goes back to the pool.

The kubelet asks the runtime for a sandbox; the runtime creates the Pod's namespace and runs the CNI plugin with ADD, which wires eth0 to the node with an address from IPAM.

Notice what the kubelet does not do: it runs no plugin and creates no interface. It learns the Pod's address from the runtime's answer, like any other field of the sandbox.

CNI: The Container Network Interface

CNI is a small specification and a set of reference plugins, maintained by the CNCF project containernetworking. It is not specific to Kubernetes: Podman and other container tools use it too. The specification is at version 1.1, and it defines a plugin as nothing more than an executable that follows a contract:

  • Input: the operation and the attachment's parameters in environment variables (CNI_COMMAND, CNI_CONTAINERID, CNI_NETNS, CNI_IFNAME, CNI_PATH), and the network configuration as JSON on standard input.
  • Output: a JSON result on standard output, or a JSON error and a non-zero exit code.
  • Verbs:
Verb Asks the plugin to
ADD Attach a container to the network: create its interface, give it an address
DEL Detach it and release everything ADD took. It must succeed even when parts are already gone
CHECK Verify that an attachment is still as ADD left it
VERSION Report which versions of the specification it supports
STATUS Say whether it is ready to accept ADD, new in 1.1
GC Clean up attachments the runtime no longer knows about, such as addresses leaked by a missed DEL, new in 1.1

A plugin runs for a few milliseconds and exits. It keeps no daemon, no socket and no memory between calls: whatever it must remember, such as which addresses are taken, it writes to disk or asks an agent for.

A Network Configuration

A runtime's configuration directory holds a network configuration list, a .conflist, which names a chain of plugins. Here is one built from the reference plugins, for a node that owns 10.244.1.0/24:

json
{
  "cniVersion": "1.1.0",
  "name": "podnet",
  "plugins": [
    {
      "type": "bridge",
      "bridge": "cni0",
      "isGateway": true,
      "ipMasq": true,
      "mtu": 1450,
      "ipam": {
        "type": "host-local",
        "ranges": [[{ "subnet": "10.244.1.0/24" }]],
        "routes": [{ "dst": "0.0.0.0/0" }]
      }
    },
    {
      "type": "portmap",
      "capabilities": { "portMappings": true }
    }
  ]
}

type names the binary: /opt/cni/bin/bridge, then /opt/cni/bin/portmap. Plugins come in three kinds:

  • Main plugins create the interface: bridge, ptp (a veth pair with a route and no bridge), macvlan, ipvlan, host-device, and the plugins of the big projects, calico, cilium-cni and flannel.
  • IPAM plugins only choose addresses, and a main plugin calls them: host-local (a range, with its allocations kept on disk), dhcp, static, or a project's own, such as calico-ipam.
  • Chained plugins adjust what the earlier ones made: portmap for a container's hostPort, bandwidth for the kubernetes.io/ingress-bandwidth and egress-bandwidth annotations, tuning for sysctls, and plugins that add policy to another plugin's network, such as Cilium in chaining mode.

Run ls /etc/cni/net.d/ on a node, and you see which plugin the cluster uses. Most of the files there are generated: Flannel's plugin, for example, builds a bridge configuration much like the one above, with the node's block and MTU, and runs the bridge plugin with it.

Who Installs the Plugin

The plugin's binary is usually put on each node by the plugin's own DaemonSet. Its Pod starts with an init container that copies the binaries into /opt/cni/bin/ and writes the configuration into /etc/cni/net.d/, through hostPath volumes. Then its main container keeps running as the plugin's agent: flanneld, calico-node or cilium-agent. The agent watches the API server for nodes, Pods and policies, and keeps the node's routes, tunnels and rules up to date. The binary attaches each Pod; the agent connects the nodes.

That split explains a puzzle at startup. The plugin's own Pods must run before any other Pod can get an address, so they use hostNetwork: true: they share the node's network namespace and need no CNI. Until the configuration file appears, the runtime reports its network as not ready, the kubelet passes that on, and the node stays NotReady:

container runtime network not ready: NetworkReady=false
reason:NetworkPluginNotReady message:Network plugin returns error: cni plugin not initialized

On a new kubeadm cluster, that is the normal state between kubeadm init and installing a plugin. CoreDNS's Pods stay Pending in the same window, since they are not on the host network.

More Than One Interface: Multus

A runtime runs one network configuration, so a Pod gets one interface besides lo. Multus is a meta-plugin that is installed as the cluster's plugin and calls others: the default network for eth0, plus the extra networks a Pod asks for in an annotation, each described by a NetworkAttachmentDefinition. It is how telecom and storage workloads get a second interface on a dedicated network, such as SR-IOV or a macvlan on a storage VLAN. The Kubernetes network model, and everything else in this article, only concerns eth0.

Inside a Node: veth Pairs, Bridges and Routes

From inside the Pod, the network is one interface and a couple of routes. On a node with Flannel:

bash
kubectl exec web -- ip -brief addr show eth0
kubectl exec web -- ip route
eth0@if7         UP             10.244.1.5/24
default via 10.244.1.1 dev eth0
10.244.1.0/24 dev eth0 proto kernel scope link src 10.244.1.5

eth0@if7 says that eth0 is one end of a veth pair, and that its peer is interface 7 of another namespace: the host's. On the host, ip link shows that peer, veth3f2a1b0c@if2, and what it is attached to. There are two common designs.

A bridge. The bridge plugin, which Flannel uses, attaches every host-side veth to a Linux bridge, cni0, and gives the bridge the gateway address, 10.244.1.1. The node's Pods are then on one layer-2 segment. A packet from web to another Pod on worker-1 is switched by the bridge from one veth to the other, without the host's routing table. A packet for anything else goes to the gateway, the bridge's own address, and from there the host routes it.

A bridge has one trap for Services. Traffic switched across a bridge skips the host's iptables unless the kernel module br_netfilter is loaded and net.bridge.bridge-nf-call-iptables is 1. Without them, kube-proxy's rules never see a packet from a Pod to a Service whose chosen backend sits on the same bridge. The Kubernetes documentation asks for this setting with any plugin that uses a bridge.

Routes only. Calico, and Cilium in most of its modes, use no bridge. Each Pod's veth is a point-to-point link, and the host has a route per Pod:

# Inside the Pod (Calico)
default via 169.254.1.1 dev eth0
169.254.1.1 dev eth0 scope link

# On the node
10.244.1.5 dev cali1a2b3c4d5e6 scope link
10.244.1.6 dev cali7f8e9d0c1b2 scope link

The Pod's gateway, 169.254.1.1, exists nowhere. The Pod asks for its MAC address over ARP, and the host-side veth answers with its own, by proxy ARP, so every packet the Pod sends lands in the host's routing table. Traffic between two Pods on the same node is routed, not switched, and the host's rules, policy included, see every packet. Cilium goes a step further: its eBPF programs on the veth can hand a packet straight to the other Pod's interface, past most of the host's stack.

To look at the network of a Pod whose image has no ip command, enter its namespace from the node:

bash
# On the node: find the Pod's sandbox, then its network namespace
POD=$(sudo crictl pods --name web -q)
NETNS=$(sudo crictl inspectp "$POD" | jq -r '.info.runtimeSpec.linux.namespaces[] | select(.type=="network").path')
sudo nsenter --net="$NETNS" ip addr
sudo nsenter --net="$NETNS" ip route

Every tool on the node then runs in the Pod's network: ss, tcpdump and ping, with the Pod's addresses and routes.

Between Nodes: Native Routing or an Overlay

A packet from web, 10.244.1.5, to api, 10.244.2.17, leaves web's veth and reaches worker-1's routing table. Now worker-1 has to send it to worker-2. The trouble is that the network between the nodes, the underlay, knows 192.168.1.11 and 192.168.1.12 but not 10.244.2.0/24. A router there that receives a packet for 10.244.2.17 drops it, or sends it to its default gateway, which drops it. There are two ways out: teach the underlay the Pod routes, or hide the Pod addresses from it.

Native Routing: Teach the Network

With native routing, packets cross the network with their Pod addresses unchanged, and something tells the network where each node's block is.

  • Routes on each node. When the nodes share one layer-2 segment, each node can route every other node's block straight to that node: 10.244.2.0/24 via 192.168.1.12. That is Flannel's host-gw backend, and Cilium's native routing with autoDirectNodeRoutes. It does not work across a router, which knows none of these routes.
  • BGP. Calico runs a BGP daemon, BIRD, inside calico-node on every node, and announces the node's blocks over TCP port 179. By default every node peers with every other, a full mesh the Calico documentation suggests for clusters of up to about a hundred nodes. Beyond that, nodes peer with route reflectors, or with the top-of-rack routers, so that the physical network itself learns every Pod block. ip route then shows 10.244.2.0/26 via 192.168.1.12 dev eth0 proto bird.
  • The cloud's routes. A cloud can be given the routes instead: an entry per node's block in the VPC's route table, as GKE's older routes-based clusters and AKS's kubenet do. Route tables have a size limit, which caps the number of nodes.
  • Pod addresses from the cloud's network. Or the Pods' addresses can be the cloud's own. The AWS VPC CNI gives each Pod a secondary address of an Elastic Network Interface (ENI) on its node, and GKE's VPC-native clusters give each node an alias IP range. The VPC routes Pod addresses like any other, and a Pod can be reached from outside the cluster, inside the VPC.

Native routing costs nothing per packet, keeps the whole MTU, and shows real addresses to every firewall and packet capture on the way. It needs a network that cooperates: routers to peer with, or a cloud that routes Pod addresses. On AWS, a node that forwards packets for addresses that are not its own also needs the instance's source/destination check turned off.

Overlays: Hide the Pod Addresses

An overlay needs nothing from the underlay but connectivity between the nodes. Each node wraps a packet for a Pod on another node in a new packet, addressed from its own IP to the other node's, and the other node unwraps it. The underlay only ever sees traffic between nodes. This is encapsulation, and three formats are common:

Format Wraps the Pod's packet in Bytes added (IPv4) Must be allowed between nodes Used by
VXLAN A UDP datagram, with an 8-byte VXLAN header and the Pod's Ethernet frame 50 UDP 4789, or 8472, the Linux kernel's old default, which Flannel and Cilium use Flannel (its default), Calico, Cilium (its default) and most others
Geneve A UDP datagram, with an 8-byte header and optional fields 50, plus options UDP 6081 Cilium, Antrea (its default), OVN-Kubernetes
IP-in-IP A second IPv4 header, and nothing else 20 IP protocol 4, which some firewalls drop and Azure's network does not carry Calico (the default in its manifest install)

VXLAN's 50 bytes are the outer IPv4 header (20), the UDP header (8), the VXLAN header (8) and the Pod's own Ethernet header (14), which VXLAN carries because it tunnels layer-2 frames. Over an IPv6 underlay, VXLAN adds 70.

Calico can also mix the two. With vxlanMode: CrossSubnet or ipipMode: CrossSubnet on its IP pool, a packet is wrapped only when it crosses from one subnet to another, and routed natively between nodes on the same subnet: an overlay only where the network would drop the packet.

Following a Packet Through VXLAN

Here is what Flannel's agent, flanneld, sets up on worker-1 so that 10.244.2.17 can be reached. Each node has a VXLAN device, flannel.1, and for every other node the agent writes three entries, from the addresses the other agents published as annotations on their Node objects:

bash
ip route show | grep flannel.1
ip neigh show dev flannel.1
bridge fdb show dev flannel.1
10.244.2.0/24 via 10.244.2.0 dev flannel.1 onlink
10.244.2.0 dev flannel.1 lladdr 6e:2b:91:0c:4a:17 PERMANENT
6e:2b:91:0c:4a:17 dev flannel.1 dst 192.168.1.12 self permanent

Read them in order, as the kernel does for the packet to 10.244.2.17:

  1. Route: 10.244.2.0/24 goes through flannel.1, to the next hop 10.244.2.0, which stands for worker-2's VXLAN device.
  2. Neighbour: that next hop's MAC address is 6e:2b:91:0c:4a:17, the MAC of flannel.1 on worker-2, written in advance so the kernel never has to ask for it with ARP.
  3. Forwarding database: frames for that MAC go to the underlay address 192.168.1.12.

So the kernel puts the Pod's untouched IP packet in an Ethernet frame addressed to worker-2's VXLAN device, adds a VXLAN header, and sends the result as a UDP datagram from 192.168.1.11 to 192.168.1.12, port 8472. On worker-2, the kernel hands UDP 8472 to its own flannel.1, which strips the outer headers. What is left is the original packet, from 10.244.1.5 to 10.244.2.17, which worker-2 routes to cni0 and api's veth.

web on worker-1 sends to api on worker-2; flannel.1 wraps the packet in VXLAN and UDP 8472 to the other node, which unwraps it, so only node addresses cross the network.

ip -d link show flannel.1 shows the device's settings, vxlan id 1 local 192.168.1.11 dev eth0 dstport 8472 nolearning, and an MTU 50 bytes below the node's. A capture on the node's interface shows the outer packets: tcpdump -ni eth0 udp port 8472. tcpdump decodes the inner packet by itself on VXLAN's standard port, 4789; on 8472, add -T vxlan.

MTU: The Overlay Tax

Every byte an overlay adds comes out of the Pod's packets. The network between the nodes has an MTU, the largest packet it carries without fragmenting it: usually 1500 bytes, and up to 9001 on some cloud networks. A wrapped packet must fit in it, so the Pod's own interface must be smaller by the overhead: 1450 for VXLAN on a 1500-byte network, 1480 for IP-in-IP. Plugins detect the node's MTU and subtract their overhead, which is why eth0 in a Pod on an overlay often says 1450.

When it goes wrong, it goes wrong in a way that is hard to recognise. A Pod whose MTU is too large sends small packets fine, so the TCP handshake, a ping, a short HTTP request and DNS all work. Its first full-size packet does not fit. It is fragmented, at a cost in throughput, or, when it carries the Don't Fragment bit, dropped, and the sender is meant to learn the right size from an ICMP "fragmentation needed" message. Where a firewall drops those messages, the sender never learns, and the connection hangs as soon as one side sends a large packet: a page that loads its headers and never its body, a git clone that stalls, a TLS handshake that stops at the server's certificate.

The canvas below sends one full-size packet from web to api. Pick an encapsulation, and lower the Pod's MTU until the packet fits on the 1500-byte link between the nodes.

Two nodes joined by a link that carries packets of up to 1500 bytes. A full-size packet goes from the Pod web on worker-1 to api on worker-2, and a bar under the nodes shows its bytes against the link's limit. An overlay wraps the packet in headers on worker-1 and strips them on worker-2: IP-in-IP adds 20 bytes, VXLAN and Geneve 50, WireGuard 60, and native routing nothing. With a Pod MTU of 1500, every overlay makes the packet too large for the link. It fits at 1480 for IP-in-IP, 1450 for VXLAN and Geneve, and 1440 for WireGuard. The controls set the encapsulation and the Pod's MTU.

A packet crossing an overlay

With VXLAN and a Pod MTU of 1500, the wrapped packet is 1550 bytes and does not fit. VXLAN and Geneve need a Pod MTU of 1450 at most, IP-in-IP 1480 and WireGuard 1440. With native routing nothing is added, and a Pod can use the network's full 1500. On a network with jumbo frames the overhead hardly matters: 9001 less 50 is still a very large packet.

To check the MTU where it matters:

bash
kubectl exec web -- cat /sys/class/net/eth0/mtu
ip link show flannel.1 | grep -o 'mtu [0-9]*'        # on the node
kubectl exec web -- ping -c 3 -M do -s 1422 10.244.2.17

The last line sends 1450-byte pings, 1422 bytes of data plus 28 of headers, with fragmentation forbidden. If they fail while a small ping succeeds, the path between the two Pods is smaller than the Pod thinks. The ping in the Pod has to support -M do, as iputils' does; BusyBox's does not.

Encrypting Pod Traffic

Pod traffic between nodes is not encrypted by default, overlay or not: a VXLAN packet carries the Pod's packet as it is. Calico and Cilium can encrypt it with WireGuard, and Cilium with IPsec as well. Each node gets a key pair, publishes its public key in the cluster, and sends Pod traffic for other nodes through a WireGuard interface: UDP 51820 for Calico, 51871 for Cilium. WireGuard adds 60 bytes over IPv4 and 80 over IPv6, and sets the Don't Fragment bit on what it sends, so a wrong MTU drops packets rather than fragmenting them. Turning it on is a change to the plugin's configuration, wireguardEnabled: true in Calico's FelixConfiguration or encryption.enabled: true with encryption.type: wireguard in Cilium's Helm values, and none to the Pods.

IP Address Management

A Pod's address comes from its plugin's IPAM, which has two questions to answer: which block of addresses belongs to each node, and which address in the block is free.

The block per node. With --allocate-node-cidrs=true, kube-controller-manager's node IPAM controller cuts --cluster-cidr into blocks of --node-cidr-mask-size, a /24 by default, and writes one into each Node's spec.podCIDR when the node joins. kubeadm turns this on when you pass --pod-network-cidr. A /24 holds 256 addresses, comfortably more than the kubelet's default limit of 110 Pods per node (maxPods), and a /16 cut into /24s is enough for 256 nodes. Flannel, and Cilium in its kubernetes IPAM mode, read the block from the Node:

bash
kubectl get nodes -o custom-columns=NODE:.metadata.name,PODCIDR:.spec.podCIDR

Not every plugin uses it. Calico splits its IP pool into blocks of 64 addresses, /26s, and gives a node another block when its last one fills, so a busy node holds several blocks and a quiet one a single block. Cilium defaults to its cluster-pool mode, in which its operator hands each node a block from Cilium's own pool and records it in the node's CiliumNode object. On such clusters, spec.podCIDR can be empty, or ignored.

The address in the block. host-local records each address it gives out as a file named after the address, under /var/lib/cni/networks/<network name>/, holding the container's ID. A sandbox destroyed without a DEL, after a node crash or a runtime bug, leaves its file behind, and the address stays taken. Enough leaks fill the block, and every new Pod on the node fails with failed to allocate for range 0: no IP addresses available in range set. Files whose container no longer exists can be removed by hand; CNI 1.1's GC verb is the specification's answer, so that a runtime can ask its plugins to clean up what it no longer knows about.

When the cloud gives the addresses. With the AWS VPC CNI, a Pod's address is a secondary IP of an ENI, and each instance type allows only so many ENIs, and so many addresses per ENI. The most Pods a node can run is the number of ENIs × (addresses per ENI − 1) + 2: an m5.large, with 3 ENIs of 10 addresses, takes 29 Pods, however much CPU and memory it has left. Prefix delegation gives the ENIs /28 prefixes, 16 addresses each, instead of single addresses, and lifts that limit; the EKS documentation then suggests capping Pods at 110 on instances with fewer than 30 vCPUs, and 250 above. The same design means a busy cluster can run out of addresses in its subnet.

Dual-stack. A cluster can give every Pod both an IPv4 and an IPv6 address, a feature stable since Kubernetes 1.23. Both ranges go in --cluster-cidr, separated by a comma, the plugin must support it, and status.podIPs lists both addresses.

Flannel, Calico, Cilium and the Cloud Plugins Compared

Every plugin implements the same model and the same CNI contract. They differ in how they reach other nodes, what else they do, and how much of the node's networking they take over:

Plugin Data plane Between nodes NetworkPolicy Replaces kube-proxy Encryption
Flannel Linux bridge and routes VXLAN (default), host-gw, WireGuard Not on its own; recent releases can add SIG Network's kube-network-policies controller No WireGuard backend
Calico iptables, nftables or eBPF BGP, IP-in-IP or VXLAN, or encapsulation across subnets only Yes, plus its own namespaced and cluster-wide policies In its eBPF data plane WireGuard
Cilium eBPF VXLAN (default) or Geneve, or native routing, with BGP if needed Yes, plus identity-based policies up to layer 7 Yes WireGuard or IPsec
AWS VPC CNI Linux routes; Pods have VPC addresses Native, through the VPC With its network policy agent, which uses eBPF No Left to the VPC
Antrea Open vSwitch Geneve (default), VXLAN, or native routing Yes, plus its own tiered policies Yes, with AntreaProxy WireGuard or IPsec
OVN-Kubernetes Open vSwitch, programmed by OVN Geneve Yes, plus cluster-wide policies Yes, through OVN's load balancers IPsec

A managed cluster often decides for you, and usually picks one of these. GKE's Dataplane V2 and Azure CNI Powered by Cilium are Cilium, EKS uses the AWS VPC CNI by default, and OpenShift uses OVN-Kubernetes. k3s ships Flannel, with a NetworkPolicy controller of its own built in.

How to choose, on a cluster where the choice is yours:

  • For a lab, an edge device or a small cluster, Flannel is the fewest moving parts, as long as you add policy enforcement or do not need it.
  • For a cluster whose network team speaks BGP, Calico peers with the routers, and its Pods become first-class addresses on the network.
  • For a large cluster, or to drop kube-proxy, write policies by identity and see every flow, Cilium is the usual answer today, at the price of a recent kernel and new tools to learn.
  • In a cloud, start with the provider's plugin or its Cilium-based option. Its Pods get routable addresses, its load balancers know them, and its support team knows it.

Whatever you choose, change it only with a plan: replacing a cluster's plugin changes every Pod's network at once, and usually means draining every node, or building a new cluster.

eBPF: Programming the Kernel's Data Path

Most of the plugins above take the kernel's own machinery, bridges, routes and iptables, and configure it. eBPF lets a plugin add its own code to the kernel instead. An eBPF program is a small function, compiled to bytecode, that a process loads into the kernel and attaches to a hook. The kernel's verifier checks it before it can run: it must terminate, touch only the memory it is allowed to, and call only the kernel helpers its hook allows. The kernel then compiles it to machine code and runs it at that hook, for every packet or event. A program keeps its state in eBPF maps, hash tables and arrays that the program and a process in user space can both read and write.

A networking plugin built on eBPF splits its work the way every agent in this series does: a process in user space watches the API server, and the kernel does the work for each packet.

  • The agent, cilium-agent or calico-node, watches Pods, Services, EndpointSlices and NetworkPolicies, loads the programs, and writes what they need into maps: which backends a Service has, which peers a Pod may talk to.
  • The programs look those maps up on every packet or connection: one hash lookup finds a Service's backends however many Services exist, where kube-proxy's iptables rules are walked one by one.

Programs attach at several points of the network path:

Hook Runs Used for
XDP In the network driver, on a received packet, before the kernel allocates its buffer Dropping or forwarding at the earliest point: NodePort and load balancer acceleration, DDoS filtering
tc, and tcx since Linux 6.6 On an interface's ingress and egress, with the packet's full buffer The main data path: Service translation, policy, routing and encapsulation, on each Pod's veth and the node's interfaces
Socket and cgroup hooks When a process calls connect() or sendmsg() Balancing a Service once per connection, before any packet exists
kprobes and tracepoints On kernel functions and events Observability: following packets and drops without changing them

eBPF programs run at socket hooks, at tc on each interface and at XDP in the driver, around netfilter where kube-proxy's rules live; an agent writes the maps they read.

Socket-Level Load Balancing

Part 4 showed kube-proxy's way: the first packet of a connection to 10.96.0.20:80 walks the KUBE-SERVICES chain, a rule picks a Pod, DNAT rewrites the destination, and conntrack rewrites every later packet, both ways. Cilium's kube-proxy replacement and Calico's eBPF data plane do it earlier. When a Pod calls connect() to 10.96.0.20:80, a program on the socket hook looks the Service up in a map, picks a backend, and changes the address the socket connects to. The connection is opened straight to 10.244.2.17:8080. No packet is ever addressed to the ClusterIP, so there is nothing to translate on the way and nothing to translate back, and a hook on getpeername() still tells the application it is connected to the Service.

Traffic from outside the cluster has no socket on the node, so NodePort and LoadBalancer traffic is translated by programs at tc, or at XDP, on the node's interface. Cilium can also answer it with direct server return, so that the reply goes from the Pod's node straight to the client.

On a cluster where Cilium replaces kube-proxy, there is no kube-proxy DaemonSet, and the Services are in maps, not in chains:

bash
kubectl -n kube-system exec ds/cilium -- cilium-dbg status | grep KubeProxyReplacement
kubectl -n kube-system exec ds/cilium -- cilium-dbg service list

Identity-Based Policy

iptables matches addresses, and Pod addresses change all the time: every new Pod is a rule to add on every node. Cilium matches identities instead. Every distinct set of security-relevant labels, such as app=api in the namespace shop, gets a numeric identity, shared by all the Pods that carry those labels. A Pod's policy becomes a map of the identities and ports it may talk to. A new replica of api takes the identity its labels already have, and nothing changes anywhere else.

The receiving node has to know a packet's source identity, not only its address. In tunnel mode, Cilium carries the identity in the VXLAN or Geneve header itself, so the receiving node applies its policy without a lookup. With native routing, it looks the identity up from the source address.

Seeing Every Flow: Hubble

Because the programs see every packet and every policy decision, they can report them. Hubble, part of Cilium, collects those events from every node, with the Pods, namespaces and labels at both ends, and the verdict. With Hubble turned on:

bash
hubble observe --namespace shop --verdict DROPPED
hubble observe --pod shop/api --protocol tcp --port 5432

A drop shows up with the reason and the policy behind it, where with iptables you would count packets on a rule. Calico has a flow view too, Calico Whisker, since its 3.30 release.

What It Costs

  • A recent kernel. Cilium and Calico's eBPF data plane both ask for Linux 5.10 or later today, or a RHEL 8 kernel with the backports, and some features need newer ones. Managed node images have it; an old fleet of servers may not.
  • New tools. iptables-save shows almost nothing useful on such a node. The state is in maps, read with the plugin's own tools (cilium-dbg, calico-node -bpf) or with bpftool: bpftool net show lists the programs attached to each interface.
  • One owner for the data path. Software that relies on kube-proxy's chains, or on seeing Service addresses in packets, sees a different picture. Run the plugin's connectivity checks after any change: cilium connectivity test deploys test Pods and checks Pod, Service, policy and outside traffic.

NetworkPolicy: Enforced by the CNI Plugin

A NetworkPolicy is a Kubernetes object, stored and validated by the API server like any other, and nothing in Kubernetes enforces it. The network plugin's agent watches policies, Pods and namespaces, and turns them into iptables rules, nftables sets or eBPF maps on each node. A cluster whose plugin does not enforce policies, such as Flannel on its own, accepts every NetworkPolicy you create and blocks nothing.

The rules of the API, which every plugin implements the same way:

  • A Pod with no policy is open. It can send to and receive from anything, which is the network model's default.
  • A policy isolates the Pods it selects. Once any policy in a namespace selects a Pod in its podSelector, with Ingress in its policyTypes, that Pod only accepts what some policy allows. The same holds for Egress and what the Pod sends.
  • Policies only allow, and they add up. There is no deny rule. What a Pod may do is the union of what all its policies allow.
  • Both ends must agree. A connection from api to db needs api's egress to allow it, if api is isolated for egress, and db's ingress to allow it, if db is isolated for ingress.
  • Replies are allowed. A policy decides who may open a connection. Packets that answer an allowed connection always pass.
  • A Pod's own node is always allowed, so the kubelet's probes keep working whatever the policies say.

Here are policies for the shop from part 4, with api and db in the namespace shop, and frontend in the namespace web. First, nothing in shop sends or receives anything:

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny
  namespace: shop
spec:
  podSelector: {}          # every Pod in the namespace
  policyTypes: [Ingress, Egress]

Then each Pod gets what it needs, at both ends of each connection:

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api
  namespace: shop
spec:
  podSelector:
    matchLabels: {app: api}
  policyTypes: [Ingress, Egress]
  ingress:
  - from:
    - namespaceSelector:
        matchLabels: {kubernetes.io/metadata.name: web}
      podSelector:
        matchLabels: {app: frontend}
    ports:
    - {protocol: TCP, port: 8080}
  egress:
  - to:
    - podSelector:
        matchLabels: {app: db}
    ports:
    - {protocol: TCP, port: 5432}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: db
  namespace: shop
spec:
  podSelector:
    matchLabels: {app: db}
  policyTypes: [Ingress]
  ingress:
  - from:
    - podSelector:
        matchLabels: {app: api}
    ports:
    - {protocol: TCP, port: 5432}

Look at the from of the api policy: namespaceSelector and podSelector are in one item of the list, so both must match: Pods labelled app: frontend in the namespace web. Put a dash before podSelector, and they become two items, either of which is enough: any Pod in web, or any frontend Pod in shop. It is the most common mistake in NetworkPolicy, and the two versions of the YAML look almost the same.

One thing is still missing, and the canvas below shows what. It replays four connections in a loop against the policies above. Switch policies on and off, and see where each connection stops.

Four Pods in three namespaces: frontend in web, api and db in shop, and CoreDNS in kube-system. Four connections play in turn: frontend to api on port 8080, api to CoreDNS on 53, api to db on 5432 and frontend to db on 5432. With the defaults, frontend reaches api and api reaches db, api's DNS lookup is dropped as it leaves api, and frontend to db is dropped at db. Allow DNS lets the lookup through. Without the default deny, api's own policy still holds its lookup back. With no policy at all, all four get through. The switches turn each set of policies on or off.

Which connections a NetworkPolicy allows

With the defaults, frontend reaches api and api reaches db, but api's DNS lookups are dropped as they leave it: default-deny isolated it for egress, and its own policy allows only db. In a real Pod that failure comes first, since api cannot resolve db and never gets as far as connecting. Turn on Allow DNS to add the policy below. frontend → db is dropped whenever db is isolated, at db's end, since no policy lets it in. And turning off the default deny does not open api's egress: the api policy lists Egress itself, so it isolates api on its own. Try Allow DNS with the other two off, too: on its own it isolates every Pod in shop for egress, and api → db stops.

yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-dns
  namespace: shop
spec:
  podSelector: {}
  policyTypes: [Egress]
  egress:
  - to:
    - namespaceSelector:
        matchLabels: {kubernetes.io/metadata.name: kube-system}
      podSelector:
        matchLabels: {k8s-app: kube-dns}
    ports:
    - {protocol: UDP, port: 53}
    - {protocol: TCP, port: 53}

A policy selects Pods by their labels and ports, not Services: name CoreDNS's Pods and port 53, not the kube-dns Service. An ipBlock is meant for addresses outside the cluster, and a ClusterIP in one does not do what it seems, since most plugins check a packet after it was translated to a Pod's address.

The NetworkPolicy API is namespaced, and written by each namespace's owner. Rules an administrator sets for the whole cluster, such as a real deny of the cloud's metadata address, come from the plugins' own objects, Calico's GlobalNetworkPolicy or Cilium's CiliumClusterwideNetworkPolicy, or from SIG Network's ClusterNetworkPolicy, an alpha CRD that replaced the earlier AdminNetworkPolicy and BaselineAdminNetworkPolicy. Check which of these your plugin supports before you rely on one.

Why Pods Cannot Reach Each Other

When traffic between Pods fails, what works and what does not usually points at one layer:

Symptom Likely cause Where to look
Nodes NotReady, cni plugin not initialized No plugin installed, or its DaemonSet is failing /etc/cni/net.d/ on the node; the plugin's Pods in kube-system
Pod stuck in ContainerCreating with FailedCreatePodSandBox The CNI ADD failed: the plugin's agent is down, or its configuration is wrong The event's message, then the agent's logs on that node (part 3)
no IP addresses available in range set The node's block is full, or leaked by sandboxes that missed their DEL /var/lib/cni/networks/ on the node; the plugin's IPAM tools
Pods on the same node talk, across nodes they do not The overlay or the routes between nodes: blocked UDP 8472, 4789 or 6081, IP protocol 4, BGP on TCP 179, or a cloud's source/destination check Firewalls and security groups; tcpdump on the tunnel port; the plugin's node status
Small requests work, large responses hang The MTU: the Pod's MTU plus the overlay's overhead is more than the path between the nodes ping -M do -s with growing sizes; the Pod's and the tunnel device's MTU
One node's Pods unreachable, the rest fine That node's agent is down, or its routes or tunnel are stale The agent's Pod and logs on that node; its routes
Connections to one app time out, others work A NetworkPolicy drops them, at the source's egress or the destination's ingress kubectl get networkpolicy -A; Hubble or the plugin's flow logs
DNS fails in one namespace only A default-deny egress policy without a DNS rule The policies in that namespace; allow UDP and TCP 53 to CoreDNS
Pod to Pod works, Pod to Service does not Services, not the Pod network Part 4's debugging steps

Debugging the Pod Network, Step by Step

1. Is the plugin installed and running on the node?

bash
kubectl get nodes -o wide
kubectl -n kube-system get pods -o wide | grep -E 'flannel|calico|cilium'
ls /etc/cni/net.d/ /opt/cni/bin/          # on the node

Look for the plugin's Pod on the very node that fails, not only at the DaemonSet's totals. One crashing agent breaks one node. Some installs put the plugin in its own namespace, such as kube-flannel or calico-system.

2. Does each node have its block, and are they all different?

bash
kubectl get nodes -o custom-columns=NODE:.metadata.name,PODCIDR:.spec.podCIDR
kubectl get pods -A -o wide --field-selector spec.nodeName=worker-2

Pod addresses should fall inside their node's block. With Calico or Cilium's cluster-pool, read their own objects instead: calicoctl ipam show --show-blocks, or kubectl get ciliumnodes.

3. What does the Pod see?

bash
kubectl exec web -- ip addr show eth0
kubectl exec web -- ip route

An eth0 with the expected address, MTU and default route rules out the ADD. Without those tools in the image, use nsenter from the node, as shown earlier, or kubectl debug with an image that has them.

4. Can it reach a Pod on another node, by IP?

bash
kubectl exec web -- ping -c 3 10.244.2.17
kubectl exec web -- ping -c 3 -M do -s 1422 10.244.2.17

Test by Pod IP, to leave Services and DNS out of it. If a Pod on the same node answers and one on another node does not, the problem is between the nodes. If small pings pass and large ones do not, it is the MTU.

5. Does the traffic leave one node and reach the other? Capture on both nodes at once:

bash
sudo tcpdump -ni eth0 -T vxlan udp port 8472     # Flannel; 4789 for Calico's VXLAN, 6081 for Geneve
sudo tcpdump -ni eth0 'ip proto 4'               # Calico's IP-in-IP

Packets that leave worker-1 and never arrive at worker-2 are dropped by the network between them: a security group, a firewall, or a cloud that does not carry IP-in-IP.

6. What does the plugin itself say?

bash
cilium status
cilium connectivity test
hubble observe --verdict DROPPED --last 50
calicoctl node status                  # BGP peers, for Calico

A plugin's own checks know its data path better than any general tool, and the drop reasons in Hubble, or in Calico's flow logs, name the policy, the route or the MTU directly.

Key Takeaways

  • The Kubernetes network model gives every Pod its own IP, reachable from every Pod on every node without NAT. Kubernetes states it; a CNI plugin implements it.
  • The container runtime, not the kubelet, runs the CNI plugin: ADD when a sandbox is created, DEL when it is removed. The plugin makes a veth pair, and its IPAM picks the address.
  • A plugin has two parts: a binary that attaches each Pod, and an agent on each node that keeps the routes, tunnels and rules to the others. Until its configuration exists, the node is NotReady.
  • Between nodes, Pod packets are either routed natively, which needs a network that knows the Pod addresses, or wrapped in an overlay such as VXLAN, Geneve or IP-in-IP, which only needs the nodes to reach each other.
  • An overlay costs bytes: 50 for VXLAN, 20 for IP-in-IP, 60 for WireGuard over IPv4. The Pod's MTU must leave room for them, or large packets are fragmented or lost.
  • eBPF lets a plugin run its own code at kernel hooks, with its state in maps: Services balanced at connect(), policies by identity, and every flow visible.
  • NetworkPolicy is enforced by the plugin, or not at all. A selected Pod is isolated, policies only allow and add up, both ends of a connection must allow it, and a default-deny of egress needs a rule for DNS.
  • To debug, go from the node to the wire: the plugin's Pods, the node's block, the Pod's interface, a ping by IP, a capture on the tunnel port, then the plugin's own tools.

FAQ

What is CNI in Kubernetes?

CNI, the Container Network Interface, is the specification and set of plugins that container runtimes use to connect containers to a network. In Kubernetes, containerd or CRI-O runs a CNI plugin for every Pod sandbox: it creates the Pod's interface, gives it an IP address and sets up its routes. Flannel, Calico and Cilium are all CNI plugins.

What is the difference between an overlay network and native routing?

With native routing, Pod packets cross the network between nodes with their own addresses, so the network must know where each node's Pod addresses are, through BGP, cloud routes or Pod addresses taken from the cloud's network. With an overlay, each node wraps Pod packets in packets addressed to the other node, with VXLAN, Geneve or IP-in-IP, so the network only needs to connect the nodes. Overlays work anywhere, but add bytes to every packet.

Which CNI plugin should I use?

On a managed cluster, start with the provider's default or its Cilium-based option. On your own clusters, Flannel is the simplest but needs an add-on to enforce NetworkPolicy; Calico suits networks that use BGP and offers iptables, nftables and eBPF data planes; Cilium uses eBPF throughout, replaces kube-proxy, and adds identity-based policy and flow visibility, but needs Linux 5.10 or later.

Does Cilium replace kube-proxy?

It can. With its kube-proxy replacement turned on, Cilium implements ClusterIP, NodePort and LoadBalancer Services with eBPF programs: connections inside the cluster are balanced when the socket connects, and traffic from outside is translated at the node's interface. The cluster then runs no kube-proxy DaemonSet, and its Services live in eBPF maps rather than iptables chains.

What is eBPF in Kubernetes networking?

eBPF is a Linux kernel feature that runs small, verified programs at hooks in the kernel, such as an interface's traffic or a socket's connect(). Network plugins such as Cilium and Calico's eBPF data plane use it to route packets, balance Services and enforce policies with map lookups instead of iptables rules, which scales better and lets them report every flow.

What MTU should Pods use with VXLAN?

The network's MTU minus 50 bytes for VXLAN over IPv4, which is 1450 on a standard 1500-byte network, or minus 70 over IPv6. Most plugins detect the node's MTU and set the Pods' for you. If the path between nodes has a smaller MTU than the nodes' own interfaces, for example through a VPN, set the plugin's MTU by hand.

Why can Pods on different nodes not communicate?

When Pods on the same node can talk but Pods on different nodes cannot, the problem is the path between the nodes. Check that firewalls and security groups allow the overlay's traffic, such as UDP 8472 or 4789 for VXLAN, UDP 6081 for Geneve or IP protocol 4 for IP-in-IP, that the plugin's agent runs on both nodes, and, with native routing, that the network has a route to each node's Pod addresses.

What's Next

You have followed a packet below the Service layer. The runtime ran a CNI plugin to give a Pod its interface and address; the plugin's agent connected the nodes, by routes or a tunnel; eBPF programs took over the work of kube-proxy and iptables; and NetworkPolicy, enforced by the same plugin, decided which connections may pass. With this part, Kubernetes Internals: How a Cluster Really Works has followed a cluster from the API server down to the wire. When a packet does not arrive, start from the table above; when something else does not converge, the Kubernetes Troubleshooting playbook is the next stop, and the whole series lists every part.


Comments

No comments yet.