Imagine you need to talk to an application running inside a k8s pod from a different node of your cluster.
What should be your destination IP address?
Pod IP Address
Well what's the big deal? We can simply use the destination pod IP address.
Well it works because regardless of which node you are every single node has an ip route that leads to direct link routing for destinations with Pod IP CIDR range.
Which allows you to do direct layer 2 ARP communication since the nodes are already connected
However the problem is pods are designed to be destructed and re-constructed on demand. For example an auto scaling policy can create new pods or remove existing pods based on scale. Apart from that pods can go down due to high traffic load and then k8s replicaset will create new pods to esnure the desired number of replicas
If a pod is re-created its not guaranteed that it will have the same IP address assigned.
Therefore, talking to a pod via its pod IP is not a reliable approach
The solution is Cluster IP
ClusterIP
ClusterIP is a k8s service that stays consistent and they can be used for talking to pods reliably since they do not change over time with pod re-creations
As we can see, we can reach the pod via both ClusterIP and the pod IP
But the interesting part is if we try to observe the packets with tcpdump, we won't see any packet with destination IP as 10.110.229.229
Interestingly we are calling at 10.110.229.229 (ClusterIP) but the SYN packet on TCP connection establishment was 10.36.0.1 which is the pod IP
This means the destination address was re-written even before the packet reached the network interface
This is exactly where the DNAT come into the picture
NAT Rules
NAT stands for Network Address Translation. Basically translating from one IP and Port to another IP and Port
In the nodes of a k8s cluster, kube-proxy writes some rules in the NAT table
We can observe them via sudo iptables -t nat -S KUBE-SERVICES
If we follow along the chain of KUBE-SVC-OGYXBJX3CJ7W6DXK we reach a rule which says
tcp -j DNAT --to-destination 10.36.0.1:80
This is exactly what translates your packets and re-writes the destination from ClusterIP:NodePort to PodIP:PodPort
Hope you enjoyed !!!







Top comments (0)