Network
A request finds your Pod
Load balancer to container
Follow the first packet of one HTTP request from a visitor's laptop to your container and back. On the way in its addresses are rewritten three times and it's wrapped inside another packet once. On the way out every change is undone, so the visitor never sees any of it.
- Service
- web · type LoadBalancer · port 80 → 8080
- NodePort
- 30080 on every node
- Endpoints
- 10.244.1.12, 10.244.2.17, 10.244.2.18
- Network
- flannel with VXLAN · kube-proxy in iptables mode
Read the whole lesson as text
1. Find the address
The visitor's browser needs an IP for shop.example.com, so it asks a DNS resolver. The answer is 203.0.113.50. That address doesn't belong to any node or Pod: it's the cloud load balancer that Kubernetes created for the web Service.
- The visitor's browser asks a DNS server for the IP of shop.example.com.
- The answer is 203.0.113.50: the cloud load balancer.
2. To the load balancer
The browser opens a TCP connection to 203.0.113.50 on port 80. We follow the very first packet, the SYN, because it's the one that sets up every rewrite on the way. Once the handshake is done, the GET request follows exactly the same path.
- The browser sends the first packet of a connection to the load balancer.
3. Onto a node
The load balancer doesn't know about Pods. It knows the nodes, and it health-checks the Service's NodePort, 30080, on each of them. It picks node-1 and rewrites the packet's destination to 10.0.1.11:30080. This load balancer keeps the visitor's address as the source.
- The load balancer checks which nodes are healthy and picks node-1.
- It forwards the packet to node-1, on the Service's NodePort, 30080.
4. kube-proxy's rules
Zoom into node-1. kube-proxy never touches this packet: it only wrote iptables rules ahead of time, from the Service and its EndpointSlices. The kernel walks those rules, picks one of the three web Pods at random, and rewrites the destination to that Pod's IP. Because the Pod might be on another node, it also rewrites the source, so the reply has to come back through node-1.
- On node-1, the kernel's iptables rules recognise port 30080 as the web Service.
- The rules pick one of the three web Pods at random: 10.244.2.17, on node-2.
- The destination is rewritten from node-1:30080 to the Pod's IP and port.
- The source is rewritten too, so the reply will come back through node-1.
5. Across nodes
10.244.2.17 lives on node-2, and the network between nodes knows nothing about Pod IPs. So flannel wraps the whole packet inside a UDP packet addressed from node-1 to node-2, port 8472. To the network it's ordinary node-to-node traffic.
- The Pod lives on node-2, so flannel prepares to wrap the packet for the trip.
- The packet travels inside a UDP packet from node-1 to node-2.
6. Into the Pod
Zoom into node-2. The packet arrives on eth0 as UDP port 8472, which the kernel hands to flannel.1 to unwrap. The inner packet is for 10.244.2.17, and the route for 10.244.2.0/24 points at the cni0 bridge. The bridge forwards it out of the port that leads to the Pod: one end of a veth pair whose other end is the Pod's eth0.
- node-2 unwraps it, revealing the original packet for 10.244.2.17.
- node-2 routes it to the bridge where its Pods are plugged in.
- The bridge hands it down the virtual cable that leads into the Pod.
7. The app answers
Zoom into the Pod. The packet comes through the veth pair to eth0, the Pod's only interface. The web container shares this network namespace with the pause container, so it sees eth0 as its own. It accepts the connection, reads GET / once the handshake is done, and answers 200 OK.
- The packet comes out on the Pod's own network card, eth0.
- The app accepts the connection. It sees node-1's address, not the visitor's.
- The app sends back the page: 200 OK.
8. Back to the visitor
The reply is addressed to 10.244.1.0, so it goes back across the VXLAN tunnel to node-1. There, conntrack finds the entry it made for the SYN and reverses both rewrites. The load balancer swaps its own address back in, and the visitor receives a reply from 203.0.113.50:80, exactly where it sent the request.
- The reply travels back to node-1, wrapped in UDP again.
- node-1 remembers the rewrites it made, and undoes both of them.
- The reply leaves node-1 for the load balancer.
- The load balancer puts its own address back. The visitor gets the page.
Summary
Three rewrites on the way in (load balancer to NodePort, NodePort to Pod, visitor to node-1) and one wrap to cross nodes. conntrack and the load balancer undid every one on the way out. Almost all of the 62 ms was the internet and the app; the cluster's own hops took well under a millisecond.
Next lesson: Rolling update