Understanding Kubernetes Persistent Volumes and Persistent Volume Claims: An Example
In the dynamic world of container orchestration, Kubernetes offers a robust ecosystem for managing and scaling applications. One of the key aspects of this is persistent storage, which is handled through Persistent Volumes (PV) and Persistent Volume Claims (PVC). Let's dive into an example to understand how to create and use PVCs in Kubernetes.
What are Persistent Volumes and Persistent Volume Claims?
Before we delve into the example, let's briefly understand what Persistent Volumes and Persistent Volume Claims are.
- Persistent Volumes (PV) are network storage resources provided by various storage providers like EBS, NFS, or even cloud storage services. They are independent of the lifecycle of the pods and can outlive them.
- Persistent Volume Claims (PVC) are requests for storage by a pod. They are bound to a PV, and the pod can use the storage until the PVC is deleted.
Creating a Persistent Volume
Let's start by creating a PV. For this example, we'll use an EBS volume. Here's a simple YAML definition:

apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
awsElasticBlockStore:
fsType: ext4
volumeID: "vol-abcdefghijklmnopqrstuvwxyz"
Creating a Persistent Volume Claim
Now, let's create a PVC that will use this PV. Here's a YAML definition for a PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
In this PVC, we're requesting 3Gi of storage with the ReadWriteOnce access mode. This PVC will be bound to the PV we created earlier.
Using the PVC in a Pod
Finally, let's use this PVC in a pod. Here's a YAML definition for a pod that mounts the PVC:

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
volumeMounts:
- name: my-volume
mountPath: /usr/share/nginx/html
volumes:
- name: my-volume
persistentVolumeClaim:
claimName: my-pvc
In this pod, we're mounting the PVC to the Nginx HTML directory. This means that any data written to this directory will be persistent and not lost when the pod is restarted.
Accessing the PVC from the Pod
To access the PVC from the pod, you can use the following environment variable:
export PVC_NAME=my-pvc export PVC_PATH=/usr/share/nginx/html
Then, in your pod, you can use these environment variables to access the PVC:

echo "Hello, World!" > $PVC_PATH/index.html
This will write a simple HTML file to the PVC, which will be persistent and accessible even if the pod is restarted.
Conclusion
In this article, we've walked through an example of creating and using a Persistent Volume Claim in Kubernetes. Understanding how to use PVCs is crucial for ensuring that your data is persistent and not lost when pods are restarted or rescheduled. With this knowledge, you can now manage persistent storage in your Kubernetes clusters effectively.









![Kubernetes Basics Explained in 10 Minutes [ DevOps ] || Techtter ||](https://i.pinimg.com/originals/b8/48/9c/b8489c0c5c02b2326b4ec2a2bb470b37.jpg)












