Running workloadsLesson 12 of 14
StatefulSets and databases
Apps like databases, where each pod has its own identity and data, are hard to run with a Deployment. Here we follow how a StatefulSet pins down names, order and storage. How a PVC becomes real storage is covered in How Storage Gets Attached (PVC·PV).
Pods0 / 3
PVCs0
Claimed capacity0Gi
RequestResponse / reportAppears in a later step
What you will learn
- Pod names are fixed ordinals, started one by one from 0
- A headless Service gives each pod a stable DNS name
- A pod that comes back reattaches to the same PVC
- PVCs remain after scaling down or deleting
Results by action
| Action | Pods | PVCs | What to check |
|---|---|---|---|
| Create | Created in order from db-0 | A data-db-N per pod | If an earlier pod never becomes Ready, the next one is not created |
Delete db-1 | Recreated with the same name | Reattached to the same data-db-1 | The data is intact |
| Scale 3 → 2 | db-2 is deleted | data-db-2 remains | Scaling back up returns that data |
| Delete the StatefulSet | All deleted | All remain by default | Leftover PVCs take up space |
Check the pods with kubectl get pods -l <selector> and PVCs with kubectl get pvc. Whether PVCs are deleted on scale-down or delete can be set with persistentVolumeClaimRetentionPolicy.
Try it yourself
kubectl get statefulset -A # StatefulSets and ready counts
kubectl get pods -o wide # Ordinal names db-0, db-1, db-2
kubectl get pvc # data-db-N, still there after scale-down
kubectl get svc <service> -o jsonpath='{.spec.clusterIP}' # None means headless
kubectl get sts <name> -o jsonpath='{.spec.persistentVolumeClaimRetentionPolicy}' # What happens to PVCs
kubectl run dns-test --rm -it --image=busybox:1.36 --restart=Never -- nslookup <pod>.<service> # Fixed DNS name (removed on exit)
Check yourself
Further reading: StatefulSets · Persistent volumes · Finding Services by Name (Service·DNS)