Headless Service

A StatefulSet replica comes back with a new IP, yet once ready, clients still reach it by the same DNS name. What does a Service with clusterIP None give instead of a virtual IP, and who picks between the replicas?

Play the animation 6 steps, animated, plays on a computer or a tablet

Headless Service, Kubernetes diagram

Headless Service, step by step

  1. StatefulSet web runs three Pods behind Service nginx, whose clusterIP is None. There is no virtual IP, so kube-proxy writes no rules for it. Under the default ClusterFirst DNS policy the lookup of nginx.default.svc.cluster.local still goes to CoreDNS through the kube-dns Service.

    Play from step 1
  2. For a headless Service with a selector the control plane still keeps an EndpointSlice, and CoreDNS answers from it: one A record for every ready Pod, three addresses instead of one virtual IP. The platform does no load balancing, and the whole set comes back for the client to choose from.

    Play from step 2
  3. The client connects straight to one address from that set, here web-1 at 10.244.3.4. No ClusterIP is in the path and no Service rule rewrites the destination, so the client chooses from the set itself and kube-proxy has no say.

    Play from step 3
  4. Pod web-2 fails its readiness probe. Its endpoint turns ready=false in the EndpointSlice and CoreDNS stops publishing that endpoint, so the same lookup now returns two addresses. A Service with publishNotReadyAddresses set would keep web-2 in the answer.

    Play from step 4
  5. Each Pod also gets a name of its own, the Pod name under the governing Service that the StatefulSet serviceName points at. For web-0 that is web-0.nginx.default.svc.cluster.local. That lookup returns web-0 alone, which lets a client reach one specific replica, such as a primary.

    Play from step 5
  6. When web-0 is deleted, the StatefulSet recreates it under the same name, here with a new IP. Once it is ready the same lookup returns that new address, 10.244.3.8. A client that cached the answer can hold the old one until the record TTL expires, 30 seconds under kubeadm.

    Play from step 6

Sources