Understanding Persistent Volumes (PV) and Persistent Volume Claims (PVC) in Kubernetes

In Kubernetes, Persistent Volumes (PV) and Persistent Volume Claims (PVC) are key components of the storage architecture that enable the management of persistent storage for applications. They provide a way to decouple storage from the lifecycle of pods, allowing data to persist even when pods are deleted or recreated.

What is a Persistent Volume (PV)?

A Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. PVs are resources in the cluster just like nodes are resources. They are independent of the pods that use them and can be reused across different pods.

Key Features of Persistent Volumes

  • Decoupled Storage: PVs provide a way to manage storage independently from the lifecycle of pods.
  • Reusability: PVs can be reused by different pods, allowing for data sharing and persistence.
  • Storage Types: PVs can be backed by various storage types, including NFS, iSCSI, cloud storage (like AWS EBS, GCP Persistent Disk), and more.

What is a Persistent Volume Claim (PVC)?

A Persistent Volume Claim (PVC) is a request for storage by a user. It specifies the size and access modes required for the storage. When a PVC is created, Kubernetes looks for a matching PV that satisfies the claim's requirements. If a suitable PV is found, it is bound to the PVC.

Key Features of Persistent Volume Claims

  • Dynamic Binding: PVCs allow users to request storage without needing to know the details of the underlying storage infrastructure.
  • Access Modes: PVCs can specify access modes such as ReadWriteOnce, ReadOnlyMany, or ReadWriteMany, determining how the volume can be accessed.
  • Size Specification: PVCs allow users to specify the amount of storage they need.

Sample Persistent Volume (PV) Configuration

Below is a sample YAML configuration for creating a Persistent Volume in Kubernetes:

        
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/data

Explanation of the PV Configuration

  • apiVersion: Specifies the version of the Kubernetes API for the Persistent Volume.
  • kind: Indicates that this resource is a PersistentVolume.
  • metadata: Contains data that helps uniquely identify the PV, including its name.
  • spec: Defines the desired state of the PV.
  • capacity: Specifies the amount of storage available in the PV.
  • accessModes: Defines how the volume can be accessed (e.g., ReadWriteOnce).
  • hostPath: Specifies the path on the host where the volume is located (for testing purposes; in production, use cloud storage or network storage).

Sample Persistent Volume Claim (PVC) Configuration

Below is a sample YAML configuration for creating a Persistent Volume Claim in Kubernetes:

        
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi

Explanation of the PVC Configuration

  • apiVersion: Specifies the version of the Kubernetes API for the Persistent Volume Claim.
  • kind: Indicates that this resource is a PersistentVolumeClaim.
  • metadata: Contains data that helps uniquely identify the PVC, including its name.
  • spec: Defines the desired state of the PVC.
  • accessModes: Specifies how the volume can be accessed (e.g., ReadWriteOnce).
  • resources: Contains the resource requests, including the amount of storage requested.

Creating and Managing PVs and PVCs

To create the Persistent Volume, you can use the following command:

        
kubectl apply -f persistent-volume.yaml

To create the Persistent Volume Claim, use:

        
kubectl apply -f persistent-volume-claim.yaml

You can check the status of your PVs and PVCs by running:

        
kubectl get persistentvolumes
kubectl get persistentvolumeclaims

Conclusion

Persistent Volumes and Persistent Volume Claims are essential components in Kubernetes for managing persistent storage. They allow for the separation of storage management from the lifecycle of pods, ensuring that data remains available even when pods are recreated. By using PVs and PVCs, developers can easily request and manage storage resources in a Kubernetes environment.