Sub topic 4
Stable names and DNS for each Pod
This chapter assumes you can apply YAML manifests with kubectl and know what a Pod, a Service, and a namespace are. It also assumes you have read the previous chapters on the StatefulSet spec and the headless Service that governs it.
Why identity matters
A Deployment names its Pods with random suffixes, such as web-7d9f8c5b6-x2kqp. When a Pod is replaced, it gets a new name and a new IP address. That is fine for stateless web servers. It is a problem for a database replica or a cluster member that other nodes must find again by name.
A StatefulSet gives each Pod an identity that survives rescheduling. That identity has three parts: an ordinal name, a hostname, and a DNS record.
Ordinal names
A StatefulSet named web with replicas: 3 creates Pods named web-0, web-1, and web-2. The number at the end is the ordinal, an index that starts at 0. The name is the StatefulSet name, a hyphen, and the ordinal.
If web-1 is deleted or its node fails, the replacement is also called web-1. It is a new Pod object, and its IP address may differ. The name and the claim www-web-1 stay the same.
The controller also adds the label statefulset.kubernetes.io/pod-name to each Pod, set to the Pod's name. This lets you build a Service that selects exactly one Pod.
Hostnames and per-Pod DNS
Inside each Pod, the hostname equals the Pod name. Run hostname in web-0 and it prints web-0.
The headless Service named in serviceName creates a DNS record for each Pod. The form is:
<pod-name>.<service-name>.<namespace>.svc.cluster.localFor the example from the spine, with the Service web in the default namespace, the names are:
web-0.web.default.svc.cluster.localweb-1.web.default.svc.cluster.localweb-2.web.default.svc.cluster.local
Each name resolves to the IP address of that single Pod. The Service name alone, web.default.svc.cluster.local, returns the addresses of all ready Pods, because a headless Service has no virtual IP.
Try it
Apply the manifests from the previous chapter, then resolve a single Pod from a temporary Pod in the same namespace:
kubectl apply -f web.yaml
kubectl get pods -l app=web -w
kubectl run -it --rm dns-test --image=busybox:1.36 --restart=Never -- nslookup web-0.webFrom the same namespace, the short form web-0.web works because the cluster's DNS search path adds the namespace and svc.cluster.local. From another namespace, use web-0.web.default or the full name.
Now check that the identity survives deletion:
kubectl get pod web-0 -o wide
kubectl delete pod web-0
kubectl get pods -l app=web -w
kubectl get pod web-0 -o wideThe new web-0 appears with the same name. Its IP address is often different. Clients that use the DNS name keep working. Clients that cached the old IP do not.
Common errors
| Symptom | Likely cause |
|---|---|
| `nslookup web-0.web` returns NXDOMAIN | `serviceName` does not match the Service name, or the Service selector does not match the Pod labels |
| `web.default.svc.cluster.local` returns one virtual IP | The Service is not headless. `clusterIP: None` is missing |
| Lookup works inside the namespace but not from another | The short name relies on the search path. Use the namespace-qualified name |
| Pod record is missing while the Pod starts | The Pod is not ready, so its address is not published unless `publishNotReadyAddresses: true` is set on the Service |
The last row matters for clustered software. Peers often need to find each other before they report ready. Setting publishNotReadyAddresses: true on the headless Service publishes records for Pods that are not yet ready.
What stays the same and what changes
| Property | Stable across rescheduling? |
|---|---|
| Pod name and ordinal | Yes |
| Hostname | Yes |
| Per-Pod DNS name | Yes |
| Claim name, such as `www-web-1` | Yes |
| Pod IP address | No |
| Node the Pod runs on | No, unless storage or affinity ties it down |
Exercise
A StatefulSet named db in the namespace prod uses the headless Service db-svc as its serviceName. What is the full DNS name of the second Pod, and why does it still resolve after that Pod is deleted and recreated?
Show answer
The second Pod has ordinal 1, so the full name is db-1.db-svc.prod.svc.cluster.local. The replacement Pod gets the same name, db-1, and the headless Service publishes the DNS record for that name again, pointing at the new Pod's IP address.
Exercise
You run nslookup web-0.web and get NXDOMAIN, but kubectl get pods shows web-0 is Running. Name two things to check.
Show answer
Check that the StatefulSet's serviceName equals the Service's name, and that the Service has clusterIP: None and a selector matching the Pod labels. Also confirm you are querying from the right namespace, or use the namespace-qualified name.