Sub topic 3
The headless Service that governs a StatefulSet
This chapter assumes you can apply YAML with kubectl, and that you know what a Pod, a Deployment, a namespace, and a normal ClusterIP Service are. You do not need to know StatefulSets yet beyond the previous chapter's overview.
Why a StatefulSet needs its own Service
A normal Service gives a group of Pods one virtual IP address. Clients connect to that address, and kube-proxy picks one Pod for each connection. This suits stateless replicas, because any replica can answer any request.
Stateful workloads often need the opposite. A database replica must reach the primary, not just any member. A client may need to talk to web-0 specifically. For that, each Pod needs its own DNS name that resolves to that Pod's IP.
A headless Service provides this. It is a Service with clusterIP: None. Kubernetes allocates no virtual IP and does no load balancing. Instead, DNS answers with the addresses of the Pods behind the Service.
What the headless Service does
A headless Service with a selector does two things:
- A lookup of the Service name, such as
web, returns the IP addresses of all ready Pods that match the selector. - Each Pod gets its own DNS record in the form
<pod-name>.<service-name>.<namespace>.svc.<cluster-domain>.
The second point is what gives a StatefulSet stable network identity. The Pod name comes from the StatefulSet and its ordinal, so web-0 always has the same DNS name, even after it is rescheduled and gets a new IP.
The serviceName field
A StatefulSet is tied to its Service through spec.serviceName. This Service is called the governing Service. The Pod DNS records use the value of this field as the Service part of the name.
apiVersion: v1
kind: Service
metadata:
name: web
labels:
app: web
spec:
clusterIP: None
selector:
app: web
ports:
- name: web
port: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: web
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- name: web
containerPort: 80
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1GiThree connections make this work:
- The Service
selector(app: web) matches the Pod template labels. - The StatefulSet
serviceName(web) matches the Servicemetadata.name. - The StatefulSet
selectoralso matches the Pod template labels.
If the names in step 2 differ, the Pods still run, but the per-Pod DNS records do not appear under the name you expect.
Try it
Apply the manifest, watch the Pods start, and resolve web-0 from a temporary Pod in the same namespace.
kubectl apply -f web.yaml
kubectl get pods -l app=web -w
kubectl get pvc -l app=web
kubectl run -it --rm dns-test --image=busybox:1.36 --restart=Never -- nslookup web-0.webThe short name web-0.web works because the test Pod's DNS search path includes its own namespace. From another namespace, use web-0.web.<namespace>, or the full form ending in .svc.cluster.local.
Try nslookup web as well. You should see one address for each ready Pod, and the order can change between lookups.
Common errors
- No
clusterIP: None. With a normal ClusterIP Service,webresolves to one virtual IP. The per-Pod names are not created. - Selector mismatch. If the Service selector matches no Pods, the lookup returns nothing.
- Missing Service. Kubernetes does not check that the governing Service exists when you create the StatefulSet. You must create it yourself, and it should exist first.
- Not-ready Pods. By default, a Pod's DNS record appears only when the Pod is ready. A Pod that must be found by its peers before it is ready, for example during cluster bootstrap, needs
publishNotReadyAddresses: trueon the Service.
Exercise
A StatefulSet named db in namespace shop has serviceName: db-hs. What is the full DNS name of its second Pod, assuming the default cluster domain? What happens to this name if that Pod is deleted and recreated on another node?
Show answer
The second Pod is db-1, so the name is db-1.db-hs.shop.svc.cluster.local. After the Pod is recreated, the name stays the same because the Pod name is the same. Only the IP address it resolves to changes.