DevOps & Containers
Kubernetes CNI Explained: How Pod Networking Really Works
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيWhen you install Kubernetes, one of the first surprises is that pods cannot talk to each other until you install a network plugin. Nodes stay in a NotReady state and CoreDNS pods wait in Pending. That missing piece is the CNI plugin. Kubernetes defines how pod networking must behave, but leaves the actual wiring to plugins that follow the Container Network Interface (CNI) specification. Understanding CNI makes it much easier to choose a plugin, secure traffic and debug connectivity problems.
What CNI Is
CNI is a specification and a set of libraries, originally from CoreOS and now a CNCF project. It defines a simple contract: when a container runtime creates a pod sandbox, it calls a CNI plugin binary with an ADD command, and the plugin attaches the pod to the network and returns its IP address. When the pod is deleted, the runtime calls DEL to clean up.
On a typical node you will find:
- Plugin binaries in
/opt/cni/bin/(for examplebridge,loopback,host-local,portmap, plus the binaries of your chosen plugin). - Network configuration in
/etc/cni/net.d/, as.confor.conflistJSON files. The runtime uses the first file in lexical order.
The kubelet does not call CNI directly. The container runtime (containerd or CRI-O) does it when it creates the pod sandbox.
The Kubernetes Networking Model
Every CNI plugin must satisfy the same basic rules:
- Every pod gets its own IP address.
- Pods can reach all other pods on any node without NAT.
- Agents on a node (such as the kubelet) can reach all pods on that node.
How a plugin achieves this varies. Some create an overlay network, wrapping pod traffic inside VXLAN or similar tunnels between nodes. Others use native routing, announcing pod subnets with BGP or relying on routes in the underlying network. Overlays work almost anywhere but add encapsulation overhead and reduce the usable MTU; native routing is more efficient but needs a network that cooperates.
Note that Services and kube-proxy are a separate layer. CNI gives pods addresses and connectivity, while kube-proxy (or a replacement such as Cilium) implements Service virtual IPs.
Popular CNI Plugins Compared
| Plugin | Data path | NetworkPolicy | Good fit |
|---|---|---|---|
| Flannel | VXLAN overlay (default), host-gw | No (needs an add-on) | Small, simple clusters, labs |
| Calico | Routing with BGP, or IP-in-IP / VXLAN | Yes, plus its own extended policies | General production, policy-heavy setups |
| Cilium | eBPF, overlay or native routing | Yes, including L7 policies | Performance, observability, kube-proxy replacement |
Flannel is the easiest to understand and is the default in k3s. It gives each node a subnet and tunnels traffic between nodes. It does not enforce NetworkPolicy on its own, so policies you create are silently ignored.
Calico is widely used in production. It can route pod traffic without encapsulation when the network allows, and it enforces standard Kubernetes NetworkPolicy and its own richer policy types.
Cilium uses eBPF programs in the Linux kernel instead of long iptables chains. It can replace kube-proxy, enforce policies at L3 to L7, and its Hubble component shows live flows between services. It needs a reasonably recent kernel, which current Ubuntu and Debian releases provide.
Choosing a CNI for a VPS Cluster
- Learning or a single node: keep the default. With k3s that is Flannel, and it just works. See our guide to Kubernetes and k3s on a VPS.
- You need NetworkPolicy: choose Calico or Cilium from the start. Changing CNI on a running cluster is disruptive and usually means rebuilding nodes.
- Nodes across different networks: an overlay (VXLAN or WireGuard encryption, which both Flannel and Cilium can provide) avoids depending on the provider's routing.
- Check the MTU: encapsulation adds headers. If the underlying interface is 1500 bytes, VXLAN pods usually get 1450. Wrong MTU values cause strange symptoms such as small requests working while large responses hang.
Restricting Traffic with NetworkPolicy
By default every pod can reach every other pod. With a policy-capable CNI you can lock this down. This example allows only pods labeled app: web to reach the database on port 5432:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-allow-web
namespace: prod
spec:
podSelector:
matchLabels:
app: postgres
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: web
ports:
- protocol: TCP
port: 5432Once a pod is selected by an Ingress policy, all other inbound traffic to it is denied. Cloud firewall rules on the VPS still matter too: they protect the nodes themselves, while NetworkPolicy controls traffic between pods.
Troubleshooting Pod Networking
Start with these checks:
kubectl get nodes # NotReady often means no CNI
kubectl -n kube-system get pods -o wide # are CNI pods running on every node?
ls /etc/cni/net.d/ /opt/cni/bin/ # config and binaries present?
kubectl run tmp --rm -it --image=busybox:1.36 -- sh
# inside the pod:
nslookup kubernetes.default
wget -qO- http://<service-name>.<namespace>.svc.cluster.localCommon causes of failure: firewall rules blocking the overlay port between nodes (UDP 8472 for Flannel VXLAN on Linux, or UDP 4789 for standard VXLAN), overlapping pod CIDR and host network ranges, leftover config files from a previous CNI in /etc/cni/net.d/, and MTU mismatches.
A three-node test cluster on small VPS instances is an affordable way to learn all of this hands-on; see our pricing.
FAQ
Can I change the CNI plugin on an existing cluster?
It is possible but risky. Pods must be recreated to get new interfaces, and leftover configuration can conflict. For small clusters it is usually simpler to build a new cluster with the desired CNI and move workloads over.
Why are my NetworkPolicies not doing anything?
The installed CNI probably does not enforce them. The Kubernetes API accepts NetworkPolicy objects regardless, so they look applied. Flannel alone ignores them; Calico or Cilium enforce them.
Does the CNI affect performance?
Yes, mostly through encapsulation overhead, MTU and how packets are processed. For most small and medium workloads the difference is modest, and correct configuration matters more than the choice of plugin.