Sub topic 1
The Kubernetes network model
Prerequisites
This chapter assumes you can:
- Read and apply YAML manifests with
kubectl. - Explain what a Pod, a Deployment and a Namespace are.
- Describe IP addresses, ports and DNS at a basic level.
- Describe what a container is.
The rules
Kubernetes does not build the network itself. It defines rules that any network implementation must satisfy:
- Every Pod gets its own IP address.
- Any Pod can reach any other Pod by that IP, with no NAT (network address translation) in between.
- Agents on a node, such as the kubelet, can reach all Pods on that node.
Because of rule 2, the address a Pod sees for itself is the same address other Pods use to reach it. Containers in the same Pod share one network namespace. They talk to each other over localhost and must not use the same port.
Who implements the rules
A CNI (Container Network Interface) plugin implements the model. When the kubelet starts a Pod, it calls the plugin. The plugin creates the network interface, assigns an address from the node's Pod CIDR range and sets up routes between nodes. Calico, Cilium and Flannel are common plugins. They differ in how they route traffic, using overlays or direct routing, and in whether they enforce NetworkPolicy. A later chapter covers them.
Seeing it in a cluster
Assume a Deployment with the label app=web is running. The commands below read one Pod's IP and request it from a temporary Pod.
kubectl get pods -o wide
POD_IP=$(kubectl get pod -l app=web -o jsonpath='{.items[0].status.podIP}')
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- wget -qO- "http://$POD_IP"The request works from any node, because the model guarantees Pod-to-Pod reachability.
Why Pod IPs are not stable endpoints
Pod IPs work for connectivity but not for addressing. A Pod's IP lasts only as long as that Pod. When a Pod is deleted, evicted or replaced during a rollout, its replacement gets a new IP. A Deployment with three replicas therefore has a changing set of addresses.
A client needs a name or address that stays the same while the Pods behind it change. The Service abstraction provides this, and the next chapters build on it.
What the model does not promise
- Open by default. Without a NetworkPolicy, every Pod can reach every other Pod. Restriction is opt-in.
- Cluster-internal addresses. Pod IPs are normally not routable from outside the cluster.
- No load balancing or discovery. Talking to a Pod IP reaches exactly one Pod.
Exercise
A teammate stores the IP of a database Pod in an application's ConfigMap. After a node drain, the application cannot connect. Why, and what does Kubernetes offer instead?
Show answer
Draining the node deleted the Pod, and the controller created a replacement with a new IP. The stored address no longer points to the database. Use a Service, which gives the Pods a stable virtual IP and DNS name.