Kubernetes PersistentVolumeClaims (PVC)¶
A PersistentVolumeClaim (PVC) represents a user's request for storage in Kubernetes.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md] It functions similarly to how a [[pods|Pod]] requests compute resources like CPU and memory, but specifically focuses on requesting storage capacity and specific access modes.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
PVCs are a key component in maintaining data persistence for [[stateful-applications|Stateful]] applications. Because the lifecycle of the underlying PersistentVolume (PV) is independent of the Pod, data remains intact even if Pods are destroyed or scaled.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
Binding and Lifecycle¶
Once a PVC is created, it enters a binding process where the cluster continuously searches for a matching PersistentVolume (PV) that satisfies the claim's requirements (size and access mode).^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md] This binding results in a one-to-one mapping between the PVC and the PV.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
If no matching PV is available, the PVC will remain in a Pending state indefinitely until a suitable resource is added to the cluster.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md] Once a match is found and bound, the PV's status updates to Bound.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
Access Modes¶
When defining a PVC, users must specify an access mode that determines how the storage volume can be mounted by nodes^[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]
- 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 (RWOP): The volume can be mounted as read-write by a single Pod, ensuring exclusivity across the entire cluster.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
In multi-node production environments (such as GKE or AWS), choosing the correct mode is critical for allowing different Pods on different nodes to access shared resources.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
StorageClass and Dynamic Provisioning¶
PVCs can utilize StorageClass resources to enable dynamic provisioning of storage.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
If a PVC specifies a StorageClass (e.g., hostpath or a cloud-specific provisioner), Kubernetes can automatically create a matching PersistentVolume (PV) and bind it to the claim^[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] Setting the storage class to an empty string ("") explicitly disables dynamic provisioning, forcing the PVC to bind only to manually created PVs^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md].
Usage in Pods¶
To utilize the storage claimed by a PVC, a [[pods|Pod]] definition includes a volume section that references the claim name.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]
volumes:
- name: html
persistentVolumeClaim:
claimName: pvc-demo
readOnly: false
This configuration allows multiple Pods (e.g., an alpine Pod writing data and an nginx Pod serving it) to mount the same underlying storage, proving the data persists independently of the Pod lifecycles.^[400-devops__06-Kubernetes__k8s-ithelp__Day20__README.md]