Sub topic 13
Dynamic provisioning
This chapter assumes you know how a PersistentVolume (PV) and a PersistentVolumeClaim (PVC) bind, and what a StorageClass is. It also assumes you can apply manifests with kubectl.
Why provision on demand
With static provisioning, an administrator creates each PV by hand before anyone claims it. Dynamic provisioning removes that step. When a PVC names a StorageClass, Kubernetes asks that class's provisioner to create the backing storage and a matching PV. The PV is then bound to the claim.
The flow
A StorageClass and a claim
This class uses the AWS EBS CSI driver:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: trueThe provisioner field selects the driver. parameters are passed to that driver and differ between drivers. The claim only has to name the class:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data
spec:
accessModes:
- ReadWriteOnce
storageClassName: fast
resources:
requests:
storage: 5GiNo PV exists before this claim. The provisioner creates one sized to the request.
Defaults and reclaim behavior
- If a PVC omits
storageClassName, the cluster's default class is used. A default class carries the annotationstorageclass.kubernetes.io/is-default-class: "true". - Setting
storageClassName: ""disables dynamic provisioning. The claim then binds only to a PV that also has no class. - A dynamically created PV inherits the class's
reclaimPolicy. WithDelete, removing the PVC also deletes the PV and the underlying disk, along with the data on it. UseRetainfor data you cannot lose.
Common errors
Check the claim's events first:
kubectl describe pvc app-data
kubectl get storageclass| Symptom | Likely cause |
|---|---|
| Pending, event says the class "not found" | The `storageClassName` has a typo or the class does not exist |
| Pending, no provisioning events | The CSI driver is not installed, or its name does not match `provisioner` |
| Pending with `WaitingForFirstConsumer` | Expected. No Pod uses the claim yet, so no volume is created |
| Pending with no class set | The cluster has no default StorageClass |
The WaitForFirstConsumer case confuses many people. The claim stays Pending on purpose until a Pod that uses it is scheduled. The next chapter explains why.
Exercise
A PVC with no storageClassName stays Pending, and kubectl get storageclass shows no class marked (default). What are two ways to fix it?
Show answer
Set storageClassName on the PVC to an existing class. Alternatively, mark a class as the default with the annotation storageclass.kubernetes.io/is-default-class: "true", so claims without a class use it.