What's a kubernetes StatefulSet?

kubernetes StatefulSet

3 min read | by Jordi Prats

When we need to have a stateful application running on Kubernetes, we might need to use a StatefulSet. With a StatefulSet, we can manage stateful applications where each pod needs a stable, unique network identity and storage that persists across pod rescheduling. Unlike Deployments, which focus on maintaining a specified number of replicas, StatefulSets ensure each pod is given a unique ordinal index and retains the same identity throughout its lifecycle. If a pod is deleted or a node fails, the replacement pod will maintain the same identity and storage.

We'll are going to see StatefulSets in use-cases thst require stable storage for data persistence across restarts, such as:

  • Databases (e.g., MySQL, PostgreSQL, ...)
  • Distributed applications that need consistent identities (e.g., Kafka, Zookeeper, ...)

StatefulSets are defined similarly to Deployments, but with a few notable differences. For example, in addition to a selector and template section, StatefulSets include a volumeClaimTemplates section to request storage:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: demo-ss
spec:
  serviceName: "demo-service"
  replicas: 3
  selector:
    matchLabels:
      app: pod-demo-ss
  template:
    metadata:
      labels:
        app: pod-demo-ss
    spec:
      containers:
      - name: nginx
        image: nginx:latest
  volumeClaimTemplates:
  - metadata:
      name: demo-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi

If we apply this definition in a cluster, it will create three pods with persistent volumes:

$ kubectl create -f sts-demo.yaml ; kubectl get pods -w
statefulset.apps/demo-ss created
NAME        READY   STATUS    RESTARTS   AGE
demo-ss-0   0/1     Pending   0          0s
demo-ss-0   0/1     Pending   0          4s
demo-ss-0   0/1     ContainerCreating   0          4s
demo-ss-0   1/1     Running             0          10s
demo-ss-1   0/1     Pending             0          0s
demo-ss-1   0/1     Pending             0          4s
demo-ss-1   0/1     ContainerCreating   0          4s
demo-ss-1   1/1     Running             0          6s
demo-ss-2   0/1     Pending             0          0s
demo-ss-2   0/1     Pending             0          4s
demo-ss-2   0/1     ContainerCreating   0          4s
demo-ss-2   1/1     Running             0          6s

Each pod in the StatefulSet will have a stable network identity (demo-ss-0, demo-ss-1, etc.) and retain data in its associated volume, which Kubernetes will manage to persist through restarts (as opposed with Deployments or DaemonSets). Pods belonging to StatefulSets work as any other Pod respecting taints and tolerations, allowing them to be deployed across various nodes in the cluster.

Besides the Pods, we can also see the PVCs created by the StatefulSet with the provided claim template:

$ kubectl get pvc
NAME                     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
demo-storage-demo-ss-0   Bound    pvc-baa93163-91df-4e9d-b483-b0089621f421   1Gi        RWO            standard       105m
demo-storage-demo-ss-1   Bound    pvc-41302634-d85b-4288-afd2-4211edad25fa   1Gi        RWO            standard       105m
demo-storage-demo-ss-2   Bound    pvc-58d45e58-6b7e-4596-9ee0-c3763aecb174   1Gi        RWO            standard       105m

Posted on 04/11/2024

Categories