Extending Persistent Volume Claims (PVC) Managed by StatefulSets(K8s)

MouliVeera
2 min readAug 22, 2023

In Kubernetes, StatefulSets manage stateful applications by providing unique identities and persistent storage for each pod. When the need arises to extend the storage capacity of Persistent Volume Claims (PVCs) managed by StatefulSets, it’s important to follow the correct procedure to ensure data integrity and application stability.

Extending PVC Managed by StatefulSets

Extending the storage capacity of PVCs managed by StatefulSets involves updating the StatefulSet’s spec.volumeClaimTemplates section to adjust the storage request. The StatefulSet controller will then automatically handle the rolling update of pods with the extended PVCs.

Handling PVC Updates Safely

As volumeClaimTemplates are immutable, directly modifying them within the StatefulSet specification won't result in the desired effect, especially for changing PVC properties like the storage size. Instead, follow these steps for safe PVC updates:

  • Delete StatefulSet: Delete the StatefulSet using the --cascade=orphan flag to retain pods:
kubectl delete statefulsets <statefulset-name> --cascade=orphan
  • Update PVC Templates: After deleting the StatefulSet, update the PVC template(s) with the desired changes, including storage size.
  • Recreate StatefulSet: Recreate the StatefulSet with the updated PVC template(s) using kubectl apply.
  • Data Migration: Depending on the changes made, manually manage data migration if needed. For example, increasing storage size requires migrating data to new PVCs.

Examples:

Example 1: Extending PVC via StatefulSet Spec

  1. Identify the name of the StatefulSet managing the PVC you want to extend:
kubectl get statefulsets

2. Delete the StatefulSet to retain pods:

kubectl delete statefulsets <statefulset-name> --cascade=orphan

3. Update PVC template(s) with the desired changes.

Locate the volumeClaimTemplates section within the StatefulSet YAML(extend-statefulset.yaml) and modify the spec.resources.requests.storage value to the desired size:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: <statefulset-name>
spec:
volumeClaimTemplates:
- metadata:
name: <pvc-name>
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: <new-size>

4. Apply the changes to extend the PVC via the StatefulSet:

kubectl apply -f extend-statefulset.yaml

Conclusion

Extending the storage capacity of PVCs managed by StatefulSets is essential for accommodating growing data requirements. By following the examples provided in this README and adhering to safe update practices, you can confidently extend PVCs while ensuring the stability and data integrity of your stateful applications.

References

--

--

MouliVeera

Mouli is a seasoned DevOps Engineer with expertise in designing and optimising CI/CD pipelines, containerisation with Docker and Kubernetes.