How to create a NFS PersistentVolume

Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism


What are the basics of creating a NFS PersistentVolume and how to provision it with a pod inside a cluster.



Hi, in this video we'll be creating a PersistentVolume backed by an NFS server, and see how we can use that inside a pod. First, we'll see the pv [SP], now we'll create this pv, and it is a pv with the manual storageClass capacity defined. ReadWriteMany access mode. And this particular section is where we define nfs. So, in nfs we define what is a server, and what is a path that we have shared.

How to create a NFS PersistentVolume?

Let's create the PV with the kubectl create -f pv.yaml command. The PV is created, now let's look at the PVC. PVC is, again, very straightforward. There are storageClassName, accessMode, and the resources where we define the request section. We will use the command kubectl create -f pvc.yaml to create a PVC. So, we have got PV and PVC.

We have both the PV and PVC created, and it is bounded because it's the manual one that we have created, so it's already in a bound mode. It will not wait for the pod to come up first to get bounded. And now, let's see how we can use that inside a pod.

How to provision PersistentVolume with a pod inside a cluster?

In the pod spec, we have defined the volume section, and in the volume section, we have used the PersistentVolumeClaim that we just created, PVC-nfs. And in the container section, we have used image alpine, and in the volumeMount section, we have mounted that inside the container under the /mnt directory. And this particular pod will just cut out the memory info and store that as a memory.txt file. In the NFS server, it should also be creating this particular file. Let's go ahead and create this pod using kubectl create -f pod.yaml. The pod is created, and it's running. We can do an exec using kubectl exec -it pod-using-nfs - sh. And let's go to the mnt directory with cd /mnt. We should see a memory.txt file, and you can see there is data in it as well.

Now, I have an NFS server already opened, and previously you can see it didn't have any data, so let's do ls again. Now, it should have the memory.txt, and it is also getting updated. We can see how an NFS server situated outside the cluster can be used as a PersistentVolume inside a pod. This is a scenario that can be used in production as well, where you have a volume outside of the cluster so that even if the cluster goes down, your data remains safe and can be recoverable. That's it for this lecture. Thank you for watching. See you in the next one.

Don't stop now, check out your next lesson