Voiced by Amazon Polly |
Introduction
In the realm of container orchestration, Kubernetes stands as the undisputed champion. Its ability to manage and scale containerized applications has revolutionized the way we deploy and maintain software. One critical aspect of this is managing data, especially in applications that require persistent storage. This is where StatefulSets comes into play.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
Understanding StatefulSets
A StatefulSet is an API object within Kubernetes used for managing stateful applications. It handles the deployment and scaling of a specific set of Pods while ensuring the order and uniqueness of these Pods.
While similar to a Deployment in that it manages Pods with the same container specification, a StatefulSet maintains a distinct and unchanging identity for each Pod. This uniqueness remains consistent even if rescheduling occurs. This feature is particularly useful for workloads requiring persistence, as StatefulSets can help match existing storage volumes to newly created Pods, even in the event of failures.
StatefulSet Architecture
StatefulSets are particularly well-suited for applications with the following needs:
- Stable and distinct network identifiers: Each Pod in a StatefulSet maintains a reliable network identity, ensuring consistency and predictability.
- Persistent storage: StatefulSets integrate well with storage volumes, providing persistence for data across Pod restarts.
- Ordered deployment and scaling: StatefulSets offers controlled and sequential scaling and deployment, maintaining the desired order for application components.
- Ordered and automated rolling updates: When updates are necessary, StatefulSets facilitate orderly and automated updates while considering the application’s statefulness.
It’s important to note that the term “stable” here is synonymous with persistence across Pod rescheduling. If your application doesn’t require steady identifiers or ordered deployment, deletion, or scaling, it might be more appropriate to use a workload object like Deployment or ReplicaSet for stateless requirements.
How to Create a StatefulSet in Kubernetes
Note:- should have k8s cluster deployed with kops or kubeadm
Step 1: Creating a StatefulSet
- Create the yaml file that creates the statefulset. It basically creates a statefulset and Headless Service for it.
$ vi nginx-sts.yaml
apiVersion: v1 kind: Service
metadata:
name: sts-svc
labels:
app: sts-svc
spec:
ports:
- port: 80
name: web
clusterIP: None #Headless Service
selector:
app: ng-sts
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: sts-web
spec:
selector:
matchLabels:
app: ng-sts # has to match .spec.template.metadata.labels
serviceName: "sts-svc"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: ng-sts # has to match .spec.selector.matchLabels
spec:
containers:
- name: ng-str
image: registry.k8s.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 300Mi
Once entered, it should look as shown below.
- Create a daemonset object using the yaml created in the previous step
$ kubectl apply -f nginx-sts.yaml
- Verify that statefulset has been created
$ kubectl get statefulset nginx-sts
- Verify that the service has been created. You can use either of the commands to view the service created
$ kubectl get service sts-svc
$ kubectl describe service sts-svc
- Verify the ordered pod creation.
$ kubectl get pods -w -l app=nginx-sts
$ kubectl get pods -o wide | grep nginx-sts
- Execute thehostname command in each Pod
$ for i in 0 1 2; do kubectl exec "nginx-sts-$i" -- sh -c 'hostname'; done
- Launch the busy box as dns-test container and get into it. We are going to test the connectivity from the busy box container. Use nslookup command to test the reachability to the pod nginx-sts-0, nginx-sts-1, and nginx-sts-2 using the service nginx-svc
$ kubectl run -i --tty --image busybox:1.28 dns-test –rm --restart=Never -- nslookup nginx-sts-0.nginx-svc
# exit - Delete pods wait for stateful to restart them and watch the process of ordered deletion and recreation.
$ kubectl delete pod -l app=nginx-sts
$ kubectl get Pod -w -l app=nginx-sts
- View the Pod’s hostname using the below command
$ for i in 0 1; do kubectl exec "nginx-sts-$i" -- sh -c 'hostname'; done
- The StatefulSet controller (i.e., nginx-sts.yaml) created three PersistentVolumeClaims that are bound to two PersistentVolumes. Run the following command and get the info
$ kubectl get pvc -l app=nginx-sts
Step 2: Scaling StatefulSet
- Scale the application to five replicas, watch the creation process and, fire below commands, and watch the process of scaling the replicas.
$ kubectl scale sts nginx-sts --replicas=5
$ kubectl get pods -w -l app=nginx-sts
- Verify that the scaled replicas are up and running.
$ kubectl get pods -l app=nginx-sts
- Get the pvc. Note that the PVC has also increased.
$ kubectl get pvc -l app=nginx-sts
- Scale down statefulset and verify ordered termination. Perform an edit on the statefullset. Look for the replicas and replace the value from 5 to 3. Save and close the editor. This command automatically applies the changes to the statefulset file.
$ kubectl edit sts nginx-sts
$ kubectl get pods -w -l app=nginx-sts
Notice that the controller deletes the pods one at a time. It waits for one to completely shut down before going to the next.
- Verify statefulSet’s PersistentVolumeClaims and verify that they are not deleted on scaling down.
$ kubectl get pvc -l app=nginx-sts
Conclusion
In the dynamic world of Kubernetes, driven by flexibility and scalability reign supreme, StatefulSets emerge as the unsung heroes for applications that demand data persistence and stability. Their ability to maintain unique identities and provide a stable environment for pods is invaluable for a wide array of stateful applications. By prioritizing data integrity, StatefulSets paves the way for a new era of resilient containerized deployments. Embrace the journey, and let your data lead the way!
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Premier Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.

WRITTEN BY Sirin Kausar Isak Ali
Comments