Skip to main content

Create Persistent Volume

Learn how to use PersistentVolume with NFS as a backend

Deploy and configure NFS server

note

Pay attention to which commands to execute on the host and which on each VM

Install an NFS server on cp1:

sudo apt-get install -y nfs-kernel-server

Create an NFS export directory and set the correct permissions on cp1:

sudo mkdir /srv/share
sudo chmod 1777 /srv/share
echo "Hello World Simple Website" > /srv/share/index.html

Export the NFS directory to the Kubernetes cluster nodes on cp1:

sudo vim /etc/exports
/srv/share/ *(rw,sync,no_root_squash,subtree_check)

On cp1, trigger volume export to worker nodes:

sudo exportfs -ra

Install the nfs-common package on all* Kubernetes nodes:

for SRV in cp1 worker{1,2,3}; do
sudo ssh $SRV apt-get install nfs-common -y;
done

Check if nodes can connect to shares:

for SRV in cp1 worker{1,2,3}; do
sudo ssh $SRV showmount -e k8scp;
done
See expected output
Export list for k8scp:
/srv/share *
Export list for k8scp:
/srv/share *
Export list for k8scp:
/srv/share *
Export list for k8scp:
/srv/share *

Prepare a YAML manifest for PersistentVolume:

pv-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /srv/share
server: k8scp
readOnly: false

Apply the PersistentVolume YAML manifest:

kubectl apply -f pv-nfs.yaml

Check the PersistentVolumes list in the cluster:

kubectl get pv
See output
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-nfs 10Gi RWX Retain Available 6s

Create a new Namespace for the service:

kubectl create namespace webapp

To use PersistentVolume, it is required to create a PersistentVolumeClaim YAML manifest:

pvc-nfs.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-nfs
namespace: webapp
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi

Apply the PersistentVolumeClaim manifest:

kubectl apply -f pvc-nfs.yaml

Get the list of PersistentVolumeClaims:

kubectl get pvc -n webapp
See output
NAME      STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs Bound pv-nfs 10Gi RWX 13s

Let's create a new application that will use the PVC:

deploy-webapp-nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: webapp-nfs
name: webapp
namespace: webapp
spec:
replicas: 3
selector:
matchLabels:
app: webapp-nfs
template:
metadata:
labels:
app: webapp-nfs
spec:
containers:
- image: nginx:1.23.2
name: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: vol-nfs
volumes:
- name: vol-nfs
persistentVolumeClaim:
claimName: pvc-nfs

Deploy a web application with an NFS volume mounted:

kubectl apply -f deploy-webapp-nfs.yaml

Check Deployment status:

kubectl get deploy,rs,pods -n webapp

Verify PersistentVolume and PersisentVolumeClaim:

kubectl get pvc,pv -n webapp

Get Events messages from Namespace:

kubectl get events --sort-by='.lastTimestamp' -n webapp

Get Pod description:

kubectl describe pod -n webapp webapp-<Tab>

Open a port tunnel to any Pod instance from the webapp Deployment:

kubectl port-forward -n webapp pod/webapp-<Tab> 8080:80 &

Check the webapp response on localhost:

curl http://127.0.0.1:8080

To cleanup, remove the webapp Namespace:

kubectl delete ns webapp