In Kubernetes, Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) are used to manage storage resources. Here's how you can define them:
-
Persistent Volume (PV): A Persistent Volume (PV) represents a piece of storage in the cluster that has been provisioned by an administrator. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.
Here's an example of how to define a PV:
apiVersion: v1 kind: PersistentVolume metadata: name: example-pv spec: capacity: storage: 1Gi volumeMode: Filesystem accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain storageClassName: example-storage hostPath: path: /path/to/host/directory
- capacity: Specifies the size of the volume.
- volumeMode: Specifies whether the volume is mounted with a filesystem or block mode.
- accessModes: Specifies the access modes for the volume.
- persistentVolumeReclaimPolicy: Specifies what to do with the volume when released.
- storageClassName: Specifies the storage class name.
- hostPath: Specifies the path on the host filesystem.
-
Persistent Volume Claim (PVC): A Persistent Volume Claim (PVC) is a request for storage by a user. It is similar to a pod. Pods consume node resources and PVCs consume PV resources. PVCs can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany, or ReadWriteMany) and can request specific storage classes.
Here's an example of how to define a PVC:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: example-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi storageClassName: example-storage
- accessModes: Specifies the access modes for the volume claim.
- resources.requests.storage: Specifies the requested storage size.
- storageClassName: Specifies the storage class name.
After defining PVs and PVCs, you need to ensure that the PV is available and matches the criteria specified in the PVC (like size and access mode). Once a PVC is created, Kubernetes automatically binds it to an appropriate PV if available.