Network
NetworkPolicy
A packet gets dropped
Write a deny rule and watch the CNI plugin drop a packet before it reaches the Pod. Kubernetes clusters start fully open - any Pod can talk to any Pod - and a NetworkPolicy doesn't add deny rules at all: it selects Pods, closes everything, and re-opens only what it allows. The CNI enforces that in each node's kernel.
- Default
- allow · no policy selects the Pod
- Policy
- app:api · ingress from app:web · TCP 8080
- Enforced by
- calico iptables chains on every node
- Apply → enforced
- ≈ 10 ms
Read the whole lesson as text
1. Default allow
The api Deployment runs one Pod, api-…-7d9fk at 10.244.2.19 on node-2. Nothing protects it: Kubernetes starts with no isolation at all, so any Pod in the cluster can open a connection to any other. A debug busybox Pod on node-1 proves it - a direct curl to the Pod's address crosses the VXLAN tunnel and gets 200 OK. No rule allowed this; that's the point.
- From a debug Pod on node-1, a direct curl to the api Pod's address.
- The call crosses nodes in the usual VXLAN wrapper.
- The packet reaches the container and the health check answers 200.
2. Write the policy
kubectl posts a NetworkPolicy to the API server. It selects Pods labelled app:api and says: ingress is allowed only from Pods labelled app:web, and only on TCP 8080. The API server validates it and stores it in etcd - but stores is all it does. The moment a policy selects a Pod, every other path to that Pod closes: there are no deny rules anywhere, only allow-lists.
- kubectl posts one NetworkPolicy that selects the api Pods.
- The API server stores the policy, and nothing else happens here.
- kubectl gets 201 Created. The policy exists, but isn't enforced yet.
3. Who enforces it
Nothing in the control plane drops packets. Enforcement belongs to the CNI: flannel in this cluster moves packets but has no policy support, so the cluster also runs Calico's policy layer on top (flannel plus Calico policy is often called Canal). Felix, Calico's agent on every node, watches NetworkPolicies, Pods and namespaces, work out exactly which IPs may talk to which, and program cali- chains into every node's iptables. Each node enforces the whole policy for its own traffic.
- Felix, Calico's agent on node-2, hears about the policy the moment it's stored.
- It writes new chains into node-2's iptables: the allow-list, per destination Pod.
4. The drop
The busybox Pod tries the same curl again. The packet arrives on node-2, flannel unwraps it, and the kernel walks it toward the bridge - straight into cali-tw. The source 10.244.1.30 is not in the web IP set, no rule matches, and the chain ends in DROP. The container receives nothing: no reset, no reply, just silence until curl times out.
- The call arrives again, unwraps on node-2, and walks toward the bridge.
- The only allow rule admits web Pods on 8080, and this source isn't one.
- The packet dies in the kernel, before the veth. The app never hears it.
5. The allowed call
web-…-x2kqp on node-2 makes the identical call - and it carries the label app:web. Its packet hits the same chain, the source 10.244.2.17 is in the IP set, the port is 8080, and the verdict is ACCEPT. The reply needs no rule of its own: the chain's first line accepts ESTABLISHED connections, so answers ride conntrack back. Egress works the same way, on cali-fw-… chains, if the policy asks for policyTypes: Egress.
- The same call from a web Pod takes the same path into the same chain.
- Rule 1 matches, the verdict is ACCEPT, and the packet continues to the bridge.
- The api Pod answers 200, and the reply passes on the conntrack rule.
6. Still open
The busybox Pod isn't blocked everywhere - only the api Pod got isolated. Nothing selected the web Pods, so a call from busybox straight to web-…-p8m4z still sails through the very kernel that just dropped its SYN. Isolation follows selection: a Pod no policy touches keeps the cluster default, which is allow.
- The very same Pod can still reach anything no policy protects.
Summary
Kubernetes starts with every Pod reachable from every Pod, and the API server never drops a packet - it only stores the rules. A NetworkPolicy isolates exactly the Pods it selects and turns labels into IP allow-lists, enforced per node by the CNI in iptables. Everything it doesn't select is still open.
Next lesson: Cilium and eBPF