When I started to learn Kubernetes, the concept of PersistentVolumes and PersistentVolumeClaims puzzled me. Here is my attempt to explain it in a simple way.
As you may know, Kubernetes API is declarative. It means that instead of defining particular operations, the configuration describes the desired state of the environment.
The PersistentVolume resource represents the actual disk storage that has been created. It can be either provisioned before by the cluster administrator or provisioned on-demand by the storage engine.
apiVersion: v1
kind: PersistentVolume
(...)
spec:
storageClassName: ...
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
(...)
The PersistentVolumeClaim resource represents a user’s intent of “I need disk storage, these are my requirements.”
apiVersion: v1
kind: PersistentVolumeClaim
(...)
spec:
storageClassName: ...
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
Usual scenario - dynamic provisioning
In the usual scenario:
- User creates a PersistentVolumeClaim resource to express the need for storage with given parameters
- Storage Driver dynamically provisions a disk based on the PVC resource and expresses that with a PersistentVolume resource.
- Kubernetes Control Plane binds PV and PVC together and mounts the volume to a Pod.
Why two separate resources?
You may wonder why we couldn’t just focus on PersistentVolume, and e.g., have Helm charts create it directly instead of Claims resources.
Here are three reasons that I’m aware of:
-
Static provisioning
The dynamic provisioning is just one of the options. The other one is static provisioning.
In that scenario, Kubernetes governs a pool of volumes, that could be even actual physical hard drives. Kubernetes assigns them based on the requests. It may happen that the Pod is bound to a disk that is larger than requested, if there is no other more suitable.
-
Different personas
Usually, PV are either dynamically provisioned or created by the cluster administrator, while the PVC is created by the application owner.
-
Lifecycle
When the PVC is deleted, it doesn’t mean that the disk itself should be deleted. Think about static provisioning scenario, where you may want to recycle a hard drive instead of deleting it from the environment.
Further reading
Actually, the Kubernetes documentation is pretty good on this topic, however the problem is finding the relevant bits.
Make sure to check out:
- https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/ - for an end to end example of creating a disk and mounting it to the Pod
- https://kubernetes.io/docs/concepts/storage/persistent-volumes/ - for a deeper dive into the topic of PersistentVolumes