Skip to content

Lesson 8 of 9 · 26m

Stateful Workloads: Storage Is the System

StatefulSets, PVC binding, headless Services, topology, and why storage semantics dictate what your application can safely promise.

Running stateless applications on Kubernetes is mostly scheduling. Running stateful systems is mostly storage, identity, and failure semantics. The platform does not make a database cloud-native. It gives you primitives; the database still has opinions about quorum, write ordering, replication, and recovery.

StatefulSets give identity, not magic

apiVersion: apps/v1
kind: StatefulSet
metadata: {name: postgres}
spec:
  serviceName: postgres
  replicas: 3
  selector: {matchLabels: {app: postgres}}
  template:
    metadata: {labels: {app: postgres}}
    spec:
      containers:
        - name: postgres
          image: postgres:16.6
          volumeMounts: [{name: data, mountPath: /var/lib/postgresql/data}]
  volumeClaimTemplates:
    - metadata: {name: data}
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: ceph-rbd
        resources: {requests: {storage: 100Gi}}

Each Pod gets stable network identity (postgres-0, postgres-1, ...), stable ordinal lifecycle, and its own PVC from volumeClaimTemplates. Scaling down does not delete those PVCs. That protects data, but it also means cleanup is explicit and dangerous.

Headless Services expose members, not a load-balanced abstraction

apiVersion: v1
kind: Service
metadata: {name: postgres}
spec:
  clusterIP: None
  selector: {app: postgres}
  ports: [{port: 5432}]

clusterIP: None makes DNS return individual Pod addresses rather than one virtual IP. Clustered systems need member identity for replication and leader election. Do not assume a normal Service is harmless; depending on the application, it can route writes to the wrong replica and create split-brain behavior.

Storage classes are operational contracts

storageClassName implies performance, replication, expansion support, snapshot capability, reclaim behavior, and topology. Read the provisioner documentation before using it. Key questions:

  • Is the volume node-local, zonal, or replicated?
  • Does it support volumeExpansion: true?
  • What happens when the node dies?
  • Are snapshots crash-consistent only, or can the application flush writes first?

A ReadWriteOnce volume often means one node, not one Pod forever. Multi-node attach is a different capability and not a substitute for application replication.

Availability is a topology decision

Pod anti-affinity, topology spread constraints, and storage topology must agree. Spreading Pods across zones does not help if every volume is pinned to one zone and failover is manual. Conversely, replicated storage is not a replacement for database replication when write consistency matters.

Field notes

  • Test failover by deleting Pods and nodes in a controlled environment; do not infer behavior from diagrams.
  • PVC resize often requires filesystem expansion support and sometimes Pod restart. Verify before promising online growth.
  • Never point two independent application clusters at the same data volume unless the software explicitly supports shared storage.
  • StatefulSet rolling updates are ordinal-aware; that is useful, but application-aware upgrade ordering may still require an operator.

Was this lesson helpful?

Sign in to rate