How to create and use a PersistentVolume & PersistentVolumeClaim

Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


Understanding how to create a PersistentVolume and PersistentVolumeClaim and use them inside a pod.



Hi, in this video, we'll create PersistentVolume and PersistentVolumeClaim and see how we can use that inside the pod. First, let's check whether any PV or PVC is present on this cluster. We do not have any PersistentVolume or PersistentVolumeClaim, so now we'll create the PV. In this particular demo, we'll be using static provisioning means, so we'll be creating the PersistentVolume manually.

You can see the kind is PersistentVolume, the name is demo-pv, StorageClass is manual, and we define the capacity, accessMode, ReadWriteOnce, and the hostPath. So basically, it is a hostPath, which is just used for testing purposes and not in production. But for this particular demo, we'll stick to a hostPath. This means it will be mounting the directory /mnt/data from one of the hosts, which is on the cluster.

Creating a PersistentVolume

I already have a node that is part of this particular cluster, and in this directory, you can see if I use the command pwd, I have created a file called index.html with content that says hello inside the directory. Now let's create this PV using kubectl create -f pv. PV is created. Now, the next step is to create the PVC. First, let's see the contents by using cat pvc.

Creating a PersistentVolumeClaim

The PersistentVolumeClaim kind is PersistentVolumeClaim, and the name is demo-pvclaim. Again, StorageClassName is manual, and acessMode should match the PV, and the resources we are requesting are 3Gi. We can use kubectl create -f pvc. Let's see if our PV and PVC are created using the command kubectl get pv, pvc. They both are created, and the status is bound, meaning the PV has been bound to the PVC. Basically, the status is bound, and the StorageClass is manual, so we have things ready to be used inside the pod.

Using PV and PVC with a pod

Let's see the contents of the pod now. In this particular scenario, I'm targeting these two on the specific node where that particular directory is present so that it takes the Nginx file. So first of all, to use PersistentVolume inside a pod, you have to specify in the pod section, spec volumes give us PersistentVolumeClaim, and give the name of the PersistentVolumeClaim.

In this particular case, we created a PVC with the name demo-pvclaim. So in the volume section, we have provided PersistentVolumeClaim with a claim name as demo-pvclaim. The node name is where the directory and the index.html file were already created. And then, it's a simple nginx container. So in the VolumeMount section, we are mounting the volume, which is the PV volume, which is defined above, to the mount path/usr/nginx/html.

Now, let's go ahead and create this pod by using kubectl create -f pod.yaml and verify it by using kubectl get pods. We can see that we have this container created, and it's running. So our pod is running, and we can do an exec into the pod with kubectl exec -it task-pv-pod -- sh. And let's go to the directory with cd usr/share/nginx/html. We should have index.html. So it has the content, and if we use curl localhost, you will get index.html. If I exit and use the command kubectl get pods -owide and the local IP as well, and curl it by using the command curl [Local_IP:Port_Number], it should be giving us the response as hello.

Basically, we have successfully mounted the PersistentVolume inside a pod. Now, when we delete the pod using kubectl delete pod task-pv-pod, we can see that the pod is getting deleted, and we can still see the file is already there. So, we have the file, and basically, we have the data. And although we don't have the pod, if we use the command kubectl get pv and Kubectl get pvc, we can see that we have both the PVC and PV.

In conclusion

In this particular case, since the reclaim policy is also retained, even if you delete the PVC, the PV won't get deleted. Even if you delete the PV, the hostPath volume, which is the /mnt/data, won't get deleted. All these have to be manually deleted, so it's a straightforward example of using PersistentVolume and PersistentVolumeClaim inside a pod. That's it for this lecture. Thank you for watching. See you in the next one.