Skip to content

Lesson 10 of 19 · 20m

StatefulSets and Storage: Identity, PVCs, and the Terminating Pod

Stable names, stable disks, the WaitForFirstConsumer zone trap, and why a node death leaves stateful pods stuck Terminating.

A Deployment gives you fungible pods: delete pod-7, get pod-9, nobody cares. Then someone puts Postgres on it and discovers the hard requirement databases impose: identity. A database can't be "any one of n identical replicas" — it needs to know which member it is, hold the same disk across restarts, and find its peers by a name that doesn't change. That's what StatefulSet exists for, and it does less than people assume.

What a StatefulSet actually gives you

Three guarantees, and only three:

  1. Stable ordinal identity: web-0, web-1, web-2. Not random suffixes — names you can put in config files.
  2. Stable network identity: through a headless Service (clusterIP: None), each pod gets DNS like web-0.web.prod.svc.cluster.local.
  3. Stable storage: volumeClaimTemplates stamps out a PVC per pod — data-web-0, data-web-1 — that reattaches to the same ordinal every reschedule.
spec:
  serviceName: web
  replicas: 3
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: fast-ssd
        resources:
          requests:
            storage: 20Gi

That's it. Ordering is the default (podManagementPolicy: OrderedReady — 0 comes up before 1, dies in reverse) and Parallel exists if your app genuinely doesn't care. Everything else — leader election, replication, backup — is still your problem or your operator's.

The PVC chain and the zone trap

Pod → PVC → StorageClass → dynamic provision → PV. The failure that teaches this chain: PVC provisioned immediately in volumeBindingMode: Immediate creates the disk in a random zone, the pod schedules to a different zone, and you have a volume the pod can never attach. WaitForFirstConsumer defers provisioning until the scheduler picks a node — the volume follows the pod. It's the default you want on multi-zone clusters.

Reclaim policy matters when you scale down or delete: Retain keeps the PV/data after the PVC is deleted; Delete destroys it. And a subtlety that saves careers: deleting or scaling a StatefulSet does not delete its PVCs — they're owned by the claim template, not the pod. Scale to zero, scale back up, data's intact. Delete the PVC, data's gone. That's the feature and the footgun in one.

Expansion is online now: allowVolumeExpansion: true on the StorageClass, edit spec.resources.requests.storage upward on the PVC, filesystem grows without downtime. Shrinking is still not a thing.

Node death is the ugly part

Node goes NotReady with a StatefulSet pod on it and you get the worst status in Kubernetes: the pod sits Terminating forever. The kubelet can't confirm shutdown, so the control plane won't reschedule — because if the node is actually still alive and isolated, rescheduling would put two writers on one ReadWriteOnce volume. Split-brain by filesystem.

The escape hatches: confirm the node is genuinely down, then either kubectl delete pod --force --grace-period=0 (accepted practice once you've verified) or apply the node.kubernetes.io/out-of-service taint (GA since 1.28) so the attach-detach controller force-detaches volumes and the pod reschedules. Do this wrong — force-delete while the node is merely partitioned — and two pods mount the same disk.

The honest tradeoff

Databases on Kubernetes are fine when a real operator is doing the work: CloudNativePG, Zalando's postgres-operator, Strimzi for Kafka — tools that own failover, backup, and upgrade logic that StatefulSet deliberately doesn't. Hand-rolled stateful services on bare StatefulSets are where people learn that the hard part was never scheduling. If your team's answer to "who owns failover at 3am" is nobody, managed databases exist and are usually the correct answer.

Practice

StorageClass with volumeBindingMode: WaitForFirstConsumer, allowVolumeExpansion: true, reclaimPolicy: Retain. StatefulSet with a headless Service. Then the test that matters: in staging, kill a node hosting web-0 and walk the recovery — watch it stick Terminating, apply the out-of-service taint or force-delete after confirming the node is dead, watch it come back on another node with the same disk. That rehearsal is the entire point.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.

What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"

Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.

Article

The World now is Serverless and Container based

A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,

Article

PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.

PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...

Article

Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works

Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.

Article