Home / Articles / Devops / kubernetes / 1.23.6 / persistent-volumes

Kubernetes Persistent Volumes

Introduction

  • As per Official definition:
  • Managing storage is a distinct problem from managing compute instances. The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed. To do this, we introduce two new API resources: PersistentVolume and PersistentVolumeClaim.

    A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.

    A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany, see AccessModes).

    While PersistentVolumeClaims allow a user to consume abstract storage resources, it is common that users need PersistentVolumes with varying properties, such as performance, for different problems. Cluster administrators need to be able to offer a variety of PersistentVolumes that differ in more ways than size and access modes, without exposing users to the details of how those volumes are implemented. For these needs, there is the StorageClass resource.

    Create NFS Persistent Volume

    • Setup for this example
    • Prerequisite: Make sure NFS server is reachable from worker nodes and try to mount nfs share on each worker once for testing.

      PV Name : task-nfs-pv
      PVC Name : task-nfs-pvc
      POD Name : task-nginx-pv-pod
      NFS Share : 192.168.50.10:/repo/exports
      NFS Share Content: Create /repo/exports/index.html file in NAS share and put below string to be validated later on. SUCCES: Hello from Kubernetes NFS volume

      Create Persistent Volume (PV)

    • Create YAML file for PVD configuration
    • # vi create-pv-nas.yaml apiVersion: v1 kind: PersistentVolume metadata: name: task-nfs-pv spec: capacity: storage: 10Gi volumeMode: Filesystem accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Recycle storageClassName: nfs mountOptions: - hard - nfsvers=4.1 nfs: path: /repo/exports server: 192.168.50.10
    • Apply the changes
    • # kubectl apply -f create-pv-nas.yaml persistentvolume/task-nfs-pv created
    • Check PV Status
    • # kubectl get pv task-nfs-pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE task-nfs-pv 10Gi RWX Recycle Available nfs 47s

      Create Persistent Volume Claim (PVC)

    • Create YAML file for PVC configuration
    • # vi create-pvc-nas.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: task-nfs-pvc spec: storageClassName: nfs accessModes: - ReadWriteMany resources: requests: storage: 10Gi
    • Apply the changes
    • # kubectl apply -f create-pvc-nas.yaml persistentvolumeclaim/task-nfs-pvc created
    • Check PVC Status
    • # kubectl get pvc task-nfs-pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE task-nfs-pvc Bound task-nfs-pv 10Gi RWX nfs 11s

      Create POD utilizing the Persistent Volume

    • Create YAML file for POD configuration
    • # vi create-pod-nas.yaml apiVersion: v1 kind: Pod metadata: name: task-nginx-pv-pod spec: volumes: - name: nginx-pv-storage persistentVolumeClaim: claimName: task-nfs-pvc containers: - name: nginx image: nginx ports: - containerPort: 80 name: "nginx-server" volumeMounts: - mountPath: "/usr/share/nginx/html" name: nginx-pv-storage
    • Apply the changes
    • # kubectl apply -f create-pod-nas.yaml pod/task-nginx-pv-pod created
    • Check POD Status
    • # kubectl get pod task-nginx-pv-pod NAME READY STATUS RESTARTS AGE task-nginx-pv-pod 1/1 Running 0 2m2s

      Access the Persistent Storage from POD

    • Run following command to access content of persisten storage
    • # kubectl exec task-nginx-pv-pod -- df -Ph /usr/share/nginx/html Filesystem Size Used Avail Use% Mounted on 192.168.50.10:/repo/exports 25G 13G 13G 51% /usr/share/nginx/html
      # kubectl exec task-nginx-pv-pod -- cat /usr/share/nginx/html/index.html SUCCESS: Hello from Kubernetes NFS volume