Resizing Persistent Volumes
In this tutorial we are going to guide you with the resize of Persistent Volumes (PVs) on your OVHcloud Managed Kubernetes Service.
The Kubernetes 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 Kubernetes provides two API resources: PersistentVolume (PVs) and PersistentVolumeClaim (PVCs).
Since Kubernetes 1.11, support for expanding PersistentVolumeClaims (PVCs) is enabled by default, and in this tutorial you will learn how to do it.
Kubernetes PVCs resizing only allows to expand volumes, not to decrease them.
Volume resizing works with all storage classes, including LUKS encrypted volumes (csi-cinder-high-speed-luks, csi-cinder-classic-luks, csi-cinder-high-speed-gen2-luks). The encryption is transparently maintained during the resize operation.
Before you begin
This tutorial presupposes that you already have a working OVHcloud Managed Kubernetes cluster, and some basic knowledge of how to operate it. If you want to know more on those topics, please look at the deploying a Hello World application documentation.
You also need to know how PVs are handled on OVHcloud Managed Kubernetes service, please refer to the Persistent Volumes on OVHcloud Managed Kubernetes guide.
When a Persistent Volumes resource is created inside a Managed Kubernetes cluster, an associated Public Cloud Block Storage volume is automatically created with it. This volume is hourly charged and will appear in your Public Cloud project. For more information, please refer to the following documentation: Volume Block Storage price
Let's make a Persistent Volume Claim
To test the PVs resizing, we will need a PV associated to the cluster, i.e. we need to deploy a service making a PVC. To keep thing simple, we choose to deploy a single instance of MySQL.
Let's begin by creating a mysql-pvc.yaml to define an initial PVC with 2 GB of allocated space:
And apply it to the cluster:
We can then verify that the PVC is correctly created and bound to a PV:
Now we create a mysql-deployment.yaml file to deploy the MySQL using that PVC:
And we deploy and verify it:
In my example cluster, the precedent commands obtains:
Accessing the MySQL instance and initializing a database
The preceding YAML file creates a service that allows other Pods in the cluster to access the database. The Service option clusterIP: None lets the Service DNS name resolve directly to the Pod’s IP address. This is optimal when you have only one Pod behind a Service and you don’t intend to increase the number of Pods.
Now we are going to use kubectl to create a mysql client on the fly to connect to the database:
And then simply create a database with a table:
On my example cluster:
Expand the PVs
In order to expand the persistent volume, the first step is to unbound the PVC from the deployments using them.
To do that, we set the deployment's replicas to 0:
Do not forget to downscale your deployment before to resize your volume
Then we path the PVC definition to expand the volume to 6 GB:
Kubernetes PVCs resizing only allows to expand volumes, not to decrease them. If you try to decrease the storage size, you will get a message like
The PersistentVolumeClaim "mysql-pv-claim" is invalid: spec.resources.requests.storage: Forbidden: field can not be less than previous value
We verify that the volume has been expanded:
In the "conditions" field shown by the output of the previous command line, we can see that the PVC is waiting for user to start a pod to finish file system resize of the volume.
Let's put replicas back to 1 on mysql-deployment.yaml, and deploy it again to start a pod:
After the pod starts, we can use again kubectl describe pvc mysql-pv-claim and we see that the PV size is 6 GB.
On my example cluster:
Verifying data integrity
So we launch again a MySQL client to verify that we can still read our database:
An SHOW DATABASES; should allow us to see our testingResize database, we can select it and find our anEmptyTable table.
On my example cluster:
Where do we go from here?
Now you can expand the Persistent Volumes on your OVHcloud Managed Kubernetes cluster, and adapt them to the live of your data.
To learn more about using your Kubernetes cluster the practical way, we invite you to look at our OVHcloud Managed Kubernetes documentation site.
-
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for assisting you on your specific use case of your project.
-
Join our community of users.