In this blog, I have covered the overview of Kubernetes StatefulSet, how to create StatefulSet and many more things which are part of Docker and Kubernetes Certification for Beginners. [To register for a Free Class, find the link at the bottom]
Kubernetes is rich with numerous features and deployment options to run containers. Therefore StatefulSet is one of the features of Kubernetes that acts as a controller to deploy and scale groups of Kubernetes pods. This post will give you a walk-through of the basics of Kubernetes StatefulSet.
- What is Kubernetes StatefulSet?
- When to use StatefulSet?
- Use Cases of StatefulSet
- Limitations of Kubernetes StatefulSet
- How to create a StatefulSet?
- How to update Kubernetes StatefulSet?
- OnDelete
- RollingUpdate
- FAQ
- Conclusion
What is Kubernetes StatefulSet?
StatefulSet is a controller in Kubernetes that allows users to manage pods the same as the deployments. It is mainly designed to use for stateful apps. In most cases, users ignore how their pods are scheduled. But many times, due to some requirements, users make sure that the pods are deployed in order with persistent storage volume and a unique stable network identifier across restarts and rescheduled. In these cases, StatefulSets can make your job easy.
Whenever StatefulSet creates a pod, it assigns an ordinal value and a stable network ID to the pod. Users can also create a VolumeClaimTemplate in the manifest file, and it will further create a persistent volume for each pod. When the StatefulSet deploys pods, they will get deployed in order from 0 to the last pod. The next pod will only be created once the previous pod is ready and in running state.
Check out: Kubernetes Operator
When to use StatefulSet?
Let’s see where Kubernetes StatefulSet can be used:
- Whenever you want to use a Cassandra cluster whose each node maintains access to its data, you can blindly go for StatefulSet.
- When a web app needs to communicate with its replica with the help of predefined network identifiers, StatefulSet is preferred.
- You can also prefer StatefulSet when a Redis pod has access to the volume, but you need to maintain access to the same volume as per your need, even after it is redeployed or restarted.
Note: You can find the StatefulSet feature in Kubernetes 1.9 and later.
Use Cases of StatefulSet
Let’s discuss some of the use cases of StatefulSet.
- In many cases, applications rely on multiple containers. Therefore the ordered approach to scaling comes into play. It makes sure that dependent containers are created in order in the case of deployments and scaling.
- StatefulSet strictly follows automated rolling updates. Updation of dependent applications and microservices should be done in order. Users can choose the order of their own choice.
- Users can define which pods correspond to which persistent storage and also create a resilient application deployment.
- It also allows using of unique network identifiers to create persistent network connectivity. Hence users do not need to worry about IP changes at the time of rescheduling.
Limitations of Kubernetes StatefulSet
As discussed above, StatefulSet has so many use cases. But there are few limitations as well. Let’s see them one by one.
- If you scale up and down or delete the pods, it will not affect the underlying persistent storage to ensure data safety. you will find provisioned volumes always within Kubernetes.
- You always need to provision the storage for StatefulSet using PersistentVolume Provisioner based on the storage class or pre-provisioned.
- Users are always required to create the headless service manually for network identity in StatefulSets.
- Using StatefulSet never guarantees termination of current pods after StatefulSet is deleted. Therefore you can implement an SOP to scale the StatefulSet to zero before deletion.
- If you do Rolling updates with the default pod management policy, it can cause serious issues if a pod is broken.
Check out: Kubernetes Prometheus Monitoring
How to create a StatefulSet?
You can configure a StatefulSet using a manifest file. StatefulSet works on a headless service that can be created in the same manifest file. So, here is the way given below to create a StatefulSet.
vi StatefulSet.yaml
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: default
labels:
app: redis
spec:
ports:
- port: 6379
protocol: TCP
selector:
app: redis
type: Cluster IP
cluster IP: None
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redis
spec:
selector:
matchLabels:
app: redis
serviceName: "redis"
replicas: 1
template:
metadata:
labels:
app: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command: ["redis-server",
ports:
- containerPort: 6379
name: web
volumeMounts:
- name: redis-aof
mountPath: /data
volumeClaim Templates:
- metadata:
name: redis-aof
spec:
accessModes: [ "ReadWriteOnce"]
storageClassName: "gp2"
resources:
requests:
storage: 1Gi
The above code creates a headless service. This service will be used further by StatefulSet. Then this code is creating a StatefulSet that is used to maintain one Redis pod connected to a volume. Let’s create these resources by running the command given below.
kubect1 apply -f StatefulSet.yaml
How to update Kubernetes StatefulSet?
You can update Kubernetes StatefulSet very easily by just making changes in your manifest file. Once you are done with making changes, run the same command used to create a StatefulSet.
kubect1 apply -f StatefulSet.yaml
The result of this command depends upon which update strategy users have selected for their StatefulSet. There are two updated strategies available for StatefulSet.
OnDelete
If you opt for the OnDelete strategy as the value of .spec.updateStrategy.type, it will not replace the pods while applying the manifest. Users need to delete existing StatefulSet pods manually before creating the new version.
RollingUpdate
If you go with RollingUpdate and apply the manifest, remove the StatefulSet pods and be replaced in reverse ordinal order. It also helps to specify .spec.updateStrategy.rollingUpdate.partition to an ordinal value. This will replace all the pods of a higher ordinal value with the new version, and older versions will be retained. This feature allows you to perform phased rollouts.
FAQs
Q1. What is stateful vs. stateless?
Ans: StatefulSet services help keep track of sessions and transactions and use historical data to react each time differently to the same inputs. Whereas stateless services depend upon the clients o maintain sessions and center around operations.
Q2. How does Kubernetes StatefulSet work?
Ans: StatefulSet works very similarly to deployment. StatefulSet deploys the containers in the sequence where the first pod is deployed and ready until the second pod deploys.
Q3. When should I use a StatefulSet?
Ans: StatefulSet can be used in the following cases:
- If you want the Redis pod to maintain access to the same volume, it is accessing before. Even if it is redeployed or restarted, you can use StatefulSet.
- If you have a Cassandra cluster that has its node to maintain access to its data.
- If your web application needs to communicate with its replicas using known predefined network identifiers, you can blindly trust StatefulSet.
Q4. Why are microservices stateless?
Ans: Microservices are stateless when they do not hold information in their internal storage that is critical to clients; rather, they hold data in external stores.
Q5. Is Facebook stateful or stateless?
Ans: Facebook uses a stateless service. Whenever the server requests a list of recent messages using the API of Facebook, it issues a get request and stores the data in the user’s machine in the form of a cache.
Q6. What is the difference between StatefulSet and Deployment?
Ans: A StatefulSet acts as a controller in Kubernetes that manages pods just like Deployments. Whereas, Deployment is more suited for stateful apps.
Conclusion
StatefulSets in Kubernetes is a great feature to deploy and scale pods in Kubernetes. Stateful applications are the general types of applications that are containerized and then placed in Kubernetes-managed environments. With many feature updates and advancements in Kubernetes storage constructs and operations, Kubernetes has also started supporting data-driven applications.
Related Post
- Kubernetes Operator: An Overview, Stateful Application Example
- How To Setup A Three Node Kubernetes Cluster For CKA: Step By Step
- Certified Kubernetes Administrator (CKA) Certification Exam
- Check out the Official Documentation for Kubernetes StatefulSets
- Kubernetes Prometheus Monitoring
- Certified Kubernetes Administrator (CKA) Certification: Step By Step Activity Guides/Hands-On Lab Exercise
Join FREE Class
Discover the Power of Kubernetes, Docker & DevOps – Join Our Free Masterclass. Unlock the secrets of Kubernetes, Docker, and DevOps in our exclusive, no-cost masterclass. Take the first step towards building highly sought-after skills and securing lucrative job opportunities. Click on the below image to Register Our FREE Masterclass Now!
Leave a Reply