乐闻世界logo
搜索文章和话题

What are the differences between Kubernetes PersistentVolume and PersistentVolumeClaim? How do you use them to manage storage?

2月21日 15:53

Kubernetes PersistentVolume (PV) and PersistentVolumeClaim (PVC) are two important resources for managing storage. They implement declarative management and dynamic allocation of storage resources.

PersistentVolume (PV)

PersistentVolume is a piece of storage in the cluster, pre-configured by the administrator or dynamically created through a storage class. PV is a cluster-level resource, independent of the Pod lifecycle.

PV Lifecycle

  1. Provisioning:

    • Static provisioning: Administrator manually creates PV
    • Dynamic provisioning: Automatically created through StorageClass
  2. Binding:

    • When a PVC requests storage, the controller binds the PVC to a suitable PV
    • Binding is one-to-one; once bound, the PV is exclusively for that PVC
  3. Using:

    • Pods use storage through PVC
    • Storage can be mounted to a specified path in the Pod
  4. Releasing:

    • After PVC is deleted, PV enters Released state
    • Data in the PV is still retained
  5. Reclaiming:

    • Retain: Manual reclamation
    • Recycle: Deprecated, replaced by dynamic provisioning
    • Delete: Automatically delete PV and underlying storage

PV Access Modes

  1. ReadWriteOnce (RWO):

    • Volume can be mounted as read-write by a single node
    • Suitable for block storage
  2. ReadOnlyMany (ROX):

    • Volume can be mounted as read-only by multiple nodes
    • Suitable for shared read-only data
  3. ReadWriteMany (RWX):

    • Volume can be mounted as read-write by multiple nodes
    • Suitable for file systems (such as NFS)
  4. ReadWriteOncePod (RWOP):

    • Volume can be mounted as read-write by a single Pod
    • Ensures only one Pod accesses at a time

PV States

  1. Available: Available, not bound to any PVC

  2. Bound: Bound to a PVC

  3. Released: PVC has been deleted, but the resource has not been reclaimed by the cluster

  4. Failed: Automatic reclamation failed

PersistentVolumeClaim (PVC)

PVC is a user's request for storage, similar to a Pod's request for compute resources. PVC is a namespace-level resource.

PVC Configuration

yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi storageClassName: standard selector: matchLabels: environment: production

Using PVC

  1. Use PVC in Pod:
yaml
apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: nginx volumeMounts: - name: my-volume mountPath: /data volumes: - name: my-volume persistentVolumeClaim: claimName: my-pvc
  1. Use PVC in Deployment:
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: volumes: - name: my-volume persistentVolumeClaim: claimName: my-pvc containers: - name: my-container image: nginx volumeMounts: - name: my-volume mountPath: /data

StorageClass

StorageClass defines different types of storage and supports dynamic provisioning of PV.

StorageClass Configuration

yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: fast provisioner: kubernetes.io/aws-ebs parameters: type: gp2 iopsPerGB: "10" reclaimPolicy: Delete volumeBindingMode: WaitForFirstConsumer allowVolumeExpansion: true

Common Provisioners

  1. kubernetes.io/aws-ebs: AWS EBS block storage

  2. kubernetes.io/gce-pd: GCE persistent disk

  3. kubernetes.io/azure-disk: Azure disk

  4. kubernetes.io/azure-file: Azure file storage

  5. kubernetes.io/cinder: OpenStack Cinder

  6. kubernetes.io/nfs: NFS storage

  7. rancher.io/local-path: Local path storage

StorageClass Parameters

  1. provisioner: Storage provider

  2. parameters: Storage provider-specific parameters

  3. reclaimPolicy: Reclamation policy (Delete or Retain)

  4. volumeBindingMode:

    • Immediate: Bind immediately
    • WaitForFirstConsumer: Wait for the first consumer
  5. allowVolumeExpansion: Whether to allow volume expansion

Dynamic Provisioning

Dynamic provisioning allows automatic creation of PV based on PVC without manual administrator creation.

Dynamic Provisioning Process

  1. User creates PVC, specifying StorageClass

  2. PersistentVolumeController monitors the PVC

  3. Controller calls the StorageClass's provisioner to create PV

  4. PV binds to PVC

  5. Pod can use the PVC

Dynamic Provisioning Example

yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: dynamic-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi storageClassName: fast

Static Provisioning

Static provisioning requires administrators to manually create PV, suitable for specific storage requirements.

Static PV Example

yaml
apiVersion: v1 kind: PersistentVolume metadata: name: manual-pv labels: type: local spec: storageClassName: manual capacity: storage: 10Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: /mnt/data

Differences Between PV and PVC

FeaturePVPVC
ScopeCluster levelNamespace level
CreatorAdministrator or dynamic provisioningUser
PurposeProvides storage resourcesRequests storage resources
LifecycleIndependent of PodDependent on Pod

Best Practices

  1. Use Dynamic Provisioning: Prioritize using StorageClass for dynamic provisioning to reduce manual management

  2. Set Reasonable Reclamation Policies: Use Retain for production environments, Delete for test environments

  3. Use Access Modes: Choose appropriate access modes based on application requirements

  4. Monitor Storage Usage: Monitor the usage of PV and PVC

  5. Backup Important Data: Regularly backup important data in PV

  6. Use Labels and Annotations: Add meaningful labels and annotations to PV and PVC

  7. Set Resource Limits: Set reasonable storage requests for PVC

  8. Use Storage Classes: Create different StorageClasses for different application requirements

Troubleshooting

  1. View PV Status:
bash
kubectl get pv kubectl describe pv <pv-name>
  1. View PVC Status:
bash
kubectl get pvc kubectl describe pvc <pvc-name>
  1. View StorageClass:
bash
kubectl get storageclass kubectl describe storageclass <sc-name>
  1. View Pod Mount Status:
bash
kubectl describe pod <pod-name>
  1. View Events:
bash
kubectl get events --sort-by=.metadata.creationTimestamp
标签:Kubernetes