StatefulSets contain a set of Pods with unique, persistent identities and stable hostnames. A pod template is used in a Statefulset, which contains a specification for its pods, pods are created using this specification. We can deploy stateful applications and clustered applications using Statefulsets in Kubernetes. StatefulSet can be updated by making changes to its Pod specification, which includes its container images and volumes.  

StatefulSets can be used when the applications require any of the following properties.

  • Stable, unique network identifiers.
  • Stable, persistent storage.
  • Ordered, graceful deployment and scaling.
  • Ordered, automated rolling updates.

For a StatefulSet with N replicas, when Pods are being deployed, they are created sequentially, in order from {0..N-1}. When Pods are being deleted, they are terminated in reverse order, from {N-1..0}.

To know more about Statefulset, click here.

In this article, we will create a Statefulset with replicas of Nginx pods. We will perform operations on the Pods to see how they are deleted and created.

Pre-requisites

  1. Kubernetes Cluster with at least 1 worker node.

    If you want to learn to create a Kubernetes Cluster, click here. This guide will help you create a Kubernetes cluster with 1 Master and 2 Nodes on AWS Ubuntu 18l04 EC2 Instances. 

What we will do

  1. Create a Statefulset

Create a Statefulset

Create a file and add the following Statefulset definition in it.

vim statefulset.yml

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: "nginx"
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web

<img alt="statefulset" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/screenshot_2020-06-21_at_112541_am.png611221f13086e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="750" loading="lazy" src="data:image/svg xml,” width=”750″>

In this example, 

  • A Headless Service, named nginx, is used to control the network.
  • The StatefulSet, named web, has 3 replicas of the nginx container that will be launched in unique Pods.
  • nginx Image with version slim:0.8 is used to deploy Nginx.

To create a Statefulset, execute the following commands.

kubectl get statefulset

kubectl create -f statefulset.yml

<img alt="create-stateful-set" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/screenshot_2020-06-21_at_112734_am.png611221f159526.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="198" loading="lazy" src="data:image/svg xml,” width=”750″>

Execute the following 2 commands to list the Statefulset and Service created in the above step.

kubectl get statefulset

kubectl get service

<img alt="get-statefulset-and-service" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/screenshot_2020-06-21_at_112828_am.png611221f17da16.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="194" loading="lazy" src="data:image/svg xml,” width=”750″>

Get the pods using the following command and see the Pods have numbers as Suffix in the Pod name. 

kubectl get pods

<img alt="get-pods-in-statefulset" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/screenshot_2020-06-21_at_112949_am.png611221f1a3b7e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="173" loading="lazy" src="data:image/svg xml,” width=”750″>

To get the complete details of the Statefulset, execute the following commands.

kubectl get statefulset

kubectl describe statefulset web

<img alt="describe-statefu-set" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/screenshot_2020-06-21_at_113037_am.png611221f21ad97.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="407" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, let’s delete the pods and see how names are preserved even after new pods are created.

We are deleting 2 pods to see what names will be assigned to the new pods upon creation.

kubectl get pods

kubectl delete pods web-0 web-2

<img alt="recreate-pods" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/screenshot_2020-06-21_at_113200_am.png611221f25f33d.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="402" loading="lazy" src="data:image/svg xml,” width=”750″>

In the above screenshot you can see that, even after deleting the pods, newly created pods get the same name.

Conclusion

In this article, we created a Statefulset and performed operations on it to check its details. We also deleted the pods to see how the name of the pod is preserved and the same is assigned to the newly created pods after deleting it.