Stateful workloads

Stateful workloads

Stateless apps are easy: kill a Pod, start another. State changes the rules.

Should it run in Kubernetes at all?

Run it in the cluster whenKeep it outside when
an operator handles failover, backup, upgradesnobody on the team can operate the database
storage is solid (CSI, snapshots, tested restores)storage is “an NFS share somebody set up”
you want the same tooling for everythinga managed service (Azure SQL, RDS) fits the need

The building blocks

graph TB SS[StatefulSet or operator CR] --> P0[pod-0] & P1[pod-1] P0 --> V0[(PVC data-0)] P1 --> V1[(PVC data-1)] V0 & V1 --> SC[StorageClass -> CSI] PDB[PodDisruptionBudget] -.protects.-> P0 & P1 AA[anti-affinity / topology spread] -.spreads.-> P0 & P1
ConcernTool
stable identity, one volume per replicaStatefulSet volumeClaimTemplates
replicas on different nodes / zonespod anti-affinity, topologySpreadConstraints
maintenance without losing quorumPodDisruptionBudget
slow start (recovery, replay)startupProbe
more spaceallowVolumeExpansion: true + edit the PVC
point-in-time copyVolumeSnapshot (CSI) / application backup
failover, replication, upgradesan operator

Access modes decide the design

  • RWO (block storage, most cloud disks): one node at a time - one PVC per replica, replication done by the application.
  • RWX (NFS, CephFS, Azure Files): shared - convenient, but two writers on one database file is corruption, not high availability.

Backups are part of the design

A replica is not a backup: a DROP TABLE is replicated in milliseconds. Plan application-consistent backups (database dump, operator backups to object storage) and test the restore - Lab 39.

References