Managing Persistent Volumes in Kubernetes : A Comprehensive Guide for Beginners
Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) are essential for managing storage in Kubernetes. This guide will walk you through adding and using Persistent Volumes in your Kubernetes deployments, ensuring your applications have the storage they need.
Understanding Persistent Volumes
Persistent Volumes (PVs):
Think of a Persistent Volume (PV) as a piece of storage in your Kubernetes cluster that is set up by an administrator. It’s like having a dedicated hard drive in your computer, but in this case, it’s in the Kubernetes environment.
PVs provide storage that can persist beyond the lifecycle of individual Pods, which means the data remains even if the Pod that uses it is deleted.
Persistent Volume Claims (PVCs):
A Persistent Volume Claim (PVC) is a request for storage by a user. It’s like asking for a specific amount of space on that hard drive.
PVCs ensure that the requested storage is available to the Pods. The PVC binds to a specific PV, making the storage accessible to your application.
Learn more about Persistent Volumes.
Step-by-Step Tasks for Managing Persistent Volumes
Task 1: Add a Persistent Volume to Your Deployment Todo App
Step 1: Create a Persistent Volume
Create a file named
pv.yml
with the following content:apiVersion: v1 kind: PersistentVolume metadata: name: my-pv spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce hostPath: path: /mnt/data
apiVersion: Specifies the version of the Kubernetes API you’re using.
kind: Defines the type of resource, in this case, a PersistentVolume.
metadata: Contains the name of the PV.
spec: Specifies the details of the PV, such as its capacity (1Gi) and access modes (ReadWriteOnce), which means the volume can be mounted as read-write by a single node.
hostPath: Defines the path on the host node where the data will be stored.
Apply the Persistent Volume to your cluster using the command:
kubectl apply -f pv.yml
This command tells Kubernetes to create the Persistent Volume as defined in the
pv.yml
file.
Step 2: Create a Persistent Volume Claim
Create a file named
pvc.yml
with the following content:apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
PersistentVolumeClaim (PVC): Specifies a request for storage.
metadata: Contains the name of the PVC.
spec: Specifies the details of the PVC, such as access modes and the amount of storage requested (1Gi).
Apply the Persistent Volume Claim to your cluster using the command:
kubectl apply -f pvc.yml
This command creates the Persistent Volume Claim as defined in the
pvc.yml
file.
Step 3: Update Your Deployment to Use the Persistent Volume Claim
Update your
deployment.yml
file to include the Persistent Volume Claim:apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment namespace: my-namespace spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: my-image volumeMounts: - mountPath: "/mnt/data" name: my-storage volumes: - name: my-storage persistentVolumeClaim: claimName: my-pvc
volumeMounts: Mounts the volume to the container at the specified path (
/mnt/data
).volumes: Defines the volume that is mounted, referencing the PVC (
my-pvc
).
Apply the updated deployment:
kubectl apply -f deployment.yml
This command updates your deployment to use the specified Persistent Volume Claim.
Step 4: Verify the Persistent Volume Creation
Check the status of your Persistent Volume:
kubectl get pv
Check the status of your Persistent Volume Claim:
kubectl get pvc
Check the status of your Pods:
kubectl get pods
You should see your Persistent Volume, Persistent Volume Claim, and Pods listed and in a healthy state.
Task 2: Access Data in the Persistent Volume from Within a Pod
Step 1: Connect to a Pod
List your Pods to find the name of one of your running Pods:
kubectl get pods
Connect to the Pod using the following command (replace
<pod-name>
with the name of your Pod):kubectl exec -it <pod-name> -- /bin/bash
This command opens an interactive terminal inside the specified Pod.
Step 2: Verify Access to Data
Once inside the Pod, navigate to the mount path to check if the Persistent Volume is accessible:
bashCopy codecd /mnt/data ls
You should see any files or directories stored in the Persistent Volume. This confirms that the Persistent Volume is properly mounted and accessible from within the Pod.
Conclusion
By mastering Persistent Volumes and Persistent Volume Claims, you can effectively manage storage in your Kubernetes deployments. This ensures your applications have the necessary storage and data persistence to function properly. Keep exploring and expanding your knowledge of Kubernetes to become proficient in managing containerized applications!
Keep learning and expanding your Kubernetes knowledge! 💥🙌
Subscribe to my newsletter
Read articles from Urvish Suhagiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Urvish Suhagiya
Urvish Suhagiya
Exploring the world of DevOps 🌐.