kubernetes headless service, talking to pods directly
A normal Service gives you one virtual cluster IP and load balances the traffic across the pods behind it. That is what you want almost every time. Almost, because sometimes each pod needs to be reachable on its own, either to talk to specific peers or because the client wants to pick a pod itself. For that case kubernetes has headless services.
What makes a service headless
Nothing more than one line. Compare the two:
# normal service, gets a cluster IP
apiVersion: v1
kind: Service
metadata:
name: db
spec:
selector:
app: db
ports:
- port: 5432
---
# headless service, no cluster IP
apiVersion: v1
kind: Service
metadata:
name: db-headless
spec:
clusterIP: None
selector:
app: db
ports:
- port: 5432
clusterIP: None is the whole trick. The value must be exactly None, not
an empty string and not "null".
How DNS behaves
With a normal service, a DNS lookup for db.default.svc.cluster.local
returns one address, the cluster IP, and kube-proxy does the balancing.
With a headless service the same lookup returns the pod IPs directly, one record per ready pod:
$ nslookup db-headless.default.svc.cluster.local
Name: db-headless.default.svc.cluster.local
Address: 10.244.0.15
Name: db-headless.default.svc.cluster.local
Address: 10.244.0.17
So the service still exists as a DNS name and still tracks its endpoints through the selector, it just refuses to sit in front of them as a proxy. The client gets the full list and decides by itself which pod to use.
Stable names with StatefulSet
The classic pairing is a headless service plus a StatefulSet. Each pod then gets its own DNS name based on its stable ordinal:
db-0.db-headless.default.svc.cluster.local
db-1.db-headless.default.svc.cluster.local
This is what database clusters and things like kafka or elasticsearch need, members must reach eachother under names that survive restarts. A regular Deployment with random pod names cannot give you that.
One option worth knowing here is publishNotReadyAddresses: true on the
service. During startup the cluster members are often not ready yet, but the
peers still need to find them to form the cluster. Without this flag the
not-ready pods are missing from DNS and the bootstrap can deadlock.
When I reach for it
- Any stateful workload with peer discovery, databases, brokers, search clusters.
- When a client library wants the list of all backends and does its own load balancing or wants to keep long connections to one specific pod.
- When I need to debug and want to hit one exact pod,
db-1instead of “who ever answers”.
For a regular stateless web app a normal Service is still the right default, there is no reason to give up the free load balancing.
Notes
clusterIP: Noneis the only required difference, the rest of the service spec looks normal.- DNS returns one A record per ready pod, so most clients recieve the full list and the resolver order decides which one is tried first.
- Session affinity and kube-proxy rules mostly stop mattering, traffic no longer goes through the cluster IP at all.
- If pods are missing from the DNS answers, check readiness probes first,
then
publishNotReadyAddressesif the cluster is still bootstrapping.
Headless services look niche, but they are the piece that makes stateful workloads behave in kubernetes, and once the DNS behaviour is clear the rest is just yaml.
- [ ] Ref: https://kubernetes.io/docs/concepts/services-networking/service/#headless-services
- [ ] Ref: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/
- [ ] Ref: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/