Mastering Kubernetes Persistent Volumes: A Guide for Beginners | Day 36 of | 90DaysOfDevOps
Table of contents
Understanding Persistent Volumes in Kubernetes π€
In the Kubernetes world, Persistent Volumes provide a durable way to store data independently of Pods' lifecycles. Think of them as the secret keepers of your application's crucial data. πΎ
Task 1: Adding Persistence to Your Todo App π
Let's dive into practicality! We'll enhance our Todo app by introducing a Persistent Volume. Follow these steps:
Step 1: Create a Persistent Volume (PV)
Start by creating a PV using a file on your node. Here's an example manifest (persistent-volume.yml
):
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
namespace: mysql
labels:
app: mysql
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/path/on/your/node"
Apply the PV using kubectl apply -f persistent-volume.yml
.
Step 2: Create a Persistent Volume Claim (PVC)
Now, create a PVC that references the PV. Example manifest (persistent-volume-claim.yml
):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Apply the PVC using kubectl apply -f persistent-volume-claim.yml
.
Step 3: Update Deployment Configuration
Update your deployment.yml
to include the Persistent Volume Claim. Add the following snippet:
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 updated deployment using:
kubectl apply -f deployment.yml
Step 4: Verification
Verify that the Persistent Volume has been added to your Deployment:
kubectl get pods
kubectl get pv
Task 2: Accessing Data in the Persistent Volume ποΈ
Now, let's connect to a Pod in your Deployment and ensure we can access the data stored in the Persistent Volume.
Step 1: Connect to a Pod
Use the command:
kubectl exec -it <pod-name> -- /bin/bash
Step 2: Verify Access
Inside the Pod, navigate to the mounted path (e.g., /app/data
) and verify access to your data.
cd /app/data
ls
Congratulations! You've just added persistence to your Todo app in Kubernetes. π
Conclusion:
πIn this blog post, I learned how to add a persistent volume to a Node Todo app on Kubernetes. I learned how to create a PV resource, a PVC resource, a Deployment resource, and a Service resource for the Node Todo app. I also learned how to apply the manifests and verify the status of the resources on the Kubernetes cluster.
I hope you found this post useful and informative. If you have any questions or feedback, feel free to leave a comment below or reach out to me on LinkedIn or GitHub.
Thanks for reading and happy coding! π