How to Configure NFS Persistent Volumes in Kubernetes – Step‑by‑Step Guide
This tutorial walks you through setting up an NFS‑backed PersistentVolume and PersistentVolumeClaim in a Kubernetes cluster, creating the necessary YAML files, verifying the resources, and mounting the volume in an Nginx pod to serve static content.
It is recommended to store Pod data on a persistent volume so that the data remains available after the Pod terminates. In Kubernetes, you can use an NFS‑based PersistentVolume. This article explains how to configure the PersistentVolume (PV) and PersistentVolumeClaim (PVC) and then mount the volume inside a Pod.
Assume you have a functional Kubernetes cluster and an NFS server. Example lab setup:
NFS server IP = 192.168.1.40
NFS shared directory = /opt/k8s-pods/data
K8s cluster = one master node and two worker nodes
Note: Ensure the worker nodes can reach the NFS server and install the NFS client on each worker for testing.
Create an index.html file in the NFS share; it will be mounted by the Nginx pod later.
echo "Hello, NFS Storage NGINX" > /opt/k8s-pods/data/index.htmlConfigure NFS‑based PersistentVolume
Create a YAML file on the master node with the following content:
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.40Save and exit the file, then create the PV:
kubectl create -f nfs-pv.yaml
persistentvolume/nfs-pv createdVerify the PV status:
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nfs-pv 10Gi RWX Recycle Available nfs 20sConfigure PersistentVolumeClaim
Create a PVC YAML file:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
storageClassName: nfs
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10GiCreate the PVC:
kubectl create -f nfs-pvc.yaml
persistentvolumeclaim/nfs-pvc createdCheck that the PVC is bound to the PV:
kubectl get pvc nfs-pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-pvc Bound nfs-pv 10Gi RWX nfs 3m54sUse the NFS PersistentVolume in a Pod
Create a Pod definition that mounts the PVC at /usr/share/nginx/html:
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-storageCreate the Pod:
kubectl create -f nfs-pv-pod.yaml
pod/nginx-pv-pod createdVerify the Pod is running:
kubectl get pod nginx-pv-pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-pv-pod 1/1 Running 0 66s 172.16.140.28 k8s-worker-2Access the Nginx page to confirm the volume is correctly mounted:
curl http://172.16.140.28
Hello, NFS Storage NGINXThe output shows that the index.html file from the NFS share is served, confirming the successful mount of the NFS‑backed PersistentVolume.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
