StatefulSets: Stable Identity and Ordered Startup
A Deployment's Pods are interchangeable and get random names. A StatefulSet's are numbered, start in order, keep their own volume, and get their own DNS name. Build one with a headless Service and per-Pod storage, delete a Pod to watch it come back as itself, then see the PVCs survive the StatefulSet's deletion.
Workloads Guide 28 of 103 Intermediate
- Kubernetes1.36.4
- Cluster4 nodes
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 40 min
PV names, UIDs and addresses are generated per run. The StorageClass here is local-path; any dynamic provisioner behaves the same for this exercise.
- StorageClasslocal-path (default)
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA1001 | 192.168.0.175 | Ubuntu 26.04 LTS | Control Plane Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE01 | 192.168.0.176 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE02 | 192.168.0.177 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE03 | 192.168.0.178 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a workload's Pods are not interchangeable - a database, a queue, anything where one replica owns its data. This matters because identity needs a name that resolves, so a StatefulSet is always two objects rather than one.
- pairing a StatefulSet with a headless Service so its Pods get DNS names, and resolving a per-Pod name through it
- seeing ordinal names and staggered ages rather than random suffixes created all at once
- getting one PersistentVolumeClaim per Pod without writing a single one
- deleting a Pod and watching it come back as itself, with its own volume
- finding the PVCs still Bound after the StatefulSet is gone, and cleaning them up separately
Before you start
- A cluster with a default StorageClass. Check with
kubectl get sc; without one,volumeClaimTemplatesproduce PVCs that stay Pending forever. - The Deployments guide. The contrast with a Deployment is the point of this one.
- Enough room for three small volumes. This uses 64Mi each.
-
Two objects, because identity needs a name to resolve
-
Numbered names and staggered ages
-
One volume per Pod, provisioned automatically
-
Per-Pod DNS from the headless Service
-
Delete a Pod and watch it come back as itself
-
The PVCs outlive the StatefulSet
-
Clean up, storage included