Headless Service

Difficulty: Advanced

Question

What is a Headless Service in Kubernetes, how does its DNS behave, and when would you use one?

Answer

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.

Normal Service vs headless Service

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:

  • DNS returns multiple A/AAAA records, one per ready Pod matched by the selector.
  • No kube-proxy rules exist for it, so there is no virtual IP to connect to.
  • The client decides which Pod to talk to.
ClusterIP ServiceHeadless Service
clusterIPAssigned virtual IPNone
DNS answerOne virtual IPEvery ready Pod IP
Load balancingkube-proxy, per connectionClient side
Stable per-Pod namesNoYes, with a StatefulSet

Why StatefulSets need it

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.

Other uses

  • gRPC and HTTP/2 clients: these keep long-lived connections, so a ClusterIP Service pins all requests from one client to one Pod. With a headless Service, the gRPC client resolves all Pod IPs and balances requests itself (for example using the round_robin policy with a dns:/// target).
  • Peer discovery: apps that need to find all their peers at startup (cluster membership, cache rings) just do a DNS lookup.
  • Not-ready peers: set publishNotReadyAddresses: true when members must discover each other before they pass readiness, which is common during cluster bootstrap.

Gotchas

  • DNS results are cached by clients and runtimes (the JVM has its own DNS cache), so new Pods may not be seen immediately. Use short TTLs or periodic re-resolution.
  • A headless Service without a selector creates no endpoints automatically; you manage EndpointSlices yourself.
  • Because there is no proxy, Service port/targetPort remapping does not apply; clients connect to the Pod's container port directly.
DNS lookup for a headless Service. The client gets every Pod IP and picks one itself, instead of going through a virtual IP.
  1. Client Pod → CoreDNS: Resolve postgres.db.svc.cluster.local
  2. CoreDNS → Client Pod: A records for both Pod IPs
  3. Client Pod → CoreDNS: Resolve postgres-0.postgres.db.svc...
  4. CoreDNS → Client Pod: IP of postgres-0 only
  5. Client Pod → postgres-0: Write to primary directly
  6. Client Pod → postgres-1: Read from replica directly

Code examples

Headless Service backing a StatefulSet

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.

Seeing the DNS difference

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.

Key points

  • Headless = clusterIP: None; no virtual IP and no kube-proxy load balancing.
  • DNS returns all ready Pod IPs; the client chooses.
  • StatefulSet serviceName + headless Service = stable per-Pod DNS names.
  • Useful for databases, Kafka, peer discovery and gRPC client-side balancing.
  • publishNotReadyAddresses lets peers discover each other before they are Ready.

Common follow-up questions

  • Why does gRPC load balancing behave badly behind a normal ClusterIP Service?
  • What DNS name does a StatefulSet Pod get?
  • How does kube-proxy implement a normal ClusterIP Service?
  • What is an ExternalName Service and how is it different?

Interview tips

  • Explain it through the DNS answer: one virtual IP vs a list of Pod IPs.
  • Give the StatefulSet DNS name format from memory; it is a strong signal.
  • Mention the gRPC long-lived connection problem as a non-database use case.

Concepts covered

Headless Service, clusterIP: None, DNS, StatefulSet, Client-side Load Balancing

More Kubernetes & Orchestration questions