Explain how kube-proxy implements a ClusterIP in iptables mode.
Short answer
In iptables mode, kube-proxy writes NAT rules into the Linux kernel on every node. When traffic is sent to a Service's ClusterIP, these rules pick one of the backend Pods at random and rewrite the destination address to that Pod's IP. The kernel does the forwarding; kube-proxy only writes the rules.
Explained in simple words
A ClusterIP like 10.96.45.12 doesn't belong to any machine — no network card has that address. So how does traffic to it ever arrive anywhere? Every node has a set of "if traffic goes to X, send it to Y instead" rules in its kernel. kube-proxy keeps those rules up to date. It's like a post office that knows "letters addressed to 'Customer Service' should go to one of these three desks", and picks a desk for each new letter.
How it works, step by step
Say Service web has ClusterIP 10.96.45.12:80 and three Pods behind it.
- kube-proxy watches the API server for Services and EndpointSlices (the list of Pod IPs).
- It creates iptables rules in the NAT table, organised in chains:
KUBE-SERVICES— the entry point. One rule per Service: "if destination is10.96.45.12:80, jump to chainKUBE-SVC-WEB…."KUBE-SVC-WEB…— the per-Service chain. It picks a Pod at random using probabilities:- rule 1: 33% chance → go to Pod 1's chain
- rule 2: 50% of the rest → go to Pod 2's chain
- rule 3: everything left → go to Pod 3's chain (This gives each Pod an equal one-third share.)
KUBE-SEP-…— one chain per endpoint (Pod). It does DNAT: rewrites the destination from10.96.45.12:80to the Pod IP, for example10.244.2.7:8080.
- The packet now travels to that Pod through the normal Pod network.
- conntrack (Linux connection tracking) remembers this choice, so all packets of the same connection go to the same Pod, and replies are translated back correctly.
Example — see the rules yourself
sudo iptables -t nat -L KUBE-SERVICES -n | grep 10.96.45.12
# KUBE-SVC-ABCD1234 tcp -- 0.0.0.0/0 10.96.45.12 /* default/web */ tcp dpt:80
sudo iptables -t nat -L KUBE-SVC-ABCD1234 -n
# KUBE-SEP-AAA all -- ... statistic mode random probability 0.33333333349
# KUBE-SEP-BBB all -- ... statistic mode random probability 0.50000000000
# KUBE-SEP-CCC all -- ...
sudo iptables -t nat -L KUBE-SEP-AAA -n
# DNAT tcp -- ... tcp to:10.244.2.7:8080
Two things interviewers love to hear
- Balancing is per connection, not per request. Once a connection is pinned to a Pod, all its requests go there. That's why gRPC or HTTP/2 traffic (one long-lived connection carrying many requests) can end up hitting only one Pod. The fix is an L7 load balancer (a service mesh like Linkerd or Istio, or client-side load balancing).
- iptables doesn't scale well to huge clusters. Rules are checked one by one, and the number of rules grows with every Service and Pod. With thousands of Services, updates get slow. Large clusters use IPVS mode, the newer nftables mode, or an eBPF CNI like Cilium that replaces kube-proxy.
What to say in the interview
"In iptables mode, kube-proxy watches Services and EndpointSlices and writes NAT rules on every node. A KUBE-SERVICES chain matches the ClusterIP and port and jumps to a per-Service chain. That chain picks one endpoint randomly using iptables statistic probabilities, and the endpoint chain does a DNAT to the Pod's IP and port. Conntrack keeps the connection pinned to that Pod. So load balancing is per connection, not per request — which is why long-lived gRPC connections can get stuck on one Pod. And because iptables rules are evaluated sequentially and grow with the number of Services, very large clusters move to IPVS, nftables or eBPF."
Follow-up questions you may get
- What is the difference between iptables and IPVS mode? IPVS uses kernel hash tables built for load balancing, so lookups stay fast with thousands of Services, and it supports more algorithms (round robin, least connections).
- How would you debug a Service that doesn't route traffic? Check the Service selector matches the Pod labels, check
kubectl get endpointslicesisn't empty, check the Pods are Ready, then check the iptables rules on the node.