Day 36:Managing Persistent Volumes in Your Deployment 💥
Day#36 Of 90 Days Of DevOps Challenge
Table of contents
- 🌟Introduction
- 📌What are Persistent Volumes (PVs) in Kubernetes?
- What are Persistent Volume Claims (PVCs) in Kubernetes?
- 🖥How PVs and PVCs Work Together
- 🖋Commands for Managing PVs
- 🖋Commands for Managing PVCs
- 📃Task 1: Adding a Persistent Volume to Your Deployment
- 📚Task 2: Accessing Data in the Persistent Volume
- 👋Conclusion
🌟Introduction
Welcome back, #90daysofDevOps challengers!
In this blog post, we'll delve into the world of Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) in Kubernetes.
These essential components play a crucial role in managing storage in your cluster.
By the end of this guide, you'll have a clear understanding of how to incorporate Persistent Volumes into your Kubernetes deployment, access data stored within them, and enhance your Kubernetes skills.
📌What are Persistent Volumes (PVs) in Kubernetes?
In Kubernetes, a Persistent Volume (PV) is a fundamental resource representing a piece of storage in the cluster. It's provisioned independently of any specific Pod or application. PVs are created and managed by cluster administrators using YAML or JSON configuration files.
✔Key Characteristics of PV
Provisioned Storage: PVs are typically provisioned in advance by administrators. For example, you can create a PV using a YAML file like this:
kubectl create -f pv.yaml
where
pv.yml
contains the PV specification.Lifecycle Independence: PVs exist until explicitly deleted, regardless of whether any Pod is using them. They persist across Pod restarts.
Access Modes: PVs support different access modes. Common modes include:
ReadWriteOnce (can be mounted as read-write by a single node)
ReadOnlyMany (can be mounted as read-only by multiple nodes)
ReadWriteMany (can be mounted as read-write by multiple nodes)
Reclaim Policies: Administrators can define a reclaim policy, which determines what happens to the storage when the PV is released. Common policies include "Retain," "Delete," and "Recycle."
What are Persistent Volume Claims (PVCs) in Kubernetes?
A Persistent Volume Claim (PVC) is a user's request for storage resources from a PV. PVCs are created by application developers or users and specify their storage needs.
✔Key Characteristics of PVC
Dynamic Provisioning: PVCs can be dynamically provisioned if no matching PV with the required specifications is found. Kubernetes will automatically create a suitable PV if dynamic provisioning is enabled for the storage class. You can create a PVC like this:
kubectl create -f pvc.yaml
where
pvc.yml
contains the PVC specification.Binding to PVs: When a PVC is created, Kubernetes tries to bind it to a suitable PV based on its requirements (e.g., capacity, access mode, storage class). To create a binding, use the
kubectl get pvc
command to check the status:kubectl get pvc
Lifecycle Bound to Pods: PVCs have a lifecycle bound to the Pods that use them. When a Pod is deleted or scaled down, the PVC is also released. When the last Pod using a PVC is deleted, the PVC can either be retained (depending on the PV's reclaim policy) or deleted.
🖥How PVs and PVCs Work Together
The interaction between PVs and PVCs is essential for Kubernetes storage management:
Cluster administrators create one or more PVs to make storage resources available in the cluster.
Application developers or users create PVCs that request specific storage resources based on their needs.
Kubernetes attempts to bind each PVC to a suitable PV. Check the binding status with:
kubectl get pvc
Pods can reference PVCs in their specifications, allowing them to use the associated storage.
🖋Commands for Managing PVs
List Persistent Volumes: To view all the PVs in your cluster:
kubectl get pv
Describe a Persistent Volume: To get detailed information about a specific PV:
kubectl describe pv <pv-name>
Delete a Persistent Volume: To delete a PV:
kubectl delete pv <pv-name>
🖋Commands for Managing PVCs
List Persistent Volume Claims: To list all the PVCs in your cluster:
kubectl get pvc
Describe a Persistent Volume Claim: To get detailed information about a specific PVC:
kubectl describe pvc <pvc-name>
Delete a Persistent Volume Claim: To delete a PVC:
kubectl delete pvc <pvc-name>
Check PVC Binding Status: To check if a PVC is successfully bound to a PV, look at the The
STATUS
column in the PVC list. It should show "Bound" when successfully bound.Check Available Storage Classes: To see the available storage classes in your cluster:
kubectl get storageclass
Set Storage Class in PVC: When creating a PVC, you can specify a storage class by setting the
storageClassName
field in the PVC's YAML file.
These commands will help you manage and interact with Persistent Volumes and Persistent Volume Claims in your Kubernetes cluster effectively.
If you still need more detailed information, please refer to the official Kubernetes documentation on Persistent Volumes.
Now, let's move to the Tasks of the Day...
📃Task 1: Adding a Persistent Volume to Your Deployment
Let's get started with Task 1, where we'll add a Persistent Volume to our Deployment for a "todo" application.
Step 1: Create a Persistent Volume (pv.yml)
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-todo-app
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/tmp/data"
This YAML file defines a PV with a capacity of 1Gi, ReadWriteOnce access mode, and a hostPath where the data will be stored.
Step 2: Apply the Updates
kubectl apply -f pv.yml -n <namespace-name>
Step 3: Create a Persistent Volume Claim (pvc.yml)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-todo-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
The PVC YAML file specifies a PVC named "pvc-todo-app" with a request for 500Mi of storage.
Step 4: Apply the Updates
kubectl apply -f pvc.yml -n <namespace-name>
Step 5: Update your Deployment(deployment.yml)
After creating the PV and PVC, update your deployment.yml file to include the Persistent Volume Claim. Ensure it references the PVC you just created.
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: vishalphadnis/todo-app
ports:
- containerPort: 8000
volumeMounts:
- name: todo-app-data
mountPath: /app
volumes:
- name: todo-app-data
persistentVolumeClaim:
claimName: pvc-todo-app
Step 6: Apply the Updated Deployment
Apply the updated deployment using the following command:
kubectl apply -f deployment.yml -n <namespace-name>
⚠ Remember: To apply changes or create files in your Kubernetes deployments, each file must be applied separately.⚠️
Step 7: Verification
Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster using these commands:
kubectl get pods -n < namespace-name >
kubectl get pv -n < namespace-name >
📚Task 2: Accessing Data in the Persistent Volume
Now that your Persistent Volume is integrated into the Deployment, let's see how you can access the data stored within it.
Step 1: Connect to a Pod
Execute the following command to connect to a Pod in Deployment
Step 2: Verify Data Access
Within the connected Pod, verify that you can access the data stored in the Persistent Volume. You should be able to read and write data to/from the volume.
kubectl exec -it <pod-name> -n < namespace-name > -- /bin/bash
Step 3: Delete the Pod and verify the persistent volume
kubectl delete pods <pod-name> -n < namespace-name >
kubectl get pods -n < namespace-name >
The pod, which we created in previous steps, will automatically create a new pod.
Now again ,Within the connected Pod, verify that you can access the data stored in the Persistent Volume.
👋Conclusion
Congratulations!💥 You've successfully managed Persistent Volumes in your Kubernetes deployment.
You've learned how to create a PV, and PVC, integrate them into your deployment, and access data within a Persistent Volume.
That's all for today's task. If you have any questions or need further assistance, feel free to ask in the comments below.
Keep exploring, and stay tuned for more Kubernetes adventures ahead! 🔒🔑🛡️
Happy DevOps-ing! 💪🚀
🚀 Stay tuned for more insightful Kubernetes blogs in the days ahead! 🎉
Thank you for joining us on this exciting Day 35 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.
So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :
Hashnode: vishaltoyou.hashcode.dev