Pod Egress and SNAT
A Pod opens a connection to a server on the internet and gets its answer, though no route out there leads back to a Pod address. How does the reply find its way home, and who rewrites the addresses on the way?
Play the animation 5 steps, animated, plays on a computer or a tablet
Pod Egress and SNAT, step by step
-
The app opens a connection to 1.1.1.1 and the packet leaves eth0 carrying src 10.244.1.5. No route on the internet leads back to that address, so the last netfilter hook on the way out is where the Node walks its POSTROUTING rules.
Play from step 1 -
Only the first packet of a flow walks these rules, in order. RETURN rules for the non-masquerade ranges come first (one covers the pod CIDR 10.244.0.0/16), so Pod to Pod traffic keeps its source. 1.1.1.1 is in none of them, so the packet falls past RETURN to masquerade.
Play from step 2 -
MASQUERADE rewrites the source to the address of the interface the packet leaves by, 192.168.1.20. The flow it just translated goes into conntrack, and the packet leaves looking like it came from the Node, an address the reply can be routed to.
Play from step 3 -
The server never saw the Pod IP, and its reply returns to 192.168.1.20. It walks no masquerade rule at all. As it enters the Node, a conntrack lookup finds the stored entry and reverses the translation off it alone, which costs a lookup rather than a rule scan.
Play from step 4 -
With the destination restored to 10.244.1.5 the reply goes down the veth into the Pod. The Pod used its own source address the whole way. The rewrite and its reversal both happened on the Node, invisible to either end of the connection.
Play from step 5