Day 36 Task: Managing Persistent Volumes in Your Deployment 💥

"I'm a 3rd-year Computer Engineering student at Marwadi University with skills in C++, web development (MERN stack), and DevOps tools like Kubernetes. I contribute to open-source projects and share tech knowledge on GitHub and LinkedIn. I'm learning cloud technologies and app deployment. As an Internshala Student Partner, I help others find jobs and courses." now currently focusing on #90DaysOfDevops
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.
Today's tasks:
Task 1:
Add a Persistent Volume to your Deployment todo app.
Create a Persistent Volume using a file on your node.
Create a Persistent Volume Claim that references the Persistent Volume.
Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file look like this
Apply the updated deployment using the command:
kubectl apply -f deployment.ymlVerify 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 commands
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 command : `kubectl exec -it -- /bin/bash
`
- Verify that you can access the data stored in the Persistent Volume from within the Pod
Let’s do task 1
Create a pv. yml Persistent Volume using a file on your node.
apiVersion: v1 kind: PersistentVolume metadata: name: pv-app spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: manual hostPath: path: "/tmp/data"apiVersion: v1: Specifies the API version used to create the object.kind: PersistentVolume: Declares that this is a PersistentVolume (PV), which provides storage to pods.metadata: name: pv-app: Names the PV as "pv-app".spec:: Contains the specifications of the PV.capacity: Defines the storage capacity (not shown fully here, but typically likestorage: 1Gi).accessModes: - ReadWriteOnce: Allows the volume to be mounted as read-write by a single node.persistentVolumeReclaimPolicy: Retain: Prevents data deletion after the PV is released.hostPath: path: "/tmp/data": Uses a directory from the host as storage.
This configuration sets up a local storage volume with 1GB capacity that can be used by one pod at a time and retains data after release.
Apply the Persistent Volume:
kubectl apply -f pv.yml
Create a pvc.yml Persistent Volume Claim that references the Persistent Volume.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-app namespace: app-deployment spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: manualapiVersion: v1: Specifies the API version for the resource.v1is the stable version for core Kubernetes resources like PV and PVC.kind: PersistentVolume: Indicates that this is aPersistentVolumeresource. APersistentVolumeis a piece of storage in the cluster that has been provisioned by an administrator.metadata:name: pvc-app: A name for thisPersistentVolume. It’s used to identify the resource in the Kubernetes cluster.
spec:accessModes:- ReadWriteOnce: Specifies the access modes for thePersistentVolume.ReadWriteOncemeans the volume can be mounted as read-write by a single node.
resources:requests:storage: 500Mi: Specifies the amount of storage requested. However, this field is used inPersistentVolumeClaim, not inPersistentVolume. ForPersistentVolume, you should define thecapacityfield instead.
Apply the Persistent Volume Claim:
kubectl apply -f pvc.yml
Create file name called storageclass.yml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: manual provisioner: k8s.io/minikube-hostpath # This is for local testing; adjust as needed for your environmentprovisioner:k8s.io/minikube-hostpathThe
provisionerfield specifies which provisioner will be responsible for creating and managing Persistent Volumes (PVs).k8s.io/minikube-hostpathis a provisioner used for Minikube, a Kubernetes environment designed for local testing. ThehostPathprovisioner stores data directly on the host node's filesystem, which is useful in development and testing scenarios but not recommended for production environments.
Use Case
This StorageClass is likely meant for local development and testing using Minikube. When a PersistentVolumeClaim (PVC) is created and uses this StorageClass, Kubernetes will use the hostPath provisioner to create storage directly on the local machine where Minikube is running.
In production environments, you would use different provisioners (like aws-ebs for AWS, gce-pd for GCP, etc.), depending on the cloud platform or storage solution you're using.
kubectl apply -f storageclass.yml

Let’s update deployment file so create new update_deployment_3.0.yml file to include the Persistent Volume Claim.
apiVersion: apps/v1 kind: Deployment metadata: name: django-todo-deployment namespace: app-deployment spec: replicas: 2 selector: matchLabels: app: django-todo template: metadata: labels: app: django-todo spec: containers: - name: django-todo image: anandraval12/django-todo-app:latest ports: - containerPort: 8000 env: - name: DATABASE_URL valueFrom: secretKeyRef: name: django-secret key: database-url - name: SECRET_KEY valueFrom: secretKeyRef: name: django-secret key: secret-key volumeMounts: - name: todo-data mountPath: /app volumes: - name: todo-data persistentVolumeClaim: claimName: pvc-appApply the updated deployment:
kubectl apply -f deployment.yml
Verify the Persistent Volume has been added to your Deployment.
kubectl get pods -n <name-space name> kubectl get pv kubectl get pvc



Let’s do task 2
Get the Pod details:
kubectl get pods
Connect to a Pod in your Deployment:
kubectl exec -it <pod-name> -- /bin/bash
Verify that you can access the data stored in the Persistent Volume from within the Pod:
cd /app/ echo "Hello" > /app/test.txt lsDelete the pod and verify the data persistence:
kubectl delete pod <pod-name> kubectl get pods kubectl exec -it <new-pod-name> -- /bin/bash cd /app/ ls cat test.txtThankyou for reading !!!!!!




