Kubernetes Persistent Volumes and Persistent Volume Claim Using NFS Server: Static Provisioning

·

4 min read

Kubernetes Persistent Volumes and Persistent Volume Claim Using NFS Server: Static Provisioning

Introduction

Kubernetes Persistent Volumes (PV) and Persistent Volume Claims (PVC) are used to manage and provide persistent storage for applications running in a Kubernetes cluster.

Persistent Volumes (PV)

Persistent Volumes represent a piece of storage in the cluster that has a lifecycle independent of any individual Pod.

They are created by a cluster administrator and can be provisioned from various storage types, such as network-attached storage (NAS), cloud-based storage, or local disk storage.

Persistent Volumes provide an abstraction layer that allows applications to request storage without needing to know the specifics of the underlying storage infrastructure.

PV is a cluster-level component.

Reclaim Policy in PV

  1. Retain: With this policy, the PV will not be deleted automatically after it is released. It requires manual intervention to reclaim or delete the PV. The data stored on the PV will be preserved.

  2. Delete: With this policy, the PV will be deleted automatically once it is released or the corresponding PersistentVolumeClaim (PVC) is deleted. The data stored on the PV will be permanently removed.

  3. Recycle: This policy is deprecated in Kubernetes and may not be supported by all storage providers. It is used to trigger a basic cleanup of the PV by deleting the contents of the volume once it is released. It is no longer recommended to use this policy.

Persistent Volume Claims (PVC)

A Persistent Volume Claim is a request for storage by a user or a group of users. When an application needs persistent storage, it creates a Persistent Volume Claim with specific requirements for storage, such as the capacity, access mode, and storage class.

The cluster's storage system then attempts to find a matching Persistent Volume that satisfies the claim.

PVC is a Namespace level component.

AccessModes in PVC

  1. ReadWriteOnce (RWO): This mode allows the volume to be mounted as read-write by a single node. It is typically used for scenarios where the volume should be mounted by only one node at a time.

  2. ReadOnlyMany (ROX): This mode allows the volume to be mounted as read-only by multiple nodes simultaneously. It is suitable for scenarios where the volume needs to be accessed by multiple nodes in a read-only manner.

  3. ReadWriteMany (RWX): This mode allows the volume to be mounted as read-write by multiple nodes simultaneously. It enables multiple nodes to read from and write to the volume concurrently.

Static provisioning

Static provisioning in Kubernetes refers to the manual creation and configuration of Persistent Volumes (PVs) by a cluster administrator.

In this approach, the administrator pre-provisions the storage resources and defines the PVs in the cluster, which can then be claimed by applications using Persistent Volume Claims (PVCs).

Architecture Diagram

Lab

Prerequisites:

  1. NFS Server (Guide to install NFS Server: https://www.tecmint.com/install-nfs-server-on-ubuntu/)

  2. K8s Cluster

Let's start the lab.

arun@Aruns-MacBook-Air ~ % kubectl get nodes
NAME       STATUS   ROLES           AGE   VERSION
master     Ready    control-plane   63d   v1.26.3
worker01   Ready    <none>          62d   v1.26.3
worker02   Ready    <none>          62d   v1.26.3

STEP 1: Created a mount directory in NFS Server

root@nfs-sever:/mnt/nfs_share# mkdir shared_path
root@nfs-sever:/mnt/nfs_share# cd shared_path/
root@nfs-sever:/mnt/nfs_share/shared_path# vi index.html
root@nfs-sever:/mnt/nfs_share/shared_path# ls -lrt
total 16
-rw-r--r-- 1 root root 12310 Jun  2 16:47 index.html

STEP 2: Deploy PV and PVC

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-nfs-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /mnt/nfs_share/shared_path
    server: 172.16.137.140
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-nfs-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: ""
  volumeName: my-nfs-pv

STEP 3: Deploy Ngnix Metadata deployment and service

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: nginx
          volumeMounts:
            - name: my-volume
              mountPath: /usr/share/nginx/html
      volumes:
        - name: my-volume
          persistentVolumeClaim:
            claimName: my-nfs-pvc
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30000

STEP 4: Load nginx webpage

arun@Aruns-MacBook-Air ~ % kubectl exec -it my-deployment-79dff76cfb-fqn2s -- bash
root@my-deployment-79dff76cfb-fqn2s:/# cd /usr/share/nginx/html
root@my-deployment-79dff76cfb-fqn2s:/usr/share/nginx/html# ls -lrt
total 4
-rw-r--r-- 1 root root 55 Jun  2 17:00 index.html
root@my-deployment-79dff76cfb-fqn2s:/usr/share/nginx/html#

STEP 5: In this lab, Even though we deleted the deployment, NFS persist the shared_path directory.

arun@Aruns-MacBook-Air persistencevolume % kubectl delete -f . 
deployment.apps "my-deployment" deleted
persistentvolume "my-nfs-pv" deleted
persistentvolumeclaim "my-nfs-pvc" deleted
service "my-service" deleted
arun@Aruns-MacBook-Air persistencevolume %
root@nfs-sever:/mnt/nfs_share/shared_path# ls
index.html
root@nfs-sever:/mnt/nfs_share/shared_path#

Summary

PersistentVolumes (PV) and PersistentVolumeClaims (PVC) are vital components for managing persistent storage in Kubernetes.

Understanding their purpose, features, and configuration options is essential for effectively utilizing storage resources within Kubernetes clusters.

End.

Please provide your valuable comments.