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. Key Features of StatefulSets
Section titled “1. Key Features of StatefulSets”- Stable Network Identifiers: Pods have deterministic hostnames:
mysql-0,mysql-1,mysql-2. - Dedicated Storage: Uses
volumeClaimTemplatesto provision a dedicated Persistent Volume for every single Pod replica. - Ordered Deployment and Scaling: Pod
mysql-1will not start untilmysql-0is Running and Ready. - Ordered Teardown: When scaling down,
mysql-2is terminated beforemysql-1.
2. StatefulSet Manifest with Headless Service
Section titled “2. StatefulSet Manifest with Headless Service”apiVersion: v1kind: Servicemetadata: name: mysql-headless labels: app: mysqlspec: ports: - port: 3306 name: mysql clusterIP: None selector: app: mysql---apiVersion: apps/v1kind: StatefulSetmetadata: name: mysqlspec: 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