Voiced by Amazon Polly |
Overview
Maintaining data persistence is crucial for stateful workloads when implementing Kubernetes microservices architecture. Our latest blog dives into the significance of persistent storage solutions, offering insights into ensuring continuity. Real-time use cases demonstrate how adopting resilient storage architectures can safeguard critical data against failures and enable seamless scaling. From deploying stateful applications like databases to managing distributed file systems, the blog highlights best practices for leveraging persistent storage effectively. Explore how implementing robust storage strategies empowers Kubernetes clusters to confidently handle diverse workloads, ensuring uninterrupted operations and data integrity.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
Conversations within the Kubernetes ecosystem often prioritize discussions on microservices, emphasizing their ability to scale dynamically while overlooking the significance of stateful services. These include databases, legacy applications, and other software that rely on maintaining consistent states for proper functionality. Fortunately, Kubernetes offers robust support for such applications through resources like StatefulSets.
Pre-requistes
- Amazon EKS Cluster
- Knowledge of Storage Concepts: Understand basic storage concepts such as block storage, file storage, and object storage. Understand the difference between storage classes, PersistentVolumes (PVs), and PersistentVolumeClaims (PVCs).
- Access to a Kubernetes Cluster: Set up access to a Kubernetes cluster, either locally using tools like Minikube or Kind or using a cloud-based Kubernetes service like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Azure Kubernetes Service (AKS).
Managing Stateful Applications with Kubernetes StatefulSets and PersistentVolumes
Even with limited experience with StatefulSets, you’ve probably encountered Deployments. Ensuring persistent storage for a deployment typically involves defining a persistent volume claim within the deployment configuration. In this setup, all Pods within the Deployment utilize the same allocated PersistentVolume. This method applies similarly to StatefulSets, and it’s beneficial to grasp this concept fully before delving into the distinct functionalities that Kubernetes offers specifically for StatefulSets.
Need to create Persistent volume claim and Persistent Volume and with statefulset, for that configurations are mentioned below:
pvc.yaml
1 2 3 4 5 6 7 8 9 10 11 |
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: shared-pv spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: local-path |
Create Persistent volume claim
Following that, integrate the PersistentVolumeClaim into a StatefulSet. The provided example demonstrates the deployment of a Busybox Pod, which executes a perpetual sleep command to simulate a workload.
Statefulset.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
apiVersion: apps/v1 kind: StatefulSet metadata: name: shared-pv namespace: demo spec: replicas: 3 selector: matchLabels: app.kubernetes.io/name: shared-pv template: metadata: labels: app.kubernetes.io/name: shared-pv spec: containers: - name: shared-pv image: "docker.io/busybox:latest" command: - sleep - "infinity" volumeMounts: - mountPath: /mnt name: shared-pv volumes: - name: shared-pv persistentVolumeClaim: claimName: shared-pv |
Create the statefulset and check whether it’s been bound to PV and PVC.
Check whether pods are created successfully and bounded to PV and PVC, and there should be only a single persistent volume present as it is a shared one
Check whether volumes are shared successfully by exec into the pod, create a file, and test whether the same is present in other pods or not, as it should be present because the single volume is shared across.
It’s imperative to grasp this differentiation: Deployment workloads often exhibit stateless behavior, allowing them to share a PersistentVolume seamlessly. However, this principle doesn’t universally apply to stateful workloads. While certain stateful workloads might resemble deployments and can effectively utilize shared data volumes, others may demand a dedicated PersistentVolume for each pod. In the subsequent section, we will delve into Kubernetes’ approach to managing these diverse workload scenarios.
Leveraging Volume Claim Templates within a StatefulSet
Stateful workloads in Kubernetes benefit from the StatefulSet resource, offering assurances on Pod ordering and uniqueness. One notable feature of StatefulSets is Volume Claim Templates, which are particularly valuable for persistent storage.
Previously, we observed that referencing a PersistentVolumeClaim in a StatefulSet result in all Pods sharing a single PersistentVolume. However, this setup may not be ideal for stateful applications requiring dedicated storage per instance. Manually creating PersistentVolumeClaims for each Pod would be cumbersome and inefficient.
Volume Claim Templates address this by allowing the definition of a PersistentVolumeClaim that replicates for each Pod in the StatefulSet, ensuring each Pod has its dedicated storage. Kubernetes handles the creation of necessary PersistentVolumeClaims automatically.
The following example mirrors the previous one but replaces manual PersistentVolumeClaim creation with the .spec.volumeClaimTemplates field. This field holds a list of PersistentVolumeClaims that Kubernetes automatically generates. Note how the volumeMounts field in each container references the name of this PersistentVolumeClaim template, enabling Kubernetes to mount the appropriate storage seamlessly.
Ind_statefulset.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
apiVersion: apps/v1 kind: StatefulSet metadata: name: template-pv spec: replicas: 3 selector: matchLabels: app.kubernetes.io/name: template-pv template: metadata: labels: app.kubernetes.io/name: template-pv spec: containers: - name: template-pv image: "docker.io/busybox:latest" command: - sleep - "infinity" volumeMounts: - mountPath: /mnt name: template-pv volumeClaimTemplates: - metadata: name: template-pv spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: local-path |
By the above configurations, the pv and pvc automatically bound one for each pod after creating that.
Check the PV and PVC association with the pods as shown below:
In the above, we can see that each pod is associated with PV and PVC separately, one for each, and we can also see that files are not shared among the pods as the memory is not shared.
Conclusion
In conclusion, persistent storage is critical to managing stateful workloads in Kubernetes. Whether utilizing Deployments or StatefulSets, allocating PersistentVolumes ensures data integrity and availability across pods. By understanding and implementing persistent storage solutions effectively, Kubernetes practitioners can maintain the reliability and consistency of their applications. As Kubernetes continues to evolve, staying informed about emerging trends and technologies in storage management will be essential for optimizing workload performance and scalability. The seamless integration of persistent storage into Kubernetes workflows is indispensable for achieving robust and resilient containerized environments.
Drop a query if you have any questions regarding Kubernetes and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
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 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 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. What is persistent storage in Kubernetes?
ANS: – Persistent storage in Kubernetes refers to retaining data even when pods are terminated or moved. It allows stateful applications to store and access data reliably across different instances and nodes.
2. What are PersistentVolumes (PVs) and PersistentVolumeClaims (PVCs) in Kubernetes?
ANS: – PersistentVolumes (PVs) are storage resources provisioned by an administrator in a Kubernetes cluster. PersistentVolumeClaims (PVCs) are requests made by pods for specific storage resources (PVs) based on their requirements. PVCs abstract the underlying storage infrastructure from the pods.
WRITTEN BY Bhanu Prakash K
K Bhanu Prakash is working as a Subject Matter Expert in CloudThat. He is proficient in Managing and configuring AWS Infrastructure as well as on Kubernetes and DevOps tools like Terraform, ansible, Jenkins, and Git. He is very keen on learning new technologies and publishing blogs for the tech community.
Click to Comment