Sub topic 12
Scaling StatefulSets and other stateful workloads
This chapter assumes you know how Deployments, Pods and PersistentVolumeClaims work, and that you have scaled a Deployment with kubectl scale. Stateful workloads follow the same scaling commands, but they behave differently, and adding replicas does not always add capacity.
Why stateful workloads scale differently
A Deployment treats its Pods as interchangeable. A StatefulSet gives each Pod a stable identity: an ordinal name such as db-0, a stable DNS name through a headless Service, and its own PersistentVolumeClaim (PVC).
Scaling therefore has side effects that stateless scaling does not:
- Each new replica needs its own storage, which must be provisioned.
- The application must be able to join a cluster, copy data and take part in replication.
- Removing a replica can strand data, because the PVC is not deleted with the Pod by default.
Ordered scaling
By default, a StatefulSet uses podManagementPolicy: OrderedReady. Scaling up creates db-0, then db-1, then db-2. Each Pod must be Running and Ready before the next one starts. Scaling down removes Pods in reverse order, highest ordinal first.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
serviceName: db
replicas: 3
podManagementPolicy: OrderedReady
persistentVolumeClaimRetentionPolicy:
whenScaled: Retain
whenDeleted: Retain
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: db
image: postgres:16
env:
- name: POSTGRES_PASSWORD
value: example-only-change-me
resources:
requests:
cpu: 500m
memory: 512Mi
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10GiThe volumeClaimTemplates section creates one PVC per Pod, named data-db-0, data-db-1 and so on. Each Pod always reattaches to its own claim.
kubectl scale statefulset/db --replicas=5
kubectl get pods -l app=db -w
kubectl get pvcIf db-3 never becomes Ready, db-4 is not created. This is a common reason a scale-up appears stuck.
What happens to storage on scale-down
With whenScaled: Retain, the default, scaling from 5 to 3 replicas deletes db-3 and db-4 but keeps data-db-3 and data-db-4. Scaling back up reattaches the old data. This protects data, but the retained volumes keep costing storage. Setting whenScaled: Delete removes the claims automatically. Use it only when the data is disposable or already replicated elsewhere.
Parallel pod management
Ordering slows large scale-ups when the Pods do not depend on each other. Setting podManagementPolicy: Parallel launches or terminates all Pods at once. Choose it only if the application does not require a strict startup order. Note that this setting does not change how rolling updates work.
Autoscaling stateful workloads
An HPA can target a StatefulSet through scaleTargetRef, and the mechanics are the same as for a Deployment. Whether it is a good idea depends on the application.
- Read replicas and sharded caches can often scale out safely if the application rebalances data on its own.
- A single-writer database gains nothing from more replicas unless the replicas serve reads.
- Scaling down can trigger data movement or leader elections that raise load at the wrong moment.
- The Vertical Pod Autoscaler is sometimes a better fit, but a resize may restart the Pod.
For databases and message brokers, an operator is often safer than raw scaling. An operator is a controller that knows how to add members, rebalance data and remove members cleanly. The StatefulSet only creates Pods. It does not know how your application handles membership.
Pair a stateful workload with a PodDisruptionBudget and a readiness probe, as covered earlier. This stops a node drain from taking down too many members at once and losing quorum.
Common mistakes
- Assuming a new replica has data. It starts empty until the application replicates to it.
- Scaling down a quorum-based system below a majority.
- Forgetting that
ReadWriteOncevolumes are tied to a zone, so a replacement Pod may not schedule if capacity is missing there. - Forgetting leftover PVCs after repeated scale-downs.
Exercise
A StatefulSet named db has 5 replicas. You run kubectl scale statefulset/db --replicas=3. Which Pods are removed, what happens to their PersistentVolumeClaims by default, and why might a later scale-up from 3 to 4 start with data already present?
Show answer
Pods db-4 and then db-3 are removed, in reverse ordinal order. By default the retention policy is Retain, so data-db-3 and data-db-4 stay. A later scale-up recreates db-3, which reattaches to the retained claim data-db-3, so it starts with its old data. That data may be stale, so the application must reconcile it.