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



