When a developer creates a PVC specifying a storage size and access mode, the cluster either binds an existing PV to it (static provisioning) or creates one for it using dynamic provisioning based on StorageClasses created by administrators. In both cases, the storage is accessed by pods using claims made from them.
The Kubernetes PVC model enables long-term, reliable storage solutions that outlast individual containers. Understanding PVCs and PVC specifications, best practices for leveraging advanced configuration options (like storage capacity limits), and how to implement dynamic provisioning using Storage Classes can help you build more efficient, reliable clusters. The PVC object represents a request to utilize storage that can specify what it needs, such as volume size or access mode. When a Pod is created, it uses the PVC to mount the requested volume into its filesystem, allowing the Pod to read and write files that will outlive it.
Once the PVC is bound to a PV, it is considered available and can be used by any Pod with its PVC bound to it. The PVC can also have a reclaim policy, which defines what happens to the volume after it is unbound from a PVC. Reclaim policies can be Retain, Recycle, or Delete. The reclaim policy for a PV can be manually or automatically managed. If the reclaim policy is Retain, the PV can be reclaimed manually by submitting a reclaim request to the cluster. If the reclaim policy is deleted, the system deletes the PV. The reclaim policy can be changed from the API.
A PVC is like a voucher that your deployment uses to claim a share of storage resources from the cluster. The PVC specifies the size and access mode of a volume. A PVC’s specifications must match the underlying PV that will back it. When a user creates a PVC, the Kubernetes cluster monitors this PVC and matches it to a PV via its specifications and reclaim policy. This process is known as dynamic provisioning. If a PVC is compared to a PV, the PV becomes bound to the PVC. If the reclaim policy of the PV is set to Retain, it will not be automatically deleted when the PVC is deleted. Each PV has a storage class defined by its storageClassName attribute. The PVC’s specification can match the StorageClass; if it does, the Kubernetes cluster binds the PV to the PVC. The PVC also has a matchExpressions field, which is an expression that combines the PVC’s key and list of values with a valid operator. The matchExpressions field ANDs the requirements — they must all be satisfied to create a binding. A PV can only be bound to one PVC at a time. However, the PVC can have multiple access modes, such as ReadWriteOncePod and ReadOnlyManyPod, and each access mode specifies how many nodes can mount the volume.
Kubernetes enables users to dynamically request storage by specifying their needs as PersistentVolumeClaim objects. These claims are matched to available PVs that meet all the specifications set by the claim. A key part of the specification is the storage class (either ReadWriteOnce or ReadWriteMany) and access mode (read-only or read-write). Users can specify these requirements in the persistentVolumeClaim object using matchLabels and matchExpressions. A matchExpression is a list of requirements expressed as a key, a list of values, and an operator that relates the keys and values together. Matches are ANDed, meaning all match expressions must be satisfied to find a matching PV. The PVC object can also specify that a StorageClass should dynamically bind the PV to a PersistentVolumeClaim. In this case, the cluster will dynamically create a PV with that storage class when the PVC is not successfully matched to any static PVs that are already present in the group. One of the best practices for efficiently using PVs and PVCs is always to include a StorageClass with your PVs. This helps the cluster match PVCs to PVs that can more effectively meet their capacity and access mode requirements. You can also set storage capacity limits for PVs and use them with PVCs to simplify the management process.
Kubernetes has several different ways to manage storage in your cluster. These include the definition of Storage Classes, dynamic provisioning of PVs, access modes, and reclaim policies. Understanding these concepts can help you create more efficient and secure storage configurations for your deployments. Dynamic provisioning eliminates the need for administrators to pre-provision persistent storage devices by allowing developers to request storage through PVCs dynamically. PVCs refer to StorageClass objects, which indicate storage device properties that a pod requires. When a user creates a PVC that refers to a StorageClass, the master checks for available PVs with the same property; if it finds one, the master binds the PVC to that PV. For example, if a PV has its claimPolicy set to ReadWriteOncePod, it only allows a single pod in the entire cluster to write to it. It is possible to scale a stateful application with this PVC if the deployment is run on nodes of the same size, as they are both writing to the same persistent volume. Another feature is the ability to set a dataSourceRef for a PVC. This field is similar to the dataSource area in a PVC, but it can contain a reference to a PersistentVolumeClaim or a VolumeSnapshot, which provides more flexibility for your deployments.