Skip to content

Home

To master Kubernetes networking, the most effective approach is to strip away the abstractions and understand the underlying Linux kernel primitives that Docker uses. Kubernetes does not invent new networking rules—it builds directly on top of the same Linux networking mechanisms (network namespaces, virtual interfaces, bridges, routing tables, and packet filtering).


1. The Core Linux Primitives Under the Hood

A Docker container is not a lightweight virtual machine; it is a standard Linux process constrained by cgroups and isolated by namespaces. For networking, the relevant component is the Network Namespace (netns).

  1. Network Namespace (netns): An isolated copy of the network stack, complete with its own routing table, ARP table, firewall rules, and interfaces (eth0, lo, etc.).
  2. Virtual Ethernet Pair (veth): A bi-directional software pipe connecting two network namespaces (like a virtual patch cable with two ends):
  3. Host End (veth*): Placed in the host's root network namespace.
  4. Container End (eth0): Placed inside the container's isolated network namespace.
  5. Linux Bridge (docker0): A virtual Layer 2 software switch residing on the host. All host-ends of the veth pairs connect to this bridge so containers can broadcast and switch frames locally.
       +---------------------------------------------------+
       |                    HOST                           |
       |                                                   |
       |   +------------------ Linux Bridge -------------+ |
       |   |                 (docker0: 172.17.0.1)       | |
       |   +---------+-------------------------+---------+ |
       |             |                         |           |
       |           vethA                     vethB         |
       +-------------|-------------------------|-----------+
                     | (Virtual Wire)          | (Virtual Wire)
       +-------------|-----------+ +-----------|-----------+
       | Container 1 |           | | Container 2 |           |
       | (netns 1)  eth0         | | (netns 2)  eth0         |
       | 172.17.0.2/16           | | 172.17.0.3/16           |
       +-------------------------+ +-------------------------+

2. Core Docker Network Drivers & Their Kubernetes Equivalents

Docker provides four standard network modes:

Network Driver How It Works Under the Hood Kubernetes Equivalent
bridge (Default) Allocates an isolated netns, attaches a veth pair to a software bridge (docker0 or user-defined), and assigns a private subnet IP (e.g., 172.17.0.0/16). Standard Pod networking managed by CNI plugins (Calico, Flannel, Cilium).
host Container shares the host’s network stack directly (--net=host). No port mapping or IP isolation occurs. Setting hostNetwork: true in a Pod spec (e.g., node agents, Ingress controllers).
none Container gets an isolated netns with only the loopback interface (lo). No external networking. Air-gapped batch pods or custom network bootstrapping init containers.
container:<id> Attaches a container directly into an existing container's network namespace (shares IP and localhost). The fundamental architecture of a Kubernetes Pod (containers sharing the pause container's netns).

3. Traffic Flow and Packet Lifecycle

A. Container-to-Container (Same Host)

  • Default Bridge: Container A looks up Container B's IP in its ARP table. Traffic flows: eth0 $\rightarrow$ vethA $\rightarrow$ docker0 bridge $\rightarrow$ vethB $\rightarrow$ eth0 via Layer 2 switching.
  • User-Defined Bridge: Enables Docker’s embedded DNS server (127.0.0.11), allowing containers to discover and communicate with each other using container names rather than ephemeral IPs.

B. Container-to-External (Egress / Internet Access)

  • Containers have private RFC1918 IPs (e.g., 172.17.0.2).
  • When traffic leaves the host network interface (e.g., eth0 / enp0s3), the Linux kernel applies Source NAT (SNAT / MASQUERADE) via iptables: $$\text{Container IP (172.17.0.2)} \longrightarrow \text{Host Primary IP (e.g., 10.0.1.5)}$$

C. External-to-Container (Ingress / Port Forwarding via -p 8080:80)

  • Docker configures Destination NAT (DNAT) rules in the PREROUTING chain of iptables: $$\text{Host:8080} \longrightarrow \text{Container IP:80}$$

4. Mapping Docker to Kubernetes Architecture

Dimension Docker (Single-Node Focus) Kubernetes (Cluster-Wide Focus)
Multi-Container Sharing --net=container:<target_id> Pod Architecture: An infrastructure container (the pause container) holds the network namespace open; application and sidecar containers join it.
Cross-Host Routing Overlay network with Docker Swarm or host-level port mapping. CNI Plugins (Calico, Cilium, Flannel): Implement a flat, routable IP-per-Pod network across nodes without requiring NAT between Pods.
Port Forwarding / Discovery -p 8080:80 (Host-bound iptables DNAT) Services (ClusterIP, NodePort, LoadBalancer): Handled cluster-wide by kube-proxy (iptables/IPVS) or eBPF (Cilium).
DNS Service Discovery Embedded resolver at 127.0.0.11 CoreDNS: Resolves cluster-wide DNS records (e.g., <service>.<namespace>.svc.cluster.local).

5. Hands-on Diagnostic Verification

To inspect these components directly on a Linux terminal running Docker:

# 1. Create a custom user-defined bridge network
docker network create my-net

# 2. Launch two containers on the network
docker run -d --name app1 --net my-net alpine sleep 3600
docker run -d --name app2 --net my-net alpine sleep 3600

# 3. Test embedded DNS resolution (Container-to-Container by name)
docker exec -it app1 ping -c 2 app2

# 4. Inspect bridge allocation and container IPs
docker network inspect my-net

# 5. View the host bridge and active NAT packet routing rules
ip link show type bridge
sudo iptables -t nat -L -n -v

STATE FILE UPDATE

  • Current Topic: Container & Docker Networking Fundamentals (Kernel Primitives & Packet Flow)
  • Mastery Focus: Linux Network Namespaces, veth pairs, Software Bridges, Port NAT, and DNS Resolution.
  • Next Target: Deep-dive into Pod networking architecture (pause container) or Multi-Node CNI routing (Overlay vs. Direct Routing).

To continue tailoring this to your goals: Would you like to inspect how a Kubernetes Pod builds on the container:<id> shared namespace model, or explore how multi-node CNI routing (VXLAN/Overlay vs. Direct Routing/BGP) scales this model across hosts?

Here is a structured, step-by-step hands-on learning lab designed to let you build, trace, and inspect all three primary communication patterns in Docker before bridging these concepts directly to Kubernetes.


Architectural Network Topology

Docker Network Topology


Step 0: Lab Environment Setup

We will create a custom user-defined bridge network (lab-net) and deploy two containers:

  1. web-server: An NGINX server listening on port 80, mapped to host port 8080.
  2. client-box: A networking diagnostics toolbox container (nicolaka/netshoot) equipped with curl, ip, traceroute, and tcpdump.

Run the following commands in your Linux/Docker terminal:

# 1. Create a custom bridge network with a fixed subnet
docker network create --subnet 172.20.0.0/16 --driver bridge docker-lab-net

# 2. Launch the web server container
docker run -d --name web-server --net docker-lab-net -p 8080:80 nginx:alpine

# 3. Launch the client diagnostics container
docker run -d --name client-box --net docker-lab-net nicolaka/netshoot sleep 3600

Flow 1: Container-to-Container Communication (East-West)

  • Objective: Observe Layer 2 software bridge frame switching, ARP resolution, and Docker's embedded DNS resolver (127.0.0.11).
# 1. Test DNS name resolution and ping inside the isolated network
docker exec -it client-box ping -c 2 web-server

# 2. Perform an internal HTTP request using the container name
docker exec -it client-box curl -I http://web-server:80

# 3. Inspect the ARP cache inside the client container
docker exec -it client-box ip neigh

What's happening under the hood

  • client-box sends a DNS query to Docker’s embedded DNS server (127.0.0.11), which returns 172.20.0.2.
  • Traffic leaves client-box's eth0, traverses its veth pair onto the host Linux software bridge, and is switched directly to web-server's veth pair without hitting host NAT tables.

Flow 2: Container-to-External Communication (Egress / North-South Outbound)

  • Objective: Trace outbound internet connectivity and inspect Source NAT (SNAT / MASQUERADE) performed by the host kernel.
# 1. Issue an outbound HTTPS request to an external public endpoint
docker exec -it client-box curl -I https://www.google.com

# 2. Trace the network hops leaving the container
docker exec -it client-box traceroute -n -m 3 8.8.8.8

# 3. (On the host terminal) Inspect the iptables MASQUERADE rule
sudo iptables -t nat -L POSTROUTING -n -v | grep 172.20.0.0

What's happening under the hood

  • The container routes non-local packets to its default gateway (172.20.0.1, the bridge IP).
  • The host kernel applies an iptables SNAT/MASQUERADE rule, rewriting the source IP from 172.20.0.3 to the host’s primary network interface IP before transmitting the packet to the external gateway.

Flow 3: External-to-Container Communication (Ingress / Inbound Port Forwarding)

  • Objective: Trace inbound host port binding and Destination NAT (DNAT) packet translation into the container namespace.
# 1. Access the container from your host terminal via the mapped port
curl -I http://localhost:8080

# 2. (On the host terminal) Inspect the listening port managed by docker-proxy
sudo ss -tulpn | grep 8080

# 3. (On the host terminal) Inspect the DNAT rule created by Docker
sudo iptables -t nat -L DOCKER -n -v | grep 8080

What's happening under the hood

  • Inbound packets hitting Host:8080 enter the PREROUTING chain.
  • Docker's iptables rule applies Destination NAT (DNAT), rewriting the destination from Host:8080 to 172.20.0.2:80.
  • The packet is switched across the bridge and delivered to the web-server container namespace.

Lab Cleanup

Once you have verified the outputs across all three flows:

docker rm -f web-server client-box
docker network rm lab-net

STATE FILE UPDATE

  • Current Topic: Docker Hands-On Networking Labs (3 Core Communication Flows)
  • Mastery Focus: Layer 2 Bridge switching & DNS, Outbound SNAT/MASQUERADE, Inbound DNAT port forwarding.
  • Next Target: Transitioning to Kubernetes Pod networking (pause container) and CNI cross-host routing architectures.

Run through these commands in your local Docker environment. Once you have seen the DNS resolution, iptables NAT tables, and traceroute hops in action, let me know if you are ready to transition to how Kubernetes replaces these Docker mechanisms with Pod pause containers and CNI plugins.