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 an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, AWS EKS Service Delivery Partner, and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
You can learn more about our DevOps training offerings from our website.
WRITTEN BY Sirin Kausar Isak Ali
Click to Comment