Learn Labs
State & Security

StatefulSets & Volumes

Stable identities, PersistentVolumeClaims, and volume topology

Deployment pods are interchangeable. A database's aren't: each replica owns its own data and has its own identity. StatefulSets give pods a stable name (redis-0), a stable DNS name (redis-0.redis), ordered startup and shutdown, and a PersistentVolumeClaim per pod that follows the pod through restarts.

kind: StatefulSetmetadata: { name: redis }spec:serviceName: redisthe headless Service: redis-0.redis.shop.svcreplicas: 1template: ...redis:8.8.3 --appendonly yes, runs as uid 999volumeClaimTemplates:one PVC per pod: data-redis-0, data-redis-1, ...- metadata: { name: data }spec:accessModes: ["ReadWriteOnce"]resources: { requests: { storage: 1Gi } }

Data survives the pod

./scripts/lab.sh statefulset.py
PVC data-redis-0 -> pvc-6f6e2d1c-... (standard, 1Gi)
 the volume lives on node ['lab-worker3']: local-path storage is a directory on that node

visits counter: 3
deleted redis-0 -> recreated as redis-0 on lab-worker3 in 1.5s
visits counter after: 4  (same PVC, data kept)

The new pod has the same name and mounts the same claim, so the counter continued. A Deployment's replacement pod would have had a new name and an empty emptyDir.

The volume decides where the pod can run

cordon lab-worker3 and delete redis-0 again: where can it go?
 15 s later: phase=Pending
 PodScheduled=False: 0/4 nodes are available: 1 node(s) had untolerated taint(s),
   1 node(s) were unschedulable, 2 node(s) didn't match PersistentVolume's node affinity.
 /api/visits during this: Server error '503 Service Unavailable'
uncordoned -> redis-0 Running again, visits: 5
boundonly hereredis-0PendingPVC data-redis-0PV (local-path)nodeAffinity: lab-worker3lab-worker3cordonedlab-workerno volume herelab-worker2no volume here0/4 nodes available: 1 unschedulable, 2 did not match the PersistentVolume's node affinity

kind's default StorageClass (standard, the local-path provisioner) creates each volume as a directory on one node, and the PersistentVolume carries a node affinity for that node. With the node cordoned, the pod had nowhere to go: the control plane is tainted, the node is unschedulable, and the other two nodes don't have the data. Redis was down until the node came back.

Cloud block storage (EBS, Persistent Disk) is the same one level up: volumes are zonal. A pod whose volume is in zone-a can only run in zone-a. Lose the zone, lose the volume's availability.

Other rules worth knowing

  • Deleting a StatefulSet (or scaling down) keeps the PVCs, on purpose. Data is deleted only when you delete the claims (or set a persistentVolumeClaimRetentionPolicy).
  • A StatefulSet never runs two pods with the same identity. If redis-0's node becomes unreachable, the old pod stays "terminating" (see Node Failure) and no new redis-0 starts until the node returns or someone force-deletes the old pod, after confirming the node is really gone.
  • ReadWriteOnce means one node can mount the volume, not one pod.

Lab

  1. Run statefulset.py. Then scale Redis to 2. What is redis-1's data? Is it a replica of redis-0?
  2. Delete the StatefulSet (not the PVC), re-apply the manifests, and check the visits counter.
  3. Stop the node running redis-0 and recover Redis before the node comes back. What did you have to delete, and what did you lose?

On this page