Ninja Docs Help

Statefulset: Resize PVC in volumeClaimTemplates

Revision

Date

Description

1.0

24.07.2024

Init Changelog

Introduction

Kubernetes StatefulSets are used to deploy stateful applications inside your cluster. Each Pod in the StatefulSet can access local persistent volumes that stick to it even after it’s rescheduled. This allows Pods to maintain individual state that's separate from their neighbors in the set. Unfortunately, Kubernetes doesn’t provide a way to resize them from the StatefulSet object. The spec.resources.requests.storage property of the StatefulSet field is immutable, preventing you from applying any capacity increases you require. This article will show you how to workaround the problem.

Instructions

You can bypass the restriction by manually resizing the persistent volume claim (PVC). You'll then need to recreate the StatefulSet to release and rebind the volume from your Pods. This will trigger the actual volume resize event.

  1. Find the PVCs associated with your StatefulSet with kubectl:

kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES example-0 Bound pvc-ccb2c835-e2d3-4632-b8ba-4c8c142795e4 1Gi RWO example-1 Bound pvc-1b0b27fe-3874-4ed5-91be-d8e552e515f2 1Gi RWO example-2 Bound pvc-4b7790c2-3ae6-4e04-afee-a2e1bae4323b 1Gi RWO
  1. Use kubectl edit to enter in-cluster resource edit mode:

kubectl edit pvc example-0
  1. In opened editor find the spec.resource.requests.storage field and change it to your new desired capacity:

--- # ... spec: resources: requests: - storage: 1Gi + storage: 2Gi
  1. Save and close file. kubectl should report that the change has been applied to your cluster:

persistentvolumeclaim/example-0 edited
  1. Repeat steps 2-4 for rest PVCs from StatefulSet (you need to edit all associated claims).

  2. Wait till Kubernetes update all claims. Check status with kubectl:

kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES example-0 Bound pvc-ccb2c835-e2d3-4632-b8ba-4c8c142795e4 2Gi RWO example-1 Bound pvc-1b0b27fe-3874-4ed5-91be-d8e552e515f2 2Gi RWO example-2 Bound pvc-4b7790c2-3ae6-4e04-afee-a2e1bae4323b 2Gi RWO
  1. Update StatefulSet YAML manifest with new value (if you use GitOps tool like FluxCD you need to commit your changes).

  2. Delete StatefulSet with orphan cascading mechanism (its Pods remain in cluster) - this minimize downtime:

kubectl delete statefulset --cascade=orphan example
statefulset.apps "example" deleted
  1. Apply StatefulSet YAML manifest - the new StatefulSet will assume ownership of the previously orphaned Pods.

  2. Perform rollout restart on StatefulSet:

kubectl rollout restart statefulset example
Last modified: 17 February 2025