Sub topic 2
Anatomy of a StatefulSet
This chapter assumes you can read and apply YAML manifests with kubectl, and that you know Pods, Deployments, Services, and namespaces. You don't need to know PersistentVolumes in detail yet. The chapter breaks a StatefulSet manifest into its parts and shows what each part does.
What a StatefulSet is
A StatefulSet is a workload object that manages a set of Pods and gives each Pod a sticky identity. A Deployment treats its Pods as interchangeable. A StatefulSet does not. Each Pod gets an ordinal number (0, 1, 2, and so on), a name built from that number, and its own storage. If a Pod is replaced, the new Pod keeps the same name and reattaches to the same storage.
The StatefulSet controller creates Pods directly. There is no ReplicaSet in between, which is a visible difference from a Deployment.
The manifest
The example below defines a headless Service and a three-replica StatefulSet that runs nginx. Save it as web.yaml.
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: 1GiThe fields that matter
The spec looks like a Deployment spec, with a few additions.
| Field | Purpose |
|---|---|
| `serviceName` | Names the headless Service that governs the Pods' network identity. |
| `replicas` | The number of Pods. Their ordinals run from 0 to `replicas - 1`. |
| `selector` | Chooses the Pods this StatefulSet owns. It must match the labels in the Pod template. |
| `template` | The Pod blueprint. Every Pod is created from it. |
| `volumeClaimTemplates` | A blueprint for one PersistentVolumeClaim per Pod. |
Two of these need extra attention.
serviceName must refer to a Service that already exists, or will exist, in the same namespace. Kubernetes does not create it for you. The Service in the manifest sets clusterIP: None, which makes it headless. A headless Service has no virtual IP. Instead, DNS returns the addresses of the individual Pods. The next chapter covers this in detail.
volumeClaimTemplates is what separates a StatefulSet from a Deployment with a shared volume. The controller creates a PersistentVolumeClaim from the template for each Pod. The claim is named <template name>-<pod name>, so here you get www-web-0, www-web-1, and www-web-2. The volumeMounts entry in the container refers to the template by its name, www.
What the controller creates
Apply the manifest and watch the Pods.
kubectl apply -f web.yaml
kubectl get pods -l app=web -w
kubectl get pvc -l app=webExpect to see web-0 start first. web-1 is created only after web-0 is Running and Ready, and web-2 follows web-1. The kubectl get pvc command lists three claims, one per Pod. A later chapter covers the ordering rules.
Common errors
- Pods stay Pending. The usual cause is a PersistentVolumeClaim that cannot bind. Check
kubectl get pvcandkubectl describe pvc www-web-0. A missing default StorageClass is a frequent reason. - The StatefulSet is rejected. The
selectormust match the labels intemplate.metadata.labels. If they differ, the API server refuses the object. - DNS names for the Pods do not resolve. The Service named in
serviceNameis missing, is not headless, or has a selector that does not match the Pod labels. - A change to
volumeClaimTemplatesis refused. Most fields of a StatefulSet spec are immutable after creation. Onlyreplicas,template,updateStrategy,persistentVolumeClaimRetentionPolicy, andminReadySecondscan be updated.
Exercise
Exercise
A StatefulSet named db has replicas: 2 and a volumeClaimTemplate named data. What are the names of the Pods and of the PersistentVolumeClaims? What happens to the claim data-db-1 if Pod db-1 is deleted?
Show answer
The Pods are db-0 and db-1. The claims are data-db-0 and data-db-1. When db-1 is deleted, the claim data-db-1 stays. The controller recreates a Pod named db-1, which mounts the same claim, so its data is still there.