Cloud Native 14 min read

Mastering Kubernetes Persistent Storage: PV, PVC, and StorageClass with NFS

This guide walks through installing NFS, configuring PersistentVolumes, PersistentVolumeClaims, and StorageClasses in Kubernetes, explains their key fields and lifecycle, demonstrates dynamic provisioning with an NFS provisioner, and shows how to use the resulting PVC in a pod.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Mastering Kubernetes Persistent Storage: PV, PVC, and StorageClass with NFS

Install Storage System

Common storage options for Kubernetes include NFS, Ceph, GlusterFS, FastDFS. This guide uses NFS and shows how to install it.

Install Service

<code>$ yum install nfs-utils rpcbind -y</code>

Create Shared Directory

<code>$ mkdir /data/k8s -p</code>

Configure NFS Export

<code>$ vim /etc/exports
/data/k8s *(rw,sync,no_root_squash)</code>

Start Services

<code>$ systemctl start rpcbind
$ systemctl start nfs
$ systemctl enable rpcbind
$ systemctl enable nfs</code>

Test

<code>$ showmount -e 192.168.205.128
Export list for 192.168.205.128:
/data/k8s *</code>
All nodes must install the NFS client.

PersistentVolume (PV)

PV is an abstraction of underlying storage, created and managed by administrators. It can be backed by Ceph, NFS, etc.

Example PV definition:

<code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv01
  labels:
    storage: pv
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /data/k8s
    server: 192.168.205.128</code>

Key fields:

accessModes : ReadWriteOnce, ReadOnlyMany, ReadWriteMany.

capacity : storage size.

persistentVolumeReclaimPolicy : Retain, Delete, Recycle.

PV status can be Available, Bound, Released, or Failed.

PersistentVolumeClaim (PVC)

PVC expresses a user's storage request and consumes a PV. Use

kubectl explain pvc

for details.

Example PVC:

<code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-test
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi</code>

Key spec fields: accessModes, resources.requests.storage, dataSource, selector, storageClassName, volumeMode, volumeName.

After creation, check status:

<code>$ kubectl get pvc
NAME      STATUS  VOLUME    CAPACITY  ACCESS MODES  STORAGECLASS  AGE
pvc-test  Bound   my-pv01   1Gi       RWO           <em>(none)</em>      ...</code>

If a second PVC is created without an available PV, its status will be Pending until a matching PV is created.

StorageClass

StorageClass automates PV creation. It defines storage properties and the provisioner to use.

Example StorageClass for NFS:

<code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs
provisioner: rookieops/nfs</code>

Fields include provisioner, parameters, and reclaimPolicy (default Delete, also Retain). The StorageClass name is used by PVCs to request dynamic provisioning.

Create it with

kubectl apply -f sc.yaml

and verify:

<code>$ kubectl get sc
NAME  PROVISIONER      RECLAIMPOLICY  VOLUMEBINDINGMODE  ALLOWVOLUMEEXPANSION  AGE
nfs   rookieops/nfs    Delete         Immediate          false                ...</code>

Dynamic PVC using the StorageClass:

<code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-from-sc
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: nfs
  resources:
    requests:
      storage: 1Gi</code>

After applying, a PV is automatically created and bound.

One StorageClass can be marked as default by adding the annotation

storageclass.kubernetes.io/is-default-class: "true"

. Only one default is allowed.

To use the PVC in a Pod:

<code>apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: nfs-pvc
      mountPath: /mnt
  restartPolicy: Never
  volumes:
  - name: nfs-pvc
    persistentVolumeClaim:
      claimName: pvc-from-sc</code>

Exec into the pod and write to

/mnt

to verify persistence.

Summary

While Kubernetes encourages stateless applications, stateful workloads require persistent storage. Understanding PV, PVC, and StorageClass, and how to configure NFS provisioners, is essential for reliable data persistence.

CloudNativeKubernetesNFSPersistentVolumeStorageClassPVC
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

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.