Skip to content

Kubernetes PersistentVolumeClaim (PVC)

A PersistentVolumeClaim (PVC) represents a user's request for storage resources in Kubernetes^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. It functions similarly to how a Pod requests specific levels of CPU and memory, but instead requests storage capacity and specific access modes^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

Once created, a PVC actively searches for a [[PersistentVolume (PV)]] that meets its specified requirements^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. When a match is found, the two resources are bound together^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. If no suitable PV is available, the PVC will remain in a Pending state indefinitely until a matching volume is added to the cluster^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

Configuration

A PVC is defined using a kind: PersistentVolumeClaim manifest file^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. The key specifications include:

  • Access Modes: Defines how the storage can be mounted (e.g., ReadWriteOnce, ReadOnlyMany).
  • Storage Class: Determines the type of storage backend (e.g., hostpath, cloud NFS).
  • Resources: Specifies the amount of storage requested (e.g., 1Gi).

Example configuration:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-demo
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: hostpath
  resources:
    requests:
      storage: 1Gi

^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]

Access Modes

Access modes define the capabilities of the volume^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]:

  • ReadWriteOnce (RWO): The volume can be mounted as read-write by a single node^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. This mode also allows multiple Pods on the same node to access the volume^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].
  • ReadOnlyMany (ROX): The volume can be mounted as read-only by many nodes^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].
  • ReadWriteMany (RWX): The volume can be mounted as read-write by many nodes^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].
  • ReadWriteOncePod: The volume can be mounted as read-write by a single Pod, ensuring exclusive access across the entire cluster^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

In production environments involving multiple nodes (e.g., GKE, AWS EKS), Pods sharing the same PVC might be scheduled on different nodes^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. To enable shared data access across different nodes in these scenarios, the PVC must use access modes like ReadOnlyMany or ReadWriteMany, typically supported by NFS or cloud-based provisioners^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

Storage Classes and Dynamic Provisioning

PVCs interact with StorageClasses to determine how storage is provisioned^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. If a PVC specifies a storageClassName, Kubernetes can dynamically create a new PersistentVolume to satisfy the request^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

If no class is specified, the cluster's default StorageClass is used^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. Conversely, setting storageClassName to an empty string ("") disables Dynamic Provisioning, requiring the PVC to bind only to manually created, pre-existing PVs^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

Usage in Pods

Pods consume storage by referencing the PVC in their volumes section^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md].

volumes:
  - name: html
    persistentVolumeClaim:
      claimName: pvc-demo
      readOnly: false
^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]

This abstraction allows the lifecycle of the data (managed by the PV/PVC) to remain independent of the lifecycle of the Pod^[400-devops-06-kubernetes-k8s-ithelp-day20-readme.md]. Consequently, data is preserved even if the consuming Pod is deleted or recreated, enabling data sharing between different Pods.

Sources

  • 400-devops-06-kubernetes-k8s-ithelp-day20-readme.md