Day 36 of 90 Days of DevOps Challenge: Managing Persistent Volumes in Kubernetes
What are Persistent Volumes in Kubernetes?
In Kubernetes, Persistent Volumes (PV) provide a way to store data beyond the lifecycle of individual pods. They’re independent storage units in the cluster, provisioned by administrators.
On the other hand, a Persistent Volume Claim (PVC) is how applications (or users) request storage resources from the PVs. By binding a PVC to a PV, your pods can access data stored across restarts and rescheduling, ensuring data persistence.
📖 For more details, check out the official documentation on Persistent Volumes.
Today’s Tasks:
Task 1: Add a Persistent Volume to Your Deployment Todo App
In this task, we will add a Persistent Volume to your todo-app Kubernetes deployment. Persistent storage is crucial for any app that needs to retain data, such as databases or file storage.
Steps:
Create a Persistent Volume (PV) using a YAML file on your node.
pv.yml:
apiVersion: v1 kind: PersistentVolume metadata: name: pv-todo-app spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: "/tmp/data"
Create a Persistent Volume Claim (PVC) that requests storage from the PV.
pvc.yml:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-todo-app spec: accessModes: - ReadWriteOnce resources: requests: storage: 500Mi
Update the deployment file to use the Persistent Volume Claim.
deployment.yml:
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: rishikeshops/todo-app ports: - containerPort: 8000 volumeMounts: - name: todo-app-data mountPath: /app volumes: - name: todo-app-data persistentVolumeClaim: claimName: pvc-todo-app
Apply the Persistent Volume and Claim:
kubectl apply -f pv.yml kubectl apply -f pvc.yml kubectl apply -f deployment.yml
Verify the addition of Persistent Volume:
kubectl get pv kubectl get pvc kubectl get pods
Note: Remember to apply each YAML file separately before applying the deployment changes.
Task 2: Accessing Data in the Persistent Volume
Once the Persistent Volume is added to your app, it’s time to ensure that the data can be accessed from within the pod.
Steps:
Connect to a Pod in your deployment using this command:
kubectl exec -it <pod-name> -- /bin/bash
Verify Access to the data stored in the Persistent Volume from within the Pod:
Navigate to the mounted path (
/app
) inside the container.Check if the data is present and accessible.
Need help understanding how Persistent Volumes work? Check out this helpful video tutorial.
🎉 Conclusion
Today, we explored how to use Persistent Volumes to ensure that your Kubernetes applications can store data permanently, even across pod restarts. This is a crucial feature for any stateful application. With the addition of PVs and PVCs, your app can now manage data efficiently.
What you learned:
How to create a Persistent Volume (PV) and Persistent Volume Claim (PVC).
How to integrate them into your Kubernetes deployments.
How to verify and access data stored in PVs from your app's pod.
Keep building your Kubernetes skills, and don’t forget to share your progress on LinkedIn with #90DaysOfDevOps! 🚀