Difficulty: Advanced
What is a Headless Service in Kubernetes, how does its DNS behave, and when would you use one?
A Headless Service is a Service with clusterIP: None. It gets no virtual IP and kube-proxy does no load balancing for it; instead, a DNS lookup of the Service name returns the IP addresses of the individual ready Pods. Combined with a StatefulSet, it also gives each Pod a stable DNS name like postgres-0.postgres.db.svc.cluster.local.
A regular ClusterIP Service gives clients one stable virtual IP. DNS for api.shop.svc.cluster.local returns that IP, and kube-proxy (iptables, IPVS or an eBPF dataplane like Cilium) spreads connections across Pods. The client never knows which Pod it reached.
With a headless Service:
| ClusterIP Service | Headless Service | |
|---|---|---|
clusterIP | Assigned virtual IP | None |
| DNS answer | One virtual IP | Every ready Pod IP |
| Load balancing | kube-proxy, per connection | Client side |
| Stable per-Pod names | No | Yes, with a StatefulSet |
Databases and clustered systems (PostgreSQL with replicas, Kafka, Cassandra, Elasticsearch, ZooKeeper) need to address specific members: "write to the primary at postgres-0", "replicate from kafka-2". A StatefulSet sets serviceName to a headless Service, and each Pod gets a DNS record of the form <pod-name>.<service>.<namespace>.svc.cluster.local. The name survives Pod restarts even though the IP changes.
round_robin policy with a dns:/// target).publishNotReadyAddresses: true when members must discover each other before they pass readiness, which is common during cluster bootstrap.port/targetPort remapping does not apply; clients connect to the Pod's container port directly.apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: db
spec:
clusterIP: None # this makes it headless
selector:
app: postgres
ports:
- name: pg
port: 5432
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
namespace: db
spec:
serviceName: postgres # gives Pods stable DNS names
replicas: 2
selector:
matchLabels: { app: postgres }
template:
metadata:
labels: { app: postgres }
spec:
containers:
- name: postgres
image: postgres:16
ports:
- containerPort: 5432
Pods become postgres-0.postgres.db.svc.cluster.local and postgres-1.postgres.db.svc.cluster.local, so clients can target the primary by name.
kubectl run dnstest -it --rm --image=busybox:1.36 -- sh
# inside the Pod:
nslookup postgres.db.svc.cluster.local
# Address: 10.244.1.12
# Address: 10.244.2.7
nslookup postgres-0.postgres.db.svc.cluster.local
# Address: 10.244.1.12
A headless Service returns one address per ready Pod, and per-Pod names resolve to exactly one IP.
Headless Service, clusterIP: None, DNS, StatefulSet, Client-side Load Balancing