Kubernetes StatefulSet – All you need to know!

Filed Under: Random
Kubernetes StatefulSet

Hello, readers! This article talks about Kubernetes StatefulSet and its implementation, in detail.

So, let us begin!! 馃檪

What is a Kubernetes StatefulSet?

As we have discussed earlier with regard to containerized applications, deployments tend to wrap up the configurations of the entire application in terms of image secrets, containers, pods, services, etc. This solution sounds good as long as the application does not maintain the state of the application.

But what if the application is a stateful one?

For this scenario, we have Kubernetes StatefulSets in the picture. With StatefulSets we can manage applications that are stateful in nature. It is a Kubernetes workload API object that enables us to have configuration within it to manage the app that persists in nature.

With Deployment workload, a StatefulSet performs the function to manage pods with similar container specifications. But on contrary, a StatefulSet maintains an identity of every pod that it has under control. This makes the pods completely interchangeable.

That is, even in the situation of rescheduling the pods, the identity of pods persists and is recorded as well as maintained by StatefulSets.

Thus it makes StatefulSets the best match when our applications require persistence of data.

Usecases of a StatefulSet

  1. When an application requires an unique as well as constant/stable network identifies within it.
  2. An application requests for a persistent storage.
  3. Graceful scaling of pods and deployment workloads.
  4. Automated Rolling updates.

Limitations of the StatefulSet

  • When a StatefulSet is deleted, it never guarantees the deletion of the pods associated with it.
  • In order to have a Pod network identity to be in place, we will have to create a Service for it to associate with in StatefulSets.
  • We would need to provision storage for Pods using a Persistent Volumne Provisioner probably a storage-class or any other solution.
  • Scaling down the statefulsets or even the deletion of the statefulsets would not delete the volume associated with it.

How to create a Kubernetes StatefulSet?

Having understood about StatefulSets, let us now try to implement it. Take a look at the below code!


apiVersion: v1
kind: Service
  name: nginx-svc
    app: nginx
  - port: 443
  clusterIP: None
    app: nginx
apiVersion: apps/v1
kind: StatefulSet
  name: demo-set
      app: nginx 
  serviceName: "nginx"
  replicas: 2
        app: nginx 
      terminationGracePeriodSeconds: 10
      - name: busybox-container
        image: busybox:1.28
        - containerPort: 443
          name: demo
        - name: demo-persistence
          mountPath: /usr/application/demo/html
  - metadata:
      name: demo-persistence
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "regional-storage"
          storage: 2Gi


  • At first, in order to have network identity for pods, we need a Headless Service running. For the same, we have created a ngnix service serving over port 443.
  • We have then added the label nginx that would be matched against the pod for selecting the right pod and service.
  • Post which, we have a StatefulSet and here we mention the service name to match the service that would help us associate a network identity to the pod.
  • In this example, we have made use of busybox as the underlying image for the container.
  • The persistent data would be mounted at the location: /usr/application/demo/html. We have set read-write-once as the access mode to it. Further, we have associated the volume with a storage class named “regional-storage” along with a space of 2Gi storagre.


By this, we have reached the end of this topic. Feel free to comment below, in case you come across any questions.

For more such posts related to Kubernetes, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors