It is recommended to place pod’s data into some persistent volume so that data will be available even after pod termination. In Kubernetes (k8s), NFS based persistent volumes can be used inside the pods. In this article we will learn how to configure persistent volume and persistent volume claim and then we will discuss, how we can use the persistent volume via its claim name in k8s pods.
I am assuming we have a functional k8s cluster and NFS Server. Following are details for lab setup,
- NFS Server IP = 192.168.1.40
- NFS Share = /opt/k8s-pods/data
- K8s Cluster = One master and two worker Nodes
Note: Make sure NFS server is reachable from worker nodes and try to mount nfs share on each worker once for testing.
Create a Index.html file inside the nfs share because we will be mounting this share in nginx pod later in article.
[[email protected] ~]$ echo "Hello, NFS Storage NGINX" > /opt/k8s-pods/data/index.html
Configure NFS based PV (Persistent Volume)
To create an NFS based persistent volume in K8s, create the yaml file on master node with the following contents,
[[email protected] ~]$ vim nfs-pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: nfs-pv spec: capacity: storage: 10Gi volumeMode: Filesystem accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Recycle storageClassName: nfs mountOptions: - hard - nfsvers=4.1 nfs: path: /opt/k8s-pods/data server: 192.168.1.40
Save and exit the file
Now create persistent volume using above created yaml file, run
[[email protected] ~]$ kubectl create -f nfs-pv.yaml persistentvolume/nfs-pv created [[email protected] ~]$
Run following kubectl command to verify the status of persistent volume
[[email protected] ~]$ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 10Gi RWX Recycle Available nfs 20s [[email protected] ~]$
Above output confirms that PV has been created successfully and it is available.
Configure Persistent Volume Claim
To mount persistent volume inside a pod, we have to specify its persistent volume claim. So,let’s create persistent volume claim using the following yaml file
[[email protected] ~]$ vi nfs-pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nfs-pvc spec: storageClassName: nfs accessModes: - ReadWriteMany resources: requests: storage: 10Gi
Save and exit file.
Run the beneath kubectl command to create pvc using above yaml file,
[[email protected] ~]$ kubectl create -f nfs-pvc.yaml persistentvolumeclaim/nfs-pvc created [[email protected] ~]$
After executing above, control plane will look for persistent volume which satisfy the claim requirement with same storage class name and then it will bind the claim to persistent volume, example is shown below:
[[email protected] ~]$ kubectl get pvc nfs-pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE nfs-pvc Bound nfs-pv 10Gi RWX nfs 3m54s [[email protected] ~]$ [[email protected] ~]$ kubectl get pv nfs-pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE nfs-pv 10Gi RWX Recycle Bound default/nfs-pvc nfs 18m [[email protected] ~]$
Above output confirms that claim (nfs-pvc) is bound with persistent volume (nfs-pv).
Now we are ready to use nfs based persistent volume nside the pods.
Use NFS based Persistent Volume inside a Pod
Create a nginx pod using beneath yaml file, it will mount persistent volume claim on ‘/usr/share/nginx/html’
[[email protected] ~]$ vi nfs-pv-pod apiVersion: v1 kind: Pod metadata: name: nginx-pv-pod spec: volumes: - name: nginx-pv-storage persistentVolumeClaim: claimName: nfs-pvc containers: - name: nginx image: nginx ports: - containerPort: 80 name: "nginx-server" volumeMounts: - mountPath: "/usr/share/nginx/html" name: nginx-pv-storage
Save and close the file.
Now create the pod using above yaml file, run
[[email protected] ~]$ kubectl create -f nfs-pv-pod.yaml pod/nginx-pv-pod created [[email protected] ~]$ [[email protected] ~]$ kubectl get pod nginx-pv-pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-pv-pod 1/1 Running 0 66s 172.16.140.28 k8s-worker-2 <none> <none> [[email protected] ~]$
Note: To get more details about pod, kubectl describe pod <pod-name>
Above commands output confirm that pod has been created successfully. Now try to access nginx page using curl command
[[email protected] ~]$ curl http://172.16.140.28 Hello, NFS Storage NGINX [[email protected] ~]$
Perfect, above curl command’s output confirms that persistent volume is mounted correctly inside pod as we are getting the contents of index.html file which is present on NFS share.
This concludes the article, I believe you guys got some basic idea on how to configure and use NFS based persistent volume inside Kubernetes pods.
Also Read : How to Setup Kubernetes(k8s) Cluster in HA with Kubeadm
Also Read : How to Install and Use Helm in Kubernetes