Cloud Native 7 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Configure NFS Persistent Volumes in Kubernetes – Step‑by‑Step Guide

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.html

Configure 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.40

Save and exit the file, then create the PV:

kubectl create -f nfs-pv.yaml
persistentvolume/nfs-pv created

Verify the PV status:

kubectl get pv
NAME    CAPACITY  ACCESS MODES  RECLAIM POLICY  STATUS    CLAIM  STORAGECLASS  REASON  AGE
nfs-pv  10Gi      RWX           Recycle          Available        nfs          20s

Configure PersistentVolumeClaim

Create a PVC YAML file:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  storageClassName: nfs
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

Create the PVC:

kubectl create -f nfs-pvc.yaml
persistentvolumeclaim/nfs-pvc created

Check 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           3m54s

Use 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-storage

Create the Pod:

kubectl create -f nfs-pv-pod.yaml
pod/nginx-pv-pod created

Verify 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-2

Access the Nginx page to confirm the volume is correctly mounted:

curl http://172.16.140.28
Hello, NFS Storage NGINX

The output shows that the index.html file from the NFS share is served, confirming the successful mount of the NFS‑backed PersistentVolume.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CloudNativeKubernetesstorageNFSPersistentVolumePVC
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.