Day 36 Task: Managing Persistent Volumes in Your Deployment ๐ฅ
A Comprehensive Guide to Managing Your Deployment Storage"
๐ถ What are Persistent Volumes in K8s
In Kubernetes, a Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator. A Persistent Volume Claim (PVC) is a request for storage by a user. The PVC references the PV, and the PV is bound to a specific node. Read official documentation of Persistent Volumes.
Problem:- As we know if the container (Pod) gets down then it will be restarted but the data present in the container/pod will be lost. Using Hostpath we can eliminate this data loss but in the K8s cluster Pod might get restarted on another worker so in this case Hostpath will not be accessible.
Solution:- To avoid data loss in the K8s cluster we'll use Persistent Volumes.
A Persistent Volume(PV) is a cluster-wide resource that you can use to store data in a way that persists beyond the lifetime of the Pod. The PV is not backed by locally attached storage on a work node but by a networked storage system such as EBS or NFS or a Distributed File System like Ceph.
Persistent Volume Claim(PVC):-
To Use PV you need to claim it first, using a persistent volume Claim.
The PVC request a PV with your desired specification(Size, Access, Speed, etc.) from K8s and once a suitable PV is found it is bound to be PVC.
After a successful bound to a Pod, you can mount it as a volume.
Prerequisites Make instances of t2.medium.
Install minikube (minikube install), login dockerhub, build docker, push image to docker hub and use that image for your deployment.yml
๐ถ Task 1: Add a Persistent Volume to your Deployment todo app.
Create a Persistent Volume using a file on your node. Template
apiVersion: v1 kind: PersistentVolume metadata: name: pv-django spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: "/mnt/data"
Create a Persistent Volume Claim that references the Persistent Volume. Template
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-django spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi
Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file looks like this template.
apiVersion: apps/v1 kind: Deployment metadata: name: todo-app-deployment spec: replicas: 1 selector: matchLabels: app: todo-app template: metadata: labels: app: todo-app spec: containers: - name: todo-app image: nileshsahare/django-todo:latest ports: - containerPort: 8000 volumeMounts: - name: todo-app-data mountPath: /app volumes: - name: todo-app-data persistentVolumeClaim: claimName: pvc-django
Apply the updated deployment using the command:
kubectl apply -f deployment.yml
Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster. Use this command
kubectl get pods
,kubectl get pv
โ ๏ธ Don't forget: To apply changes or create files in your Kubernetes deployments, each file must be applied separately. โ ๏ธ
๐ถ Task 2: Accessing data in the Persistent Volume.
Connect to a Pod in your Deployment using the command:
kubectl exec -it <pod-name> -- /bin/bash
Verify that you can access the data stored in the Persistent Volume from within the Pod.
kubectl exec -it todo-app-deployment-76b6445598-mxcml -- /bin/bash
Need help with Persistent Volumes? Check out this video for assistance.
Delete the pod in which we made changes.
Check whether the new pod have created files or not.
Happy Learning :)
If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐ #devops #90daysofdevop #jenkins #k8s