iptables
iptables is the Linux tool for packet filtering and network address translation. The filtering runs inside the kernel, in a framework called Netfilter. iptables is the command you use to write rules into it. Docker, Kubernetes and OpenStack still manage rules this way, which is why sudo iptables -L on a container host prints far more than you wrote.
How it works
Rules live in tables, and each table has chains. The filter table, the default, has INPUT, OUTPUT and FORWARD chains. The nat table uses PREROUTING for destination NAT and POSTROUTING for source NAT. Each rule has a match, such as -p tcp --dport 22, and a target, such as ACCEPT, DROP, REJECT or MASQUERADE.
A packet walks a chain from top to bottom, and the first terminating target wins. Append a DROP for all SSH traffic, then an ACCEPT for 192.168.12.0/24, and that ACCEPT never fires. Insert it above the DROP with -I instead.
Jaideep Khandelwal, One2N's CTO, had a NAS with only an ethernet port and no cable run to the router. A spare Raspberry Pi 4 had both ethernet and Wi-Fi. He made the Pi a NAT device for the NAS:
MASQUERADE rewrites each packet's source to the Pi's Wi-Fi address, so replies find their way back. The NAS could then reach the internet and run its cloud sync. Rules vanish on reboot unless you save them with iptables-save.
Why it matters in production
In Kubernetes, kube-proxy in iptables mode turns each Service into nat table chains. Every endpoint gets one DNAT rule with a random probability. With 3 pods the rules read 1/3, then 1/2, then the rest, so each pod gets 2/3 x 1/2 = 1/3 of new connections. Ten thousand Services mean tens of thousands of rules, and each resync of that many rules gets slow.
That is why kube-proxy gained an nftables mode, generally available in Kubernetes 1.33. nftables, in the kernel since Linux 3.13, is the successor. Many distributions already run iptables as a wrapper over it. A CNI plugin may skip both and use eBPF.
See also
On the One2N blog
References
