👋 Hi! I’m Bibin Wilson. In each edition, I share practical tips, guides, and the latest trends in DevOps and MLOps to make your day-to-day DevOps tasks more efficient. If someone forwarded this email to you, you can subscribe here to never miss out!

✉️ In Today’s Deep Dive Edition

Today, we are going deep into NAT in Kubernetes networking.

This is one of those fundamental networking concepts you should understand if you manage production Kubernetes clusters, troubleshoot networking issues, or prepare for technical interviews.

We’ll cover:

  • How SNAT, DNAT, and MASQUERADE actually work

  • What happens when a Pod connects outside the cluster

  • How Kubernetes uses NAT for Service traffic

  • Where conntrack, kube-proxy, and CNI plugins fit in

  • A real GKE networking issue I faced and how we fixed it

By the end, you should have a much clearer picture of what happens to a packet as it moves through a Kubernetes cluster.

📦 Useful Resources

  1. Kubestronaut Roadmap: Aman Jaiswal shares his journey to becoming a Kubestronaut, along with the key learnings and experiences that helped him along the way.

  2. 50% off KodeKloud: Save up to 50% on KodeKloud annual subscriptions for a limited time.

  3. 20% OFF KubeCon North America (Code: COMTECHIES20): KubeCon + CloudNativeCon North America is coming to Salt Lake City, November 9–12. Get 20% off the current registration price using code COMTECHIES20.

In one of the GKE projects I worked on, Pods using VPC secondary IP ranges were unable to connect to services running on our organization's private network.

While troubleshooting the iptables rules, we found that traffic leaving the cluster still had the Pod IP as the source IP. The problem was that the Pod IP range was not routable from the organization's private network, so the return traffic couldn't reach the Pods.

We fixed this by configuring ip-masq-agent to masquerade the traffic using the node IP. Since the node IP is in the subnet's primary range, pod traffic could reach the org's private network.

The fix was a single NAT configuration change. But understanding why it worked meant understanding what Kubernetes was actually doing to the packets behind the scenes.

So, before we get into Kubernetes, let's quickly cover the fundamentals of NAT. Once you understand these concepts, Kubernetes networking becomes much easier to follow.

Let's get started.

What is NAT?

When a device on a private network needs to access the internet, NAT translates its private IP address into a public IP address. The following image illustrates it better.

NAT also tracks the connection. When the response returns, NAT uses that information to forward the packet to the correct device.

The external server only sees the translated public IP. But this brings up an important question:

Which IP address does NAT actually change: the source or the destination?

That depends on the type of NAT being used. Let's start with SNAT.

SNAT (Source NAT)

SNAT is the process of rewriting the source IP of an outgoing packet. NAT changes the source IP before forwarding the packet and reverses the translation when the response comes back.

For example, imagine a VM in a private subnet that needs to download packages from the internet. The VM doesn't have a public IP, so a NAT gateway replaces its private source IP with a public IP before sending the packet out.

When the response returns, NAT translates it back and forwards it to the original VM.

A classic example of SNAT is an AWS NAT Gateway, which allows instances in private subnets to access the internet using the NAT Gateway's Elastic IP.

Since many hosts can share the same public IP, NAT can also rewrite the source port to keep each connection unique. This is called PAT (Port Address Translation), also known as NAPT.

Cloud NAT services such as AWS NAT Gateway and Azure NAT Gateway perform port translation behind the scenes

But there is a limit.

NAT has a finite number of source ports available for translated connections. A large number of connections, especially to the same destination, can eventually cause SNAT port exhaustion.

When that happens, new connections may fail until ports become available again.

So far, we've assumed that NAT knows which source IP to use for the translation.

But what if the public IP address used for the translation changes?

That's where MASQUERADE comes in.

IP Masquerade

In the SNAT example above, the NAT IP is static, so normal SNAT works well. But what if the IP assigned to the NAT device's outgoing interface changes? That's where IP MASQUERADE comes in.

Instead of specifying the translated IP manually, MASQUERADE automatically uses the IP assigned to the outgoing network interface.

A classic example is a home router. Your ISP may change the public IP assigned to the router. Instead of configuring a fixed SNAT address, MASQUERADE automatically uses the IP currently assigned to the router's outgoing interface.

The following image illustrates it better.

IP MASQUERADE is a key concept used in Kubernetes networking, which we'll learn in the upcoming section.

So far, we've looked at changing the source address. Now let's look at what happens when the destination address needs to change.

DNAT (Destination NAT)

DNAT (Destination Network Address Translation) is the opposite of SNAT. Instead of changing where a packet came from, it rewrites where it’s going.

If you have published a Docker container port on a typical bridge network, you have seen this same DNAT concept in action.

The following image illustrates DNAT using Docker port forwarding.

When we run a container, it gets a private IP on the container network, something like 172.17.0.2, and the app inside listens on port 80. That address only exists on the VM. Nothing outside can route to it.

So clients connect to the VM instead, on 203.0.113.10:8080.

When that packet arrives, the VM rewrites the destination from 203.0.113.10:8080 to 172.17.0.2:80 and forwards it to the container. Both the address and the port change.

That rewrite is DNAT.

DNAT is commonly used for things such as port forwarding and sending traffic from a public-facing address to servers running inside a private network.

Now that SNAT, MASQUERADE, and DNAT are clear, we can see how these same concepts are used inside Kubernetes.

How Kubernetes Implements NAT

Now let's connect those NAT fundamentals to Kubernetes.

Kubernetes doesn't introduce a new type of NAT. The same SNAT, MASQUERADE, and DNAT concepts we just covered are used under the hood of Kubernetes networking.

What changes is where and why the translation happens.

For example,

  • Pod to Internet: The Pod's source IP may be SNATed, or MASQUERADEd, before the traffic leaves the cluster.

  • Service to Pod: The Service destination can be DNATed to a backend Pod IP.

  • Return traffic: conntrack keeps track of these translations so they can be reversed for response packets.

The following parts of the Kubernetes networking stack make this happen.

  • conntrack tracks connections and NAT mappings.

  • kube-proxy configures Service forwarding. In iptables mode, this includes NAT rules that eventually DNAT Service traffic to backend Pods.

  • The cluster networking setup, including the CNI plugin and other networking components, provides Pod connectivity and may also configure egress masquerading.

Key Insight:

Direct Pod-to-Pod communication, whether on the same or different nodes, does not require NAT or proxies. Pods are designed to communicate with each other using their Pod IPs.

The CNI/network implementation makes this possible. It might use native routing, overlays, VPC routes, eBPF, or another method underneath.

Now, let's look at the two most common scenarios where we can see NAT in action.

Pod Egress and MASQUERADE

Pods have their own IP addresses, but those addresses may not be routable outside the cluster network.

When a Pod connects to an external service, its source IP can be MASQUERADEd behind the node's IP.

The external service sees the request as coming from 192.168.1.20, not 10.1.1.5 as illustrated in the image below.

The exact behavior depends on the cluster networking configuration. For example, Calico can SNAT Pod traffic to the node IP, while some clusters use ip-masq-agent to control which destination ranges should be masqueraded.

Kubernetes Services and DNAT

Now let's look at the other direction.

Suppose a NodePort Service exposes an application on 192.168.1.20:30080, and the backend Pod is listening on 10.1.1.5:8080

When a client connects to the NodePort, iptables rewrites the destination to the backend Pod. The packet is then forwarded to the Pod.

The following image illustrates the traffic flow when the externalTrafficPolicy is set to Cluster (default policy).

Important Note: In the image, a single node is shown for simplicity. With externalTrafficPolicy: Cluster, the Pod can be on another node, where SNAT helps ensure return traffic flows back through the receiving node.

And what happens when the Pod sends the response?

That's where conntrack comes into the picture. Linux already knows that the original connection was translated from 192.168.1.20:30080 to 10.1.1.5:8080. It uses that connection state to apply the reverse NAT translation to the response.

So, from the client's point of view, it is still communicating with 192.168.1.20:30080

Key Insight:

By default, Kubernetes Services use externalTrafficPolicy: Cluster, in which case the Pod may see the node IP instead of the original client IP.

To preserve the client's real IP, set externalTrafficPolicy to Local. Kubernetes then sends traffic only to local endpoints and preserves the client source IP. The trade-off is that traffic sent to a node without a local endpoint is dropped.

That’s a Wrap!

If you work with Kubernetes, these networking fundamentals would really help with your day-to-day work and cluster administration.

They are also common topics in Kubernetes, DevOps, SRE, and platform engineering interviews because they test whether you understand what actually happens to network traffic inside a cluster.

You don't have to be a networking expert. But if you manage Kubernetes in production, these are fundamentals worth knowing.

Reply

Avatar

or to participate