This is#90DaysofDevopschallenge under the guidance ofShubham Londhesir.
Introduction
Today marks a pivotal step in our #90daysofDevOps challenge as we delve into the realm of managing Persistent Volumes (PV) in Kubernetes deployments. Let's break down this intricate task into easily digestible nuggets of knowledge.
What are Persistent Volumes in K8S
In the dynamic world of Kubernetes, where applications thrive and evolve, Persistent Volumes (PVs) emerge as the secret ingredient behind the scenes, orchestrating a seamless dance of data storage and retrieval. But what exactly are Persistent Volumes, and why are they indispensable in our Kubernetes journey?
Decoding Persistent Volumes
Storage Sanctuaries: Picture PVs as enchanted sanctuaries nestled within the Kubernetes cluster, reserved exclusively for storing the lifeblood of our applications โ data. These sanctuaries, crafted by the hands of cluster administrators, offer a sanctuary for our application's valuable data assets.
Reserved Parking Spots: Think of PVs as reserved parking spots in a bustling city, each with its unique capacity and capabilities. Just as we reserve a spot for our vehicle, PVs allocate dedicated storage resources for our applications, ensuring they have a safe haven to store their critical data.
Abstraction Layer: PVs shield applications from the complexities of storage management, providing a layer of abstraction that simplifies the storage provisioning process. Developers can focus on building resilient applications, knowing that PVs handle the intricacies of storage allocation behind the scenes.
Why Persistent Volumes Matter
Data Durability: In the ever-changing landscape of containerized applications, data durability is paramount. PVs offer a lifeline for our data, ensuring it persists across application lifecycles and deployments, even in the face of adversity.
Scalability on Demand: With PVs, we have the power to scale our storage resources on demand, adapting to the evolving needs of our applications. Whether it's a surge in data volume or a spike in user activity, PVs provide the flexibility to accommodate dynamic storage requirements.
Cross-Cluster Portability: PVs transcend the boundaries of individual Kubernetes clusters, offering a portable storage solution that can seamlessly migrate across environments. This portability ensures consistency and reliability, regardless of the underlying infrastructure.
High Availability: By configuring PVs for high availability and redundancy, we ensure that our data remains accessible and resilient, even in the face of hardware failures or system disruptions. PVs act as guardians, safeguarding our data assets with unwavering vigilance.
Task1
To create a Persistent Volume using a file on your node, Follow these steps:
Create a file named
pv.yaml
.Inside
pv.yaml
, write the following code:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-django-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/mnt/data"
Save the
pv.yaml
file.Apply the configuration using the following command:
kubectl apply -f pv.yaml
- Create a file
pvc.yaml
and write the code for Persistent Volume Claim.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-django-todo-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
kubectl apply -f pvc.yaml
Below is the YAML code for the Deployment configuration in the deploymentvolumes.yaml file:
apiVersion: apps/v1 kind: Deployment metadata: name: django-todo-deployment spec: replicas: 3 selector: matchLabels: app: django-todo-app template: metadata: labels: app: django-todo-app spec: containers: - name: django-todo-app image: akash2097/django-todo:latest ports: - containerPort: 8080 volumeMounts: - name: django-todo-app-data-volume mountPath: tmp/app/data volumes: - name: django-todo-app-data-volume persistentVolumeClaim: claimName: pvc-django-todo-app
kubectl apply -f deploymentvolumes.yaml
To verify that the Persistent Volume has been added to your Deployment:
Check Pods:
kubectl get pods
Check Persistent Volumes:
kubectl get pv
Task 2:
To connect to a Pod in your Deployment and access data in the Persistent Volume, follow these steps using the command line:
Connect to the Pod using below command:
kubectl exec -it <pod-name> -- /bin/bash kubectl exec -it django-todo-deployment-5bd46cbf47-84gbp -- /bin/bash kubectl exec -it django-todo-deployment-5bd46cbf47-q95p6 -- /bin/bash kubectl exec -it django-todo-deployment-5bd46cbf47-w9dvh -- /bin/bash
Replace with the name of your Pod.
Create a File in the Pod: Once you're inside the Pod, navigate to the desired directory. For example:
cd /tmp/app
Create a file or perform any operations you need to within the Pod's interactive shell.
Access Data in the Persistent Volume:
Check the contents of the file you created in the Pod to ensure you can access the data stored in the Persistent Volume.
Exit the Pod:
Once you've finished your operations, exit from the Pod's interactive shell.
By following these steps, you can connect to a Pod in your Deployment and verify access to the data stored in the Persistent Volume.