The Netfilter Path a Packet Takes

A packet from a Pod to a Service ClusterIP enters the Node kernel, which swaps its destination for a real Pod IP. Which netfilter hooks does it pass, why must that precede routing, and what happens to the reply?

Play the animation 6 steps, animated, plays on a computer or a tablet

The Netfilter Path a Packet Takes, Kubernetes diagram

The Netfilter Path a Packet Takes, step by step

  1. A packet leaves the client Pod through its veth pair and arrives in the host network namespace, so the kernel sees it as incoming traffic. The first hook it meets is PREROUTING, before any routing decision has run, and conntrack records a flow it has never seen.

    Play from step 1
  2. Inside PREROUTING the nat table runs, and the KUBE-SERVICES chain kube-proxy writes in iptables mode matches the ClusterIP. A rule down that chain rewrites the destination to a real backend, 10.244.2.7:8080 on Node-2, and conntrack stores that translation beside the flow.

    Play from step 2
  3. Only now does the kernel decide where the packet goes, and it decides on the rewritten address. Unlike the boxes around it, this one is not a hook but a fork. A packet for an address the Node itself owns goes to INPUT for local delivery. Everything else goes to FORWARD.

    Play from step 3
  4. FORWARD is where the filter table runs and an iptables NetworkPolicy implementation drops what is not allowed. POSTROUTING is the last hook before the wire, and MASQUERADE lives there because only after routing is the outgoing interface known. By default neither kube-proxy nor egress masquerade rules SNAT a Pod to Pod packet, so the Pod source survives.

    Play from step 4
  5. The reply arrives on eth0 from 10.244.2.7. At PREROUTING conntrack matches the recorded flow and sees an established connection, so no Service rule is read on the way back. The reversal itself is a source rewrite, so it lands at POSTROUTING: the source becomes 10.96.0.20 again, the address the Pod dialed, and the veth hands the packet to the Pod.

    Play from step 5
  6. At the fork the packet turns the other way when the address after the rewrite belongs to the Node itself, as it does for a hostNetwork backend on this Node. Routing sends the packet to INPUT and a local socket has it. Such a packet never reaches POSTROUTING, so MASQUERADE can never see it.

    Play from step 6

Sources