App
StatefulSet with storage
Stable names, ordered starts
Why database Pods get names like db-0 and keep their own volume when rescheduled. Deploy Postgres as a StatefulSet and watch three things a Deployment can't give a database: stable identity, one disk per Pod, and ordered starts. Kill a Pod and it comes back with the same name, the same DNS record and the same data.
- Objects
- StatefulSet db · headless Service · 3 PVCs
- Names
- db-0 · db-1 · db-2 - DNS per Pod
- Claim template
- db-data · 50 Gi · fast-nvme
- Order
- OrderedReady · 0 → 1 → 2
Read the whole lesson as text
1. The spec decoded
You apply a StatefulSet called db, together with a headless Service, pg-headless, that has no ClusterIP at all. Three fields do the work: serviceName gives every Pod a DNS name, podManagementPolicy OrderedReady starts them one at a time, and volumeClaimTemplates stamps out one 50 Gi claim per Pod. A Deployment gives you none of this: random suffixes, all Pods at once, whatever storage you share between them.
- kubectl posts the StatefulSet and its headless Service to the API server.
- The StatefulSet controller hears about it within milliseconds and starts at ordinal zero.
2. db-0 first
The controller creates exactly one Pod, and its name is fixed before it exists: db-0, never db-7f9c2. That name is the identity - the Pod's hostname is db-0, and the same name comes back after every reschedule. Then the controller waits: no db-1 until db-0 is Running and Ready, which lands at 9.6 s, most of it the image pull.
- It creates db-0 first. The name comes from the ordinal, not a random generator.
- db-0 is Running and Ready, so the controller is finally allowed to create db-1.
3. One volume each
The gate open, the controller does two things in order: create the PVC, then the Pod. db-1's claim is named db-data-db-1 - template name plus Pod name - and it is db-1's alone; db-2 will get its own. The scheduler places db-1 on node-2, and only then does provisioning start, because the StorageClass is WaitForFirstConsumer: no disk is carved out until the cluster knows which node needs it.
- Before the Pod, the controller creates db-1's PVC from the template.
- Then Pod db-1, immediately after its claim.
- The scheduler has picked node-2, so the CSI driver carves out a 50 Gi volume and binds the claim.
4. Mount it
Zoom into node-2. The kubelet sees db-1 assigned to it, but the volume isn't attached yet, so the container waits. The CSI driver attaches the disk to node-2, the node-side plugin stages it - one filesystem, formatted on first use - and publishes it into the Pod at /var/lib/postgresql/data. Only then does the container start, and Postgres's first act is to write a WAL file to its own disk.
- node-2's kubelet sees the Pod, but the disk isn't attached yet.
- The CSI driver attaches the volume to node-2.
- The node-side plugin stages it: one filesystem per volume, shared by every mount of it on this node.
- A bind mount of the staged filesystem appears inside the Pod's namespace.
- The disk is there, so the container starts - and its first WAL lands on db-1's own volume.
5. Find the primary
db-1 is a standby, and it finds its primary by name: db-0.pg-headless.default.svc.cluster.local. Because the Service is headless - clusterIP None - the answer is not a virtual IP but db-0's Pod IP itself: 10.244.1.20. One name, one Pod, no kube-proxy in between: exactly what replication wants, and useless for load balancing. When db-1 turns Ready, the gate opens for db-2, which lands on node-2 too and is Ready by 30.5 s.
- db-1 looks up its primary by name and gets db-0's Pod IP, not a virtual one.
- db-1 passes its own readiness probe, so db-2 may finally start.
- The controller creates db-2's claim and Pod - the same two calls as for db-1.
- db-2 is Ready too: three Pods, three disks, all bound.
6. Same name, same disk
Now delete db-1 - say node-2 is being drained. The controller waits until the old db-1 is completely gone, since two Pods can't share a name, then recreates a Pod named db-1 and leaves the PVC alone: db-data-db-1 already exists, so it is simply reused. node-2 is cordoned for the drain, so the scheduler picks node-1, and the disk must be detached from node-2 and attached to node-1 before it can be mounted again. Postgres starts, finds the WAL on the volume, and replays it - the data survived a reschedule. A Deployment replacement would get a new random name, and an emptyDir would come back empty.
- You delete db-1. Its PVC is not part of the delete.
- Once the old db-1 is gone, the controller recreates a Pod with the same name and reuses the existing claim. It lands on node-1.
- The disk detaches from node-2 and attaches to node-1.
- node-1's kubelet mounts the same volume into the new db-1.
- db-1 is Ready again with its old data - and its DNS record now points at 10.244.1.21.
7. Scale and update
Scaling up is ordinal-ascending: db-3 is born with its own db-data-db-3, Ready at 60.9 s. Scaling down is the mirror: the highest ordinal goes first - db-3 - but its PVC stays, because the default persistentVolumeClaimRetentionPolicy is Retain. Updates run ordinal-descending: with the image now 16.5, db-2 is replaced first, waits for Ready, then db-1, then db-0. Set updateStrategy.rollingUpdate.partition to 2 and only db-2 gets the new version - a canary made of ordinals.
- You scale to 4 replicas; the controller adds ordinal 3.
- db-3 gets a claim and a Pod of its own, on node-2.
- db-3 is Ready: four Pods, four disks.
- Scaling back to 3 deletes db-3, the highest ordinal - and its PVC stays behind.
- You change the image to 16.5; the update starts at the top ordinal.
- db-2 is deleted and recreated on the new image, and nothing moves until it is Ready.
- db-2 is Ready on 16.5; db-1 is next, its PVC riding along under the same name.
Summary
Three things a Deployment cannot give a database, from one object. Identity: db-0 is db-0 forever, in DNS and in the Pod list, across reschedules. Storage: one claim per ordinal, created from the template, reused - never recreated - and kept on scale-down unless you opt in to deleting it. Order: OrderedReady starts 0, 1, 2 and holds the line until each is Ready, which is exactly what a primary and its standbys need.
Next lesson: Jobs and CronJobs