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.
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: 5kind'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.
Kubernetes can schedule stateful pods; it can't make one copy of data
highly available. Use the database's own replication (Redis Sentinel or
Cluster, Postgres streaming replication, Kafka replication) across
zones, through an operator (CloudNativePG, Strimzi, the Redis operators),
or use a managed database. And set volumeBindingMode: WaitForFirstConsumer on StorageClasses, so a volume is created in the
zone where its pod gets scheduled, not before.
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 newredis-0starts until the node returns or someone force-deletes the old pod, after confirming the node is really gone. ReadWriteOncemeans one node can mount the volume, not one pod.
Lab
- Run
statefulset.py. Then scale Redis to 2. What isredis-1's data? Is it a replica ofredis-0? - Delete the StatefulSet (not the PVC), re-apply the manifests, and check the visits counter.
- Stop the node running
redis-0and recover Redis before the node comes back. What did you have to delete, and what did you lose?