Expanding Persistent Volume Claims without downtime
TypeScriptExpanding a Persistent Volume Claim (PVC) in Kubernetes can often be done without downtime, depending on the storage class and capabilities of the underlying storage provider. Many storage classes support dynamic volume expansion, which allows you to increase the size of a PVC while it is bound to a Persistent Volume (PV) and in use by a pod.
Before you expand a PVC, make sure:
- Your storage class has the
allowVolumeExpansion
field set totrue
. - The storage backend supports volume expansion.
- No other operations are performed on the PVC while resizing is in progress.
Here's a Pulumi TypeScript program that shows how you might patch a Persistent Volume Claim to request more storage, which assumes that the storage class allows for volume expansion and the underlying storage supports it.
First, ensure your Kubernetes cluster and Pulumi are configured correctly.
This Pulumi program will perform a patch operation on an existing PVC to expand its storage capacity. To do this, you'll use the
kubernetes.core.v1.PersistentVolumeClaim
resource, specifically theapply
method to patch the resource dynamically. Theapply
method allows for declaratively specifying changes to resources in a Kubernetes cluster.import * as kubernetes from "@pulumi/kubernetes"; // Name of the existing PVC you want to expand. const pvcName = "my-pvc"; // New size of the Persistent Volume Claim const newSize = "10Gi"; // For example, increase to 10 GiB. // Get reference to the existing Persistent Volume Claim. const persistentVolumeClaim = kubernetes.core.v1.PersistentVolumeClaim.get(pvcName, pvcName); // Patch the existing PVC to request more storage. const expandedPVC = persistentVolumeClaim.apply(pvc => { // Ensure the spec field exists, and has a resources object. if (!pvc.spec || !pvc.spec.resources) { throw new Error("PVC spec or resources are not defined"); } // Ensure we're not reducing the size (which is not supported). const currentSize = pvc.spec.resources.requests["storage"]; if (newSize <= currentSize) { throw new Error("New size must be greater than the current size."); } // Patch the PVC with the new size request. return { ...pvc, spec: { ...pvc.spec, resources: { ...pvc.spec.resources, requests: { "storage": newSize, }, }, }, }; }); // Export the name of the PVC after patching. export const patchedPvcName = expandedPVC.metadata.apply(meta => meta.name);
In this program:
- We import the
@pulumi/kubernetes
package, which gives us access to thePersistentVolumeClaim
resource. - We specify the name of the PVC we wish to expand using the
pvcName
variable. - We set the
newSize
to the desired amount. The storage unit must be the same or larger than the current size and conform to the valid Kubernetes resource quantity specifications. - Using the
get
function, we obtain a reference to the existing PVC. - The
apply
method is utilized to patch the PVC'sspec.resources.requests.storage
property with thenewSize
. This change triggers the volume expansion process. - We have condition checks in place to ensure the
spec
andresources
fields are defined on the PVC and that we're not attempting to reduce the size, which isn't supported. - The updated PVC is returned, and the process of resizing it begins immediately in the underlying infrastructure.
- We export the
patchedPvcName
to provide the name of the PVC after it has been patched. This is useful for identifying the resource after running Pulumi.
Note: After updating the PVC object, Kubernetes will attempt to resize the volume. The success and timing of the resize process depend on your environment and the specifics of the storage provisioner. Some cloud providers may require the pod to be restarted to realize the new size, so consult the documentation for your storage provider.
Remember, this code assumes that the storage class associated with the PVC supports volume expansion, as not all storage systems can handle live changes to volume size. If you attempt to resize a PVC that is not set up for expansion, Kubernetes will not change the size of the PVC.
- Your storage class has the