Disclaimer

PersistentVolume and PersistentVolumeClaim is API for users and administrators that abstracts details of how storage is provided from how it is consumed. PV are resources in the cluster. PVC are requests for those resources and also act as claim checks to the resource.

PersistentVolume life cycle is :

  1. Provisioning (PV Creation).
  2. Binding (Bind the storage request to the PV. VPC).
  3. Using (Start using volume inside a Pod).
  4. Reclaiming (Recycle, Retain, Deleted).

Two types of PV provisioning is static (PV needs to be created before by administrator) and dynamic (PV is created at the same time of PVC, need to configure StorageClass first).

Source image : https://btech.id/


Persistent Volumes (VP)

  1. Provisioned by an administrator or dynamically provisioned using StorageClasses.
  2. Represent a storage in Kubernetes.
  3. Provision persistent networked storage to pods that can be mounted inside a container to store data.
  4. Access by nodes mode : ReadWriteOnly (RWO), ReadOnlyMany (ROX), ReadWriteMany (RWX).
  5. Before the PV is mounted into a pod, it must first be mounted on the node where the pod is running.
  6. Before the PV can be used by pod, PVC needs to be made to binding or claim storage so it is not used by others.

Persistent Volumes Claim (VPC)

  1. Represent a request for storage by a pod to Kubernetes.
  2. PVC consume PV resources.


Logical Topology


Workflow

  1. Make sure your kubernetes cluster is ready. Reference.
  2. Install and configure NFS Server outside kubernetes cluster. Reference.
  3. Install and configure NFS Client on each kubernetes nodes : sudo apt -y install nfs-common
  4. Mount NFS folder on each kubernetes nodes : sudo mount -t nfs 10.10.10.5:/nfs-path-dir/ local-dir
  5. Create PersistentVolume and PersistentVolumeClaim from your kubernetes master node.
#kubectl apply -f nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 15Gi
  volumeMode: Filesystem
  ##storageClassName: slow
  ##persistentVolumeReclaimPolicy: Retain
  accessModes:
    - ReadWriteMany
  nfs:
    server: kubernetes.nfs
    path: "/volumes"
#kubectl apply -f nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  ##storageClassName: slow
  resources:
    requests:
      storage: 15Gi
kubernetes@master:~$ kubectl get pv
NAME     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM             STORAGECLASS   REASON   AGE
nfs-pv   15Gi       RWX            Retain           Bound    default/nfs-pvc                           44m

kubernetes@master:~$ kubectl get pvc
NAME      STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nfs-pvc   Bound    nfs-pv   15Gi       RWX                           42m
kubernetes@kubernetes:~$
  1. Create a Pod and use PersistentVolumeClaim as volume for that pod.
apiVersion: v1
kind: Pod
metadata:
  name: nginx-with-pv
spec:
  volumes:
    - name: nginx-with-pv
      persistentVolumeClaim:
        claimName: nfs-pvc
  containers:
    - name: nginx-with-pv
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: nginx-with-pv
  1. Pod Verification
kubernetes.master$ kubectl get pod
kubernetes.master$ echo "hello world!" > /volumes/index.html

kubernetes.master$ kubectl exec -it [nginx-pod-name] /bin/bash
nginx-pod# curl http://localhost

Reference : https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/