PersistentVolume(PV) and PersistentVolumeClaim(PVC)
Managing storage is a distinct problem from managing compute instances. The PersistentVolume subsystem provides an API for users and administrators that abstracts details of how storage is provided from how it is consumed. To do this, we introduce two new API resources: PersistentVolume and PersistentVolumeClaim.
PersistentVolume
Definition: PVs represent physical storage resources (e.g., disks, cloud storage volumes) that can be dynamically provisioned and made available to Kubernetes workloads.
Ownership: Kubernetes owns PVs, and they are not directly managed by individual pods or applications.
Accessibility: PVs can be mounted as directories within pods, providing persistent storage for data that needs to outlive the lifecycle of individual pods.
Provisioning: PVs can be statically provisioned (created manually) or dynamically provisioned using a storage class, which defines the type of storage and provisioning parameters.
Access Modes: PVs can have different access modes:
ReadWriteOnce(RWO): the volume can be mounted as read-write by a single node. ReadWriteOnce access mode still can allow multiple pods to access the volume when the pods are running on the same node. For single pod access, please see ReadWriteOncePod.
ReadOnlyMany(ROX): the volume can be mounted as read-only by many nodes.
ReadWriteMany(RWX): the volume can be mounted as read-write by many nodes.
ReadWriteOncePod(RWOP): the volume can be mounted as read-write by a single Pod. Use ReadWriteOncePod access mode if you want to ensure that only one pod across the whole cluster can read that PVC or write to it.
Reclaiming: When a user is done with their volume, they can delete the PVC objects from the API that allows reclamation of the resource. The reclaim policy for a PersistentVolume tells the cluster what to do with the volume after it has been released of its claim. Currently, volumes can either be Retained, Recycled, or Deleted.
Retain: The data remains on the PV.
Recycle: The data is deleted, and the PV is made available for reuse.
Delete: The PV and its data are deleted.
Example yaml definition file:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
persistentVolumeReclaimPolicy: Retain
storageClassName: my-storageclass
PersistentVolumeClaim
Definition: PVCs are requests from pods for storage. They specify the desired storage size, access mode, and other requirements.
Ownership: PVCs are owned by pods and are used to request storage from available PVs.
Matching: Kubernetes matches PVCs to suitable PVs based on their storage requirements and access modes.
Binding: Once a PVC is bound to a PV, the pod can mount the PV and access the storage.
Life Cycle: PVCs are created and deleted along with the pods that request them.
Example yaml definition file:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: my-storageclass
Relationship Between PVs and PVCs:
Binding: A PVC is bound to a PV when Kubernetes determines that the PV meets the PVC's requirements.
Unmounting: When a pod is deleted, the PVC is unbound from the PV, and the PV can be used by another pod.
Reclamation Policy: The PVC's reclamation policy determines what happens to the data on the PV when the PVC is deleted.
Key Use Cases:
Stateful Applications: Applications that need to persist data between restarts or across pod terminations, such as databases, message queues, and caching systems.
Data Volume Sharing: Sharing data between multiple pods or applications.
Data Backup and Restore: Backing up and restoring data to and from external storage.
Using PVCs in Pods
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: myfrontend
image: nginx
volumeMounts:
- mountPath: "/var/www/html"
name: mypd
volumes:
- name: mypd
persistentVolumeClaim:
claimName: my-pvc
Subscribe to my newsletter
Read articles from Amal Kuriakose directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by