Cloud Computing 4 min read

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.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Configuring NFS with PV and PVC for Persistent Storage in Kubernetes

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-utils

On the master node, create a directory that will be shared:

mkdir nfsdata

Set permissive permissions so any client can write:

chmod 666 nfsdata

Edit the NFS export file to expose the directory:

vi /etc/exports

Add 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 nfs

Verify the export from any node:

showmount -e ip

Create a mount point on each of the three servers and mount the NFS share:

mkdir /nfs/data

Pull the MySQL image on the worker nodes so the image is cached locally:

docker pull mysql:8.0.22

Define 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.57

Apply the PV definition:

kubectl apply -f nfs-pv.yaml

Create a matching PersistentVolumeClaim (nfs-pvc.yaml):

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs

Apply the PVC definition:

kubectl apply -f nfs-pvc.yaml

Deploy a MySQL pod that uses the PVC (mysql.yaml) and apply it:

kubectl apply -f mysql.yaml

Check that the pod is running:

kubectl get pod

Enter the MySQL container to create test data:

kubectl exec -it mysql-6b8564f498-wd4m4 -- mysql -uroot -p123.com

Optionally, 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.

KubernetesMySQLstorageNFSPersistentVolumePVC
Practical DevOps Architecture
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.