Cover image for What is a Kubernetes Stateful Set?

What is a Kubernetes Stateful Set?

techworld_with_nana profile image Techworld with Nana ・1 min read

Stateful Sets are used specifically to run stateful, replicated services, like Databases inside Kubernetes cluster.

Configuration of a Stateful Set:

apiVersion: apps/v1

kind: StatefulSet


  name: mysql


  replicas: 3




        - name: mysql

          image: mysql:8.0


            - name: MYSQL_DATABASE

              value: my-db

            - name: MYSQL_ROOT_PASSWORD



                  name: mysql-credentials

                  key: password


            - name: mysql

              containerPort: 3306

The main part of stateful set, that makes it able to run databases is that each replica pod gets assigned a unique ID. This ID sticks to the pod even when its rescheduled to another worker node. Through this unique ID, the pod retains the connection to the volume that holds the state of the database. However because when rescheduling the pod, the volume gets detached and then reattached again, the storage volumes must be on a remote persistent disk, which means the storage need to be hosted outside of the cluster itself.

So overall, each replica pod in a stateful set has its state and data. So when it gets restarted it needs to regain that state. This means we can't use local storage for stateful sets, since local storage is bound to node, so when pod is rescheduled to another node its previous state won't be available for it. Instead it should use a remote storage that isn't bound to any specific node.

Complete and FREE Kubernetes & Docker course on Youtube

Posted on by:

techworld_with_nana profile

Techworld with Nana


DevOps Consultant | YouTuber 🎬 | Software Engineer 👩🏻‍💻 Based in Austria 🇦🇹


markdown guide