Skip to content

StatefulSets: State-Preserving Workloads

A StatefulSet manages the deployment and scaling of a set of Pods, providing guarantees about the ordering and uniqueness of these Pods.


  1. Stable Network Identifiers: Pods have deterministic hostnames: mysql-0, mysql-1, mysql-2.
  2. Dedicated Storage: Uses volumeClaimTemplates to provision a dedicated Persistent Volume for every single Pod replica.
  3. Ordered Deployment and Scaling: Pod mysql-1 will not start until mysql-0 is Running and Ready.
  4. Ordered Teardown: When scaling down, mysql-2 is terminated before mysql-1.

2. StatefulSet Manifest with Headless Service

Section titled “2. StatefulSet Manifest with Headless Service”
apiVersion: v1
kind: Service
metadata:
name: mysql-headless
labels:
app: mysql
spec:
ports:
- port: 3306
name: mysql
clusterIP: None
selector:
app: mysql
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql-headless
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 20Gi