Sub topic 16
Expanding a volume
This chapter assumes you know how PersistentVolumeClaims bind to volumes and how a StorageClass provisions storage dynamically. You also need to be comfortable with kubectl and YAML.
What expansion does
Volume expansion grows an existing claim without recreating it or losing data. You edit the requested size on the PersistentVolumeClaim (PVC). Kubernetes asks the storage driver to enlarge the underlying volume, then the filesystem is resized.
Two limits apply:
- You can only grow a claim. Requests to shrink are rejected.
- The StorageClass must set
allowVolumeExpansion: true, and the CSI driver must support resizing.
The fast class from the dynamic provisioning chapter already meets the first condition:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: trueIf a class lacks this field, the API server rejects any attempt to enlarge claims that use it.
Requesting more space
Patch the claim with a larger size:
kubectl get pvc demo-pvc
kubectl patch pvc demo-pvc -p '{"spec":{"resources":{"requests":{"storage":"2Gi"}}}}'
kubectl describe pvc demo-pvc
kubectl get pvc demo-pvcThe PVC spec shows the size you asked for. The status.capacity field shows what exists now. Right after the patch, the two differ. They match again once the resize finishes.
The resize sequence
The resize has two parts. The controller grows the backing volume, for example the EBS disk. The kubelet then grows the filesystem on the node where the volume is mounted.
Many drivers support online expansion, so a running Pod keeps working while the filesystem grows. Others need the Pod to restart before the filesystem part completes. In that case the PVC shows a condition named FileSystemResizePending. Delete the Pod and let its controller recreate it, and the resize finishes on the next mount.
Common errors
| Symptom | Likely cause |
|---|---|
| `forbidden: only dynamically provisioned pvc can be resized and the storageclass that provisions the pvc must support resize` | The class has no `allowVolumeExpansion: true`, or the PV was created by hand |
| Size does not change after the patch | The CSI driver does not support expansion, or its resizer sidecar is not running |
| `FileSystemResizePending` stays set | The filesystem resize waits for a Pod to mount the volume again |
| Request to lower the size is rejected | Claims cannot shrink |
Events from kubectl describe pvc name the failing step, so check them first.
Exercise
A PVC requests 5Gi from a class that has allowVolumeExpansion: true. You patch it to 10Gi. kubectl get pvc still reports a capacity of 5Gi, and describe shows FileSystemResizePending. What is happening, and what do you do?
Show answer
The backing volume has already grown, but the filesystem resize needs the volume to be mounted again by a Pod, because this driver does not expand the filesystem online. Delete the Pod so its controller recreates it. When the volume is mounted again, the kubelet grows the filesystem and status.capacity changes to 10Gi.