avatar🌌
TangentTangent Blog

技术博客 | Go & Kubernetes & Linux

技术博客 | Go & Kubernetes & Linux

Detailed Explanation of PV (PersistentVolume) and PVC (PersistentVolumeClaim)

1. Basic Concepts ​

  • PV (PersistentVolume): A storage resource pre-configured by the administrator or dynamically created by a StorageClass, representing actual storage in the cluster.
  • PVC (PersistentVolumeClaim): A user’s request for storage, specifying required capacity, access modes, etc.

2. Binding Conditions ​

The binding between PVC and PV must meet the following conditions:

  1. Capacity (storage)
    • The PV’s capacity must be greater than or equal to the PVC’s requested capacity.
  2. Access Modes
    • Common modes:
      • ReadWriteOnce (read/write by a single node)
      • ReadOnlyMany (read-only by multiple nodes)
      • ReadWriteMany (read/write by multiple nodes)
    • The PV must support the access mode requested by the PVC.
  3. StorageClass
    • If the PVC specifies storageClassName, it will only bind to PVs with the same name.
    • If not specified, it binds to PVs with no storage class or the default storage class.

3. Reclaim Policy ​

The reclaim policy of a PV determines how the PV is handled after the PVC is released:

  • Retain: The PV and its data are not deleted; manual handling is required.
  • Recycle: Deprecated. Used to simply clear data and make the PV available again.
  • Delete: The PV and its backend storage resource are automatically deleted (e.g., cloud disks).

The delete policy is only supported for storage types provided by cloud vendors, such as AWS EBS, GCE Persistent Disk, etc.

4. PV/PVC States ​

PV States ​

  • Available: Not bound to a PVC, available
  • Bound: Bound to a PVC
  • Released: PVC deleted, but resource not yet reclaimed
  • Failed: Automatic reclamation failed

PVC States ​

  • Pending: Not bound to a PV
  • Bound: Bound to a PV
  • Lost: The bound PV is unavailable

5. PVC Protection ​

Kubernetes provides PVC protection to prevent PVCs in use by Pods from being accidentally deleted:

  • When a PVC is mounted by a Pod, deletion is blocked until the PVC is no longer used by any Pod.
  • Implemented via the PVC’s finalizer field.

6. Removing PV Binding State ​

To remove the binding state of a PV, follow these steps:

  1. Edit the PVC’s spec.claimRef field to be empty.
bash
kubectl edit pvc <pvc-name>
  1. Delete and recreate the PV.
bash
kubectl delete pv <pv-name>
kubectl apply -f <pv-definition-file>

7. Example YAML ​

yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-demo
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  hostPath:
    path: /mnt/data
  persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-demo
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  resources:
    requests:
      storage: 500Mi

8. Common Issues and Suggestions ​

  • For production, use dynamic StorageClass
  • Pay attention to PV/PVC lifecycle and data safety
  • Regularly check PV status to avoid resource leaks

Reference ​

Kubernetes StorageClass Explained and Lab Tutorial
Kubernetes Secret Explained
由 Valaxy v1.2.2 驱动|主题-Yunv1.2.2