Stateful Sets differer from deployments in several ways.
The yaml configuration for a stateful set is the same as a deployment, the kind is just different.
StatefulSets also need a headless service defined.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
labels:
app: mysql
spec:
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
replicas: 3
selector:
matchLabels:
app: mysql
serviceName: mysql-h #headless service
When scaling a stateful set, they are scaled in an ordered graceful deployment, meaning that it will scale the pods one by one. When scaling down, the highest numbered pod will be killed 1st down the the lowest numbered pod. These will also be killed one by one.
You can override the ordered, graceful deployment with the use of the podManagementPolicy: Parallel
This will scale pods in parallel instead of one by one. The default value is podManagementPolicy: OrderedReady
Stateful sets utilize a VolumeClaimTemplate to define the creation of a individual PVC for each StatefulSet.
volumeClaimTemplate:
- metadata:
name: data-volume
spec:
accessModes:
- ReadWriteOnce
storageClassName: vmfs3
resources:
requests:
storage: 10Gi
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
labels:
app: mysql
spec:
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
volumeMounts:
- name: data-volume
mountPath: /var/lib/mysql
volumeClaimTemplate:
- metadata:
name: data-volume
spec:
accessModes:
- ReadWriteOnce
storageClassName: vmfs3
resources:
requests:
storage: 10Gi