Skip to content

Kubernetes PV and PVC

PV (PersistentVolume) and PVC (PersistentVolumeClaim) are Kubernetes API objects used to manage storage in a cluster^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. They decouple storage provisioning from storage consumption, allowing developers to request storage resources without needing to know the underlying infrastructure details^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

Core Concepts

PersistentVolume (PV)

A PersistentVolume represents a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using StorageClasses^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. It is a resource in the cluster, describing a specific volume implementation, such as an NFS directory, a host path, or a cloud storage volume^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

PersistentVolumeClaim (PVC)

A PersistentVolumeClaim is a request for storage by a user^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. It specifies the desired attributes of the storage, such as: * Storage Size: The amount of storage requested (e.g., 30Gi)^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. * Access Modes: ReadWriteOnce, ReadOnlyMany, or ReadWriteMany^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. * StorageClassName: The class of storage to use^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

PVCs act as logical volumes for Pods, allowing developers to consume storage abstractly without managing the backend details^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

Binding Mechanism

A PVC must be bound to a PV before it can be used by a Pod^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. The binding process is controlled by the control plane based on specific criteria:

  1. Spec Matching: The PV's capacity and access modes must satisfy the PVC's requirements^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].
  2. StorageClass Match: The storageClassName fields of the PV and PVC must be identical^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

Once bound, the claim is exclusive to that specific PV.

Dynamic Provisioning

In large-scale clusters, manually pre-creating thousands of PVs is impractical and error-prone^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. To solve this, Kubernetes uses Dynamic Provisioning, which automatically creates PVs based on demand^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

This mechanism relies on StorageClass, an API object that acts as a template for creating PVs^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

StorageClass

A StorageClass defines two key components^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]: 1. PV Properties: Such as storage type (e.g., Block, Filesystem) and volume size^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. 2. Provisioner: The storage plugin (e.g., Ceph, AWS EBS) responsible for creating the volume^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

When a user creates a PVC specifying a storageClassName, Kubernetes locates the corresponding StorageClass and invokes its provisioner to create a new PV that matches the claim^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].

Decoupling Benefits

This storage architecture provides significant flexibility:

  • Separation of Concerns: Administrators manage storage infrastructure (PVs/StorageClass), while developers consume storage via PVCs^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md].
  • Backend Agnostic: The PVC YAML definition does not change regardless of the underlying storage implementation^[400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md]. The backend can be switched or migrated without affecting the developer's configuration.

Sources

  • 400-devops__06-Kubernetes__k8s-paas__原理及源码解析__Kubernetes_yaml文件.md