Configuring NFS with PV and PVC for Persistent Storage in Kubernetes
This guide walks through installing NFS on Kubernetes nodes, creating a shared directory, configuring exports, setting up PersistentVolume and PersistentVolumeClaim objects, and deploying a MySQL instance that uses the NFS-backed storage.
This article demonstrates how to use NFS together with Kubernetes PersistentVolume (PV) and PersistentVolumeClaim (PVC) to provide durable storage for applications.
First, install the NFS client packages on all cluster nodes:
yum install -y nfs-common nfs-utilsOn the master node, create a directory that will be shared:
mkdir nfsdataSet permissive permissions so any client can write:
chmod 666 nfsdataEdit the NFS export file to expose the directory:
vi /etc/exportsAdd the following line (replace IP with your client addresses if needed):
/root/k8s/nfsdata *(rw,no_root_squash,no_all_squash,sync)Start the required services in the correct order:
systemctl start rpcbind systemctl start nfsVerify the export from any node:
showmount -e ipCreate a mount point on each of the three servers and mount the NFS share:
mkdir /nfs/dataPull the MySQL image on the worker nodes so the image is cached locally:
docker pull mysql:8.0.22Define a PersistentVolume that points to the NFS share (nfs-pv.yaml):
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /nfs/data
server: 192.168.240.57Apply the PV definition:
kubectl apply -f nfs-pv.yamlCreate a matching PersistentVolumeClaim (nfs-pvc.yaml):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50Gi
storageClassName: nfsApply the PVC definition:
kubectl apply -f nfs-pvc.yamlDeploy a MySQL pod that uses the PVC (mysql.yaml) and apply it:
kubectl apply -f mysql.yamlCheck that the pod is running:
kubectl get podEnter the MySQL container to create test data:
kubectl exec -it mysql-6b8564f498-wd4m4 -- mysql -uroot -p123.comOptionally, stop the kubelet on the node where the pod runs to verify that the pod is recreated on another node and that the data persists.
This procedure provides a reliable NFS‑backed persistent storage solution for stateful workloads in a Kubernetes cluster.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.