Resizing Persistent Volumes in Kubernetes

Resizing Persistent Volumes in Kubernetes

You want to resize a PersistentVolume (PV) in Kubernetes? In this tutorial, you’ll learn how to do it. If you don’t already know what a PV is and how you can create one, you should check out the tutorial Creating Persistent Volumes in Kubernetes first.

 

Let’s resize

Resizing a PV is not a big deal. Make sure that you edit the PersistentVolumeClaim and NOT the PersistentVolume object. The PVC object can be edited with the command kubectl edit. If you have gone through the tutorial mentioned before, you have a PVC object in your cluster which name is nginx-documentroot. We will increase the size of the corresponding volume from 1 GiB to 5 GiB by editing the PVC object:

$ kubectl edit pvc nginx-documentroot

Your default editor will be opened and it will show the yaml-representation of the PVC object. You can now change the volume size under .spec.resources.requests.storage.

 

After saving and exiting the editor, the volume resize takes place. The default StorageClass (Cinder) in NWS Managed Kubernetes clusters supports Online Expansion, i.e. the filesystem on the volume is resized, if it is mounted to a running pod. This means you don’t have to restart your pods to make the extra storage available to your container applications. Note that decreasing the size of an existing volume is not possible. The Kubernetes-API doesn’t even allow it.

To make sure that the resize was successful, you can take a look at the PVC and the PV objects:

 

The PV was updated so that both objects now show the capacity of 5 GiB. To be completely sure that the resize worked you can kubectl exec into the container that mounts the PV:

 

Further helpful information

This little tutorial showed you how to resize PersistentVolumes. But how do you know when it’s time to do a resize? The Kube Prometheus Stack is a great tool to monitor the disk usage of all the PVs in your cluster. If you want to get notifications as soon as the disk usage on a volume exceeds a certain limit, you might find the tutorial Kubernetes Alerting with Prometheus Alertmanager helpful.