How to create a StatefulSet in Kubernetes

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

statefulset

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

create-stateful-set

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

kubectl get statefulset

kubectl get service

get-statefulset-and-service

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

kubectl get pods

get-pods-in-statefulset

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

kubectl get statefulset

kubectl describe statefulset web

describe-statefu-set

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

kubectl get pods

recreate-pods

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.

Share this page:

0 Comment(s)