In Kubernetes we find different resources for deploying applications such as
In this blog, we will be focusing on Deployment and Statefulset,
- How deployment and Statefulset works
- What is the differnece between them
Deployment is the easiest and most familiar resource for deploying your application, Deployment is largely used for stateless applications. However, you can save the state of deployment by attaching a Persistent Volume to it and making it stateful, but all the pods of deployment will be sharing the same Volume, and data across all of them will be the same.
Once you’ve defined and deployed a deployment, Kubernetes ensures that the pods manage to meet the requirements of the desired set. This provides granular control over the rollout of new pod versions and rollback to previous versions.
Deployment Component in kubernetes
The following are the major components of deployment :
- Deployment template
- Persistent volumes
In the template below, we’re also attaching a persistent volume:
In the template below, we have our PersistentVolumeClaim:
To execute our deployment, we need a service to access the above deployment
First, we run the service template :
kubectl apply -f service.yml
Then we run the command for the deployment template :
kubectl apply -f demo-deployment.yml
Before going for statefulset we should understand the concept of stateful and stateless applications
Stateless and Stateful application
the term “stateless” means that no past data nor state is stored or needs to be persistent when a new container is created. Stateless applications tend to include containerized microservices apps or any short-term workers, whereas Stateful applications typically involve some database, such as Cassandra, MongoDB, or MySQL, and process a read and/or write to it. the server must access and hold onto state information generated during the processing of the earlier request, which is why it is called stateful.
StatefulSet is a Kubernetes resource used to manage stateful applications. It manages the deployment and scaling of a set of Pods and provides guarantees about the ordering and uniqueness of these Pods. StatefulSet is also a Controller but unlike deployments, it doesn’t create ReplicaSet rather it creates the Pod with a unique naming convention.
If you create a StatefulSet with name counter, it will create a pod with name counter-0, counter-1, counter-2, etc
Every replica of a stateful set will have its own state, and each of the pods will be creating its own PVC. So a statefulset with 3 replicas will create 3 pods, each having its own Volume, i.e 3 PVCs.
Statefulset Component in kubernetes
The following are the major components of StatefulSets :
- Headless Service
We create a StatefulSet template:
First, we create a StatefulSet template:
Secondly, we create a PersistentVolume :
Lastly, we now create a Headless Service :
Now, let’s run the create kubectl command to create the StatefulSet:
kubectl apply -f statefulset-service.yml kubectl apply -f statefulset.yml
Here are some main differences between Deployments and StatefulSets:
- Deployments are used for stateless applications whereas StatefulSets for stateful applications
- The pods in a deployment are interchangeable whereas the pods in a StatefulSet are not.
- In statefulset pod`s names are in sequential order on the other hand in deployment pod`s names are unique.
- A headless service handles the pods’ network ID in StatefulSets, while deployments require a service to enable interaction with pods
- In a deployment, the replicas all share a volume and PVC, while in a StatefulSet each pod has its own volume and PVC.
In this blog, we’ve looked at the two different ways to perform deployments in Kubernetes, and a comparison between them: Deployment and Statefulset. I hope it provide you better understanding about these two deployments in kubernetes.