Kubernetes StatefulSets: Running Stateful Applications in Containers
Last Updated :
25 Oct, 2023
StatefulSets are Kubernetes components that are used specifically for deploying stateful applications. In Kubernetes on the basis on methods of deploying. There are two types of applications - Stateful applications and stateless applications. Therefore, There are two ways for deploying an application on Kubernetes - through Deployment (for deploying stateless applications) and through StatefulSets (for deploying stateful applications).
Applications that maintain any form of persistent state or data are called stateful applications. Most of the database applications that we build like MySQL and MongoDB are stateful applications. The basic point of difference between a stateful and a stateless application is the need of persistent storage. Stateful applications requires a persistent storage while stateless applications do not have a need to store data, therefore they don't require any persistent storage.
Stateful Applications And Stateless Applications
So what do we mean by stateful or stateless applications, basically all applications have some degree of state that is - in some applications the state of the application does not really impact the user (we will discuss this in stateless application section). These applications are called stateless applications. While other applications (like databases, message queues, caches, etc.) have a much higher degree of state because they rely on storage. This state does impact the user, that is we can not handle requests in a database independently. This type of applications are called Stateful applications. To explain these points further:
Stateful Applications
Those applications that maintain some form of persistent state or data are called stateful applications. The key characteristic that differentiate them from stateless applications is that these applications don't rely on storing data locally and they don't treat each request as independent. They manage data between interactions. Sometimes stateless applications connect to the stateful application to forward the requests to a database.
Stateless Applications
Those applications that do not maintain any form of persistent state or data locally are called stateless applications. In stateless applications, each request or interaction is treated independently. These applications are designed to be highly scalable, easy to manage and fault-tolerant because unlike Stateful applications, they don't have to track past interactions or requests.
Stateless applications are deployed using deployment component. Deployment is an abstraction of pods and allows you to replicate the application meaning it allows you to run to 1, 5, 10 or n identical pods of the same stateless application.
StatefulSets
As we discussed earlier, StatefulSets are Kubernetes component that is used specifically for stateful applications. according to the official Kubernetes documentation - StatefulSets are API objects in Kubernetes that are used to manage stateful applications. StatefulSets represents the set of pods with unique, persistent identities, and elastic hostnames. It makes us assured about the ordering of scaling and deployments in case of stateful applications since during scaling pods in stateful applications, pods get created as well as deleted in a specific order (gets created one by one and last one is deleted first).
Note:- The structure of a StatefulSet YAML file is identical to a deployment, the only difference is that it has a service name to define a service.
Example of a StatefulSets
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: gfg-statefulset
annotations:
description: "This is a sample GFG statefulset"
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 4
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumes:
- name: www
persistentVolumeClaim:
claimName: myclaim
Deploying A Stateful Application Using Kubernetes StatefulSets
In this section, we will learn how to deploy a stateful Redis cluster using Kubernetes. Following the tutorial step by step in order to deploy the stateful set:
Step 1. Creating a kubernetes namespace where we will keep all our resources to deploy. For creating a namespace, enter the following command in your terminal:
kubectl create ns gfg-namespace
This will create you a namespace called "gfg-namespace" You will get a similar output:

Step 2. Create a statefulset.yaml file and enter the following code inside the file. You can create the file by using the command:
touch statefulset.yaml
Now enter the following code inside the statefulset.yaml:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-cluster
data:
update-node.sh: |
#!/bin/sh
REDIS_NODES="/data/nodes.conf"
sed -i -e "/myself/ s/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/${POD_IP}/" ${REDIS_NODES}
exec "$@"
redis.conf: |+
cluster-enabled yes
cluster-require-full-coverage no
cluster-node-timeout 15000
cluster-config-file /data/nodes.conf
cluster-migration-barrier 1
appendonly yes
protected-mode no
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis-cluster
spec:
serviceName: redis-cluster
replicas: 6
selector:
matchLabels:
app: redis-cluster
template:
metadata:
labels:
app: redis-cluster
spec:
containers:
- name: redis
image: redis:5.0.1-alpine
ports:
- containerPort: 6379
name: client
- containerPort: 16379
name: gossip
command: ["/conf/update-node.sh", "redis-server", "/conf/redis.conf"]
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
volumeMounts:
- name: conf
mountPath: /conf
readOnly: false
- name: data
mountPath: /data
readOnly: false
volumes:
- name: conf
configMap:
name: redis-cluster
defaultMode: 0755
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "hostpath"
resources:
requests:
storage: 50Mi
---
apiVersion: v1
kind: Service
metadata:
name: redis-cluster
spec:
clusterIP: None
ports:
- port: 6379
targetPort: 6379
name: client
- port: 16379
targetPort: 16379
name: gossip
selector:
app: redis-cluster
Step 3. Now similar to previous step, create a service file named app.yaml and enter the following code into it:
touch app.yaml
apiVersion: v1
kind: Service
metadata:
name: hit-counter-lb
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 5000
selector:
app: myapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hit-counter-app
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: aimvector/api-redis-ha:1.0
ports:
- containerPort: 5000
Step 4. Enter the following command to apply the app.yaml file in our namespace:
kubectl -n gfg-namespace apply -f app.yaml
Step 5. Now we can apply the statefulset.yaml in our namespace. Enter the following command to apply the statefulset:
kubectl -n gfg-namespace apply -f statefulset.yaml
It will create configmap, service and statefulset for the redis-cluster. it will give you a similar output:

Step 6. We have successfully created our pods. in our Redis cluster we have 6 pods running. In order to check if the pods are running or not, we can get the list of all pods inside our namespace by the following command
kubectl -n gfg-namespace get pods
This will show you 6 pods running, all the pods are indexed properly like we discussed earlier. If all the pods are not yet running, you shall wait and enter the command again since it takes some time for pods to get running. You will get a similar output:

Step 7. Now we should check if there are equal number of Persistant Volumes are running, for that enter the following code in the terminal:
kubectl -n gfg-namespace get pv
You will get 6 PVs running, each one for a pod. You will get a similar output:

This is what Kubernetes does, it creates a pod, then it creates the volume and assigns the volume to the pod and move to the next Kubernetes pod, it repeats the same thing for all the pods.
Step 8. Now let's scale up our pods form 6 to 10, for that we can enter the following command:
kubectl -n gfg-namespace scale statefulset redis-cluster --replicas=10
Step 9. Let's again check our pods by the command
kubectl -n gfg-namespace get pods
And this time we have 10 pods running indexed from 0 to 9. You will also get a similar output:

Step 10. Similarly we can scale down our pods to 3 by enter the following command:
kubectl -n gfg-namespace scale statefulset redis-cluster --replicas=3
and now you will get this output:

We now have only 3 pods. This is how we use StatefulSets in order to deploy Stateful applications. Let's now discuss some differences between deployment and StatefulSet
Differences Between Deployment and StatefulSet
|
StatefulSets are Kubernetes component that is used specifically for stateful applications.
| Deployment is used to deploy stateless applications.
|
In StatefulSets, the pods get created as well as deleted in a specific order
| In Deployment, all pods are created parallelly.
|
When we scale down StatefulSets, the last pod gets deleted
| When we scale down a deployment a random pod is picked up and deleted
|
A sticky and predictable name is assigned to the pods
| A random name is assigned to the pods.
|
Each pod uses its own persistent volume (like we saw in the tutorial.)
| All the pods use the same persistent volume
|
Conclusion
In this article we discussed about Kubernetes StatefulSets. These are Kubernetes used to deploy stateful applications. We also discussed what are these stateful and stateless applications? Stateful applications are applications that maintain some form of persistent state or data. While stateless applications are those applications that do not maintain any form of persistent state or data locally. To learn more about how to deploy StatefulSet we have an article on "How to Use Kubernetes StatefulSets". We hope that this article helped you gain a basic insight on what Kubernetes StatefulSets are and why are they used in deploying Stateful applications.
We would like to end this article with some frequently asked questions about Kubernetes StatefulSets.
Similar Reads
Deploying a React Application in Kubernetes
Kubernetes is an open-source free manager for your computer program. These programs can be in the containers. Each container holds a program and everything it needs to run. To Keep track of all these containers that contain your application this is where the Kubernetes role comes in. Kubernetes does
5 min read
How To Share Storage Between Containers In Kubernetes ?
Kubernetes, or K8s, is an open-sourced container orchestration technology that is used to automate the manual processes of deploying, managing, and scaling applications by the help of containers. Kubernetes uses a single container per pod, which is great for most stateless applications, but some app
7 min read
How to use Prometheus to Monitor Docker Containerized Applications
Containerization has transformed software development by providing portability and consistency through platforms like Docker. Yet ensuring the health of those containerized applications requires monitoring. Prometheus is a monitoring tool that uses its powerful features to monitor the application an
7 min read
How to Restart Container in Kubernetes?
Kubernetes is a platform for container orchestration created to simplify application deployment, scaling, and administration. Suppose you have a big package of LEGO parts and you want to use them to make something very spectacular. But as your LEGO creation becomes larger, it becomes harder to organ
8 min read
Kubectl Rollout Restart for Statefulset - Kubernetes
Stateful applications are managed via Kubernetes StatefulSets, a particular workload API object. They offer guarantees concerning the ordering and uniqueness of pods, in contrast to deployments. Applications like databases and distributed systems, which need trustworthy, unique network IDs and long-
9 min read
Rolling Updates and Rollbacks in Kubernetes: Managing Application Updates
Many websites and apps now run on clusters of computers called containers. Containers let apps run smoothly as groups work on updating and improving the software behind the apps. A system called Kubernetes helps manage and update all those containerized apps. Sometimes app updates go wrong or cause
14 min read
Containerizing Applications with Docker Compose: Step-by-Step Tutorial
In the present quickly developing scene of software development and deployment, containerization has arisen as a unique advantage. It offers a solution for the perpetual test of ensuring consistency in software conditions across different phases of the development lifecycle and different sending tar
7 min read
How to Use Docker For Stateful Applications with Persistent Volumes?
Data persistence is provided through a controlled directory called a Docker Volume, which may be mounted inside Docker containers. When containers are stopped or deleted, it enables data to continue to exist. Volumes can be shared across containers and are unaffected by container lifecycles. They ma
2 min read
Deploying Application Using Kubernetes - Full Project Tutorial
Kubernetes or K8s is an open-sourced container orchestration technology that is used for automating the manual processes of deploying, managing, and scaling applications withy the help of containers. Kubernetes was originally developed by engineers at Google and In 2015, it was donated to CNCF (Clou
11 min read
Scaling Applications With Kubernetes ReplicaSets
Kubernetes is an orchestration tool used to scale , deploy and manage the containerized application. It provides ReplicaSet which is used to run a desired number of pods on any given time . In this guide, I will first discuss what Kubernetes is. Then I will discuss about ReplicaSets. After this I wi
5 min read