Kubernetes networking
About this lesson
Every Pod in Kubernetes gets its own IP and can reach every other Pod directly, with no address translation. But Pods are replaced all the time, so their IPs keep changing. A Service fixes that with one stable virtual IP and a DNS name. Follow one request from a Python client: DNS turns api into the Service's ClusterIP, kube-proxy's rules on the node rewrite it to a ready Pod, and the reply comes back looking as if the Service answered.
Every Pod gets an IP
A Pod is the smallest thing Kubernetes runs: one or more containers that share one network address. Each Pod gets its own IP. Pods on node 1 get 10.244.1.x and Pods on node 2 get 10.244.2.x. Containers inside one Pod talk over localhost.
cluster Pod network 10.244.0.0/16 node-1 10.244.1.0/24 node-2 10.244.2.0/24 web Pod 10.244.1.3 api Pod 1 10.244.1.4 api Pod 2 10.244.2.5 containers in one Pod → localhost
Read the lesson step by step
Every Pod gets an IP
A Pod is the smallest thing Kubernetes runs: one or more containers that share one network address. Each Pod gets its own IP. Pods on node 1 get 10.244.1.x and Pods on node 2 get 10.244.2.x. Containers inside one Pod talk over localhost.
cluster Pod network 10.244.0.0/16 node-1 10.244.1.0/24 node-2 10.244.2.0/24 web Pod 10.244.1.3 api Pod 1 10.244.1.4 api Pod 2 10.244.2.5 containers in one Pod → localhostPod to Pod, no NAT
Any Pod can reach any other Pod at its IP, even on another node, with no NAT (address rewriting) on the way. Kubernetes requires this; a network plugin (CNI) builds it. Here web sends straight to api Pod 2 at 10.244.2.5.
web 10.244.1.3 → api Pod 2 10.244.2.5 node-1 → node-2, address unchanged the Kubernetes network model: every Pod has its own IP every Pod reaches every Pod, no NAT built by a CNI plugin (Flannel, Calico, Cilium…)Pods come and go
Pods are disposable. When api Pod 2 is deleted, its node fails, or it is updated, a fresh Pod replaces it and gets a new IP: 10.244.2.5 becomes 10.244.2.6. Code that saved the old address now fails. Clients need a name that stays put.
api Pod 2 10.244.2.5 ✗ deleted new Pod 10.244.2.6 ✓ running web still sends to 10.244.2.5 → no answer Pod IPs are not stable: never hard-code themA Service: one stable address
A Service is a fixed front door for a group of Pods. This one, named api, picks every Pod labelled app: api. It gets a virtual IP, its ClusterIP (here 10.96.0.20), which stays the same for as long as the Service exists.
kubectl apply -n shop -f api-svc.yaml Service api ClusterIP 10.96.0.20 (from 10.96.0.0/12) selector app: api → api Pod 1, api Pod 2 port 80 → targetPort 8080 on each Pod type: ClusterIP (the default, inside the cluster only)EndpointSlice: who is ready
Kubernetes keeps a live list of the ready Pods behind each Service, called an EndpointSlice. When a Pod is replaced, or fails its readiness check, the list updates by itself. Nobody edits it by hand.
EndpointSlice for Service api 10.244.1.4:8080 ready 10.244.2.6:8080 ready Pod replaced or not ready → list changes kept up to date by the control planeDNS turns api into an IP
Line 2 asks for http://api/items. Inside a Pod, the short name api expands to api.shop.svc.cluster.local. The cluster's DNS server, CoreDNS, answers with the Service's ClusterIP, 10.96.0.20.
urlopen("http://api/items") /etc/resolv.conf search: shop.svc.cluster.local … api → api.shop.svc.cluster.local CoreDNS: api.shop.svc.cluster.local = 10.96.0.20Send to the ClusterIP
The web Pod sends its request to 10.96.0.20, port 80. No machine owns that address: it is virtual, and nothing listens on it. Before the packet leaves node 1, the node's own packet rules catch it.
packet 10.244.1.3 → 10.96.0.20:80 the ClusterIP is virtual: no Pod, no machine node-1 rules match it on the way outkube-proxy picks a Pod
kube-proxy, a helper on every node, turned the EndpointSlice into these rules. They pick one ready Pod at random and rewrite the destination: 10.96.0.20:80 becomes 10.244.2.6:8080. This rewrite is called DNAT.
kube-proxy (iptables mode, the Linux default) EndpointSlice → rules for 10.96.0.20:80 pick one ready endpoint at random DNAT 10.96.0.20:80 → 10.244.2.6:8080Delivered, and back
Now it is a plain Pod-to-Pod packet, so it crosses to node 2 and api Pod 2 answers. On the way back the rules undo the rewrite, so web sees a reply from 10.96.0.20. Line 3 prints 200.
10.244.1.3 → 10.244.2.6:8080 (Pod to Pod) reply 10.244.2.6 → rules → web source rewritten back to 10.96.0.20:80 print(r.status) → 200The next call may differ
Each new connection is a fresh random pick, so the load spreads over both Pods. Start a third Pod labelled app: api and it joins the EndpointSlice: the Service grows with no change to the code in web.
new connection → 10.96.0.20:80 this time → 10.244.1.4:8080 (api Pod 1) more Pods with app: api → more endpoints web's code never changes
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What does the Kubernetes network model promise about Pods?
Each Pod gets its own cluster-wide IP, and any Pod can reach any other Pod at that IP, even on another node, with no address translation. A CNI network plugin builds this network.
02Why should a client not save a Pod's IP address and call it forever?
When a Pod is deleted, evicted, rescheduled or updated, a fresh Pod takes its place with a new IP. A Service gives clients a stable address instead.
03The Service api has ClusterIP 10.96.0.20. What happens to a packet sent to 10.96.0.20:80?
The ClusterIP is virtual: no machine owns it. kube-proxy has turned the EndpointSlice into rules on every node, and those rules rewrite (DNAT) the destination to one ready Pod's IP and targetPort.
04In namespace shop, the Python code opens http://api/items. How does the name api become an address?
Kubernetes sets each Pod's DNS search list, starting with <namespace>.svc.cluster.local, so the short name works inside the same namespace. CoreDNS returns the ClusterIP.
05What does an EndpointSlice hold?
The control plane keeps an EndpointSlice for each Service with a selector, listing the matching Pods' IPs and ports with a ready condition. kube-proxy only sends traffic to ready endpoints.
Glossary
- Pod
- The smallest thing Kubernetes runs: one or more containers that share one network address and can reach each other over localhost.
- CNI plugin
- A network plugin (Container Network Interface) such as Flannel, Calico or Cilium that gives Pods their IPs and connects them, so any Pod can reach any other.
- NAT
- Network address translation: rewriting the addresses on a packet as it passes. Pod-to-Pod traffic needs none; a Service uses a rewrite called DNAT.
- Service
- A stable front door for a group of Pods, chosen by a label selector, with a fixed virtual IP and DNS name.
- ClusterIP
- The virtual IP of a Service, reachable only inside the cluster. ClusterIP is also the default Service type.
- EndpointSlice
- A list, kept up to date by Kubernetes, of the Pod addresses behind a Service and whether each one is ready.
- kube-proxy
- A helper on every node that turns Services and EndpointSlices into packet rules (iptables, ipvs or nftables) that send ClusterIP traffic to a ready Pod.
- DNAT
- Destination NAT: rewriting a packet's destination address, here from the ClusterIP to one Pod's IP and port.
Notes and sources
- The Kubernetes network model gives every Pod its own cluster-wide IP and lets Pods reach each other without NAT. Kubernetes does not build that network itself: a CNI network plugin such as Flannel, Calico or Cilium does.
- The address ranges are common defaults, not rules: 10.244.0.0/16 is Flannel's default Pod network (one /24 per node) and 10.96.0.0/12 is kubeadm's default Service range. Your cluster may use others.
- Real Pod names carry a random suffix (for example api-7c9d8-x2kq4), and a replacement Pod gets a new name as well as a new IP. The figure calls them api Pod 1 and api Pod 2 to keep them readable.
- iptables is the default kube-proxy mode on Linux today; it can also run in nftables mode (its planned successor) or the older ipvs mode (deprecated since Kubernetes v1.35); some network plugins, such as Cilium, replace kube-proxy with their own rules. The idea is the same: a packet for the ClusterIP is rewritten to one ready Pod.
- A ClusterIP is reachable only inside the cluster. To let outside traffic in, use a Service of type NodePort or LoadBalancer, or an Ingress or Gateway in front of the Service.
- Strictly, the Pod does not ask the CoreDNS Pod directly: its /etc/resolv.conf names the kube-dns Service ClusterIP (10.96.0.10 on a default kubeadm cluster), and kube-proxy rewrites (DNATs) that address to a CoreDNS Pod, the same trick this figure shows for api.
- A crashed container is not a new Pod: the kubelet restarts it inside the same Pod (restartPolicy Always by default), and the Pod keeps its IP. A new Pod, with a new IP, appears when the old one is deleted, evicted, lost with its node, or replaced in a rollout.
- The Python runs inside a Pod in namespace shop of such a cluster; outside one, the name api does not resolve. The YAML is applied with kubectl apply -n shop -f api-svc.yaml; its type defaults to ClusterIP.
- Kubernetes: Services, Load Balancing, and Networking (the network model)
- Kubernetes: Cluster Networking
- Kubernetes: Service
- Kubernetes: EndpointSlices
- Kubernetes: Virtual IPs and Service Proxies
- Kubernetes: DNS for Services and Pods
- Kubernetes: Using CoreDNS for Service Discovery
- Kubernetes: Customizing DNS Service
- Kubernetes: Pod Lifecycle (container restarts)
- Kubernetes: kubeadm init (default --service-cidr)
- Flannel: kube-flannel.yml (default Pod network 10.244.0.0/16)
- Python docs: urllib.request.urlopen
Quiz Unlocks after the last step.