Sub topic 9
Headless Services and per-Pod DNS
This chapter assumes you know how a ClusterIP Service works, how a selector builds EndpointSlices, and how cluster DNS resolves <service>.<namespace>.svc.cluster.local. It also assumes you can apply manifests with kubectl.
What a headless Service does
A normal ClusterIP Service has one virtual IP. Clients connect to that IP, and kube-proxy picks a Pod. Some clients need the real Pod addresses instead. Examples are a database client that must find every replica, and a peer in a cluster that must contact a specific member.
A headless Service has no virtual IP. You create one by setting clusterIP: None.
apiVersion: v1
kind: Service
metadata:
name: web-headless
spec:
clusterIP: None
selector:
app: web
ports:
- name: http
port: 80
targetPort: httpThe selector still builds EndpointSlices from the app=web Pods. Because there is no virtual IP, kube-proxy does no work for this Service. DNS does the job instead.
What DNS returns
For a ClusterIP Service, the name web resolves to one A record: the virtual IP. For a headless Service, web-headless resolves to several A records, one for each ready Pod IP. The client chooses which one to use, and it usually tries them in the order it receives them.
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- nslookup web-headless.default.svc.cluster.localExpect one address per ready Pod. Pods that fail their readiness probe are left out, as with any Service. Clients that cache DNS answers can keep using a stale address, so set short caches or reconnect on failure.
Per-Pod DNS with StatefulSets
A headless Service can also give each Pod its own stable DNS name. This works when the Pods belong to a StatefulSet whose serviceName field names the headless Service. Each Pod then gets a name in this form:
<pod-name>.<service>.<namespace>.svc.cluster.localFor a StatefulSet named db that uses a headless Service named db, the first Pod is reachable as db-0.db.default.svc.cluster.local. If db-0 is rescheduled and gets a new IP, the name stays the same and points to the new address. This is how replicated databases let one member find another by identity.
A Deployment does not give you this. Its Pod names change on each restart, so no stable per-Pod name exists.
Choosing between the two
| Need | ClusterIP | Headless |
|---|---|---|
| One stable address and load balancing | Yes | No |
| Client sees every Pod IP | No | Yes |
| Stable name for each Pod (StatefulSet) | No | Yes |
| Handled by kube-proxy | Yes | No |
Common errors
- Expecting load balancing. DNS returns several addresses, but the client decides which to use. Some clients always pick the first.
- Naming a nonexistent Service in a StatefulSet's
serviceName. The per-Pod names then do not resolve. - Forgetting that unready Pods are not published, so a new Pod's name may not resolve until it passes its probe.
Exercise
A StatefulSet named kafka in the namespace data uses a headless Service named kafka-hs. What is the DNS name of its second Pod, and what does nslookup kafka-hs.data.svc.cluster.local return?
Show answer
The second Pod is kafka-1, so its name is kafka-1.kafka-hs.data.svc.cluster.local. The lookup of the Service name returns the IP addresses of all ready Pods behind it, not a single virtual IP.