liveBook · Manning

StatefulSet

As explained in Kubernetes for Developers

A StatefulSet is a Kubernetes workload resource that manages stateful Pods with stable identities and persistent storage associations.

A database or similar service may need several Pods, each with its own disk. A Deployment gives its Pods the same specification, so pointing them at one ReadWriteOnce volume works for a single replica but prevents another replica from mounting that volume. A StatefulSet manages the multi-Pod case. section 9.2

A StatefulSet provides two useful guarantees:

  • A volume template creates a separate PersistentVolumeClaim (PVC) for each Pod, rather than making every replica reference the same claim. section 9.2
  • Each Pod gets a stable identifier, or ordinal, linked to its PVC. StatefulSets also guarantee ordering during creation, scaling, and updates. section 9.2

Stable identities help an application coordinate its Pods; they do not configure the application’s roles by themselves. In a three-Pod Redis setup, an init container reads each Pod’s ordinal and selects a configuration file: ordinal 0 becomes the primary, while the others become replicas. The replica configuration names the primary Pod. section 9.2.2

        command:
       - bash
       - "-c"
       - |
         set -ex
         # Generate server-id from Pod ordinal index.
         [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
         ordinal=${BASH_REMATCH[1]}
         # Copy appropriate config files from config-map to emptyDir.
         mkdir -p /redis/conf/
         if [[ $ordinal -eq 0 ]]; then
           cp /mnt/redis-configmap/primary.conf /redis/conf/redis.conf
         else
           cp /mnt/redis-configmap/replica.conf /redis/conf/redis.conf
         fi

The init-container script extracts the ordinal from the Pod hostname and copies either the primary or replica configuration into place. section 9.2.2

Check replication

Write a value through redis-0, then read it through redis-1. An attempted write through redis-1 returns a read-only-replica error. These checks confirm the application’s configured primary and replica roles. section 9.2.2

Stable storage associations make recovery possible, but disk retention needs deliberate configuration. Deleting a StatefulSet does not delete its PVCs. Deleting a PVC can delete its underlying disk when the reclaim policy is Delete; use a StorageClass with a Retain policy when the disk must survive deletion. A retained disk can then be relinked through recreated PersistentVolume and PVC objects. section 9.3

How Kubernetes for Developers covers it

How deep it goes
Explains volume templates, PVCs, ordinals, and ordering through a configured Redis deployment, then addresses disk recovery.
Examples
Uses a three-Pod Redis primary-and-replica configuration, read/write checks, and a retained-disk recovery exercise.
What it assumes
Builds on single-Pod workloads and PVC-backed storage before introducing multi-Pod identities and roles.
What you can do after
Build it from scratch as a multirole Redis StatefulSet and recover its data by retaining and relinking disk resources.
How it connects
Connects StatefulSets to Deployments, Pods, PersistentVolumeClaims, PersistentVolumes, init containers, and Redis replication.

Also explained in

Compare how 4 books explain StatefulSet →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.