Expanding a StatefulSet controller volume
To increase the size of a volume for the StatefulSet controller without shutting down the service:
Before you start, install kubectl
Create a StatefulSet controller
-
Create a file named
sts.yaml
with a controller configuration:sts.yaml
apiVersion: apps/v1 kind: StatefulSet metadata: name: ubuntu-test spec: selector: matchLabels: app: ubuntu serviceName: "ubuntu" replicas: 3 template: metadata: labels: app: ubuntu spec: terminationGracePeriodSeconds: 10 containers: - name: ubuntu image: ubuntu command: ["/bin/sh"] args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"] volumeMounts: - mountPath: /data name: pvc-dynamic volumeClaimTemplates: - metadata: name: pvc-dynamic spec: accessModes: [ "ReadWriteOnce" ] storageClassName: "yc-network-hdd" resources: requests: storage: 1Gi
-
Create a controller:
kubectl apply -f sts.yaml
The command will create a StatefulSet controller named
ubuntu-test
comprising three pods. The size of PersistentVolumeClaim per pod is 1 GB. -
Make sure the controller pod status changed to
Running
and PersistentVolumeClaim, toBound
:kubectl get pods,pvc
Result:
NAME READY STATUS RESTARTS AGE pod/ubuntu-test-0 1/1 Running 0 90s pod/ubuntu-test-1 1/1 Running 0 80s pod/ubuntu-test-2 1/1 Running 0 72s NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE persistentvolumeclaim/pvc-dynamic-ubuntu-test-0 Bound pvc-603ac129-fe56-400a-8481-feaa******** 1Gi RWO yc-network-hdd 91s persistentvolumeclaim/pvc-dynamic-ubuntu-test-1 Bound pvc-a6fb0761-0771-483c-abfb-d4a8******** 1Gi RWO yc-network-hdd 81s persistentvolumeclaim/pvc-dynamic-ubuntu-test-2 Bound pvc-f479c8aa-426a-4e43-9749-5e0f******** 1Gi RWO yc-network-hdd 73s
-
Make sure that, for objects with the
k8s-csi
prefix, the disk status changed toREADY
:yc compute disk list
Result:
+----------------------+--------------------------------------------------+------------+-------------------+--------+----------------------+-------------+ | ID | NAME | SIZE | ZONE | STATUS | INSTANCE IDS | DESCRIPTION | +----------------------+--------------------------------------------------+------------+-------------------+--------+----------------------+-------------+ | ef3b5ln111s3******** | k8s-csi-15319ac44278c2ff23f0df04ebdbe5a8******** | 1073741824 | ru-central1-a | READY | ef3nrev9j72t******** | | | ef3e617rmqri******** | k8s-csi-336f16a11f750525075d7c155ad26ae3******** | 1073741824 | ru-central1-a | READY | ef3nrev9j72t******** | | | ef3rfleqkit0******** | k8s-csi-ba784ddd49c7aabc63bcbfc45be3cc2e******** | 1073741824 | ru-central1-a | READY | ef3nrev9j72t******** | | +----------------------+--------------------------------------------------+------------+-------------------+--------+----------------------+-------------+
Make changes to controller settings
-
Save the current
ubuntu-test
controller configuration to a file namedubuntu-test-sts.yaml
:kubectl get sts ubuntu-test --output yaml > ubuntu-test-sts.yaml
-
In
ubuntu-test-sts.yaml
, increase the value for thevolumeClaimTemplates.spec.resources.requests.storage
parameter from1Gi
to2Gi
:... spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi
Note
There is no need to increase the corresponding value of
1Gi
for thekubectl.kubernetes.io/last-applied-configuration
parameter. -
Delete the current
ubuntu-test
StatefulSet controller:kubectl delete statefulset ubuntu-test --cascade=orphan
-
Make sure the StatefulSet controller has been deleted:
kubectl get sts
-
Delete the first pod,
ubuntu-test-0
:kubectl delete pod ubuntu-test-0
-
Make changes to the PersistentVolumeClaim of the deleted
ubuntu-test-0
pod by increasing the storage size to 2 GB:kubectl patch pvc pvc-dynamic-ubuntu-test-0 --patch '{"spec": {"resources": {"requests": {"storage": "2Gi"}}}}'
-
Apply the changes to the
ubuntu-test
controller:kubectl apply -f ubuntu-test-sts.yaml
-
Reduce the number of the
ubuntu-test
controller pods to 1:kubectl scale statefulset ubuntu-test --replicas=1
-
Increase the storage size for the
ubuntu-test-1
andubuntu-test-2
pods to 2 GB:kubectl patch pvc pvc-dynamic-ubuntu-test-1 --patch '{"spec": {"resources": {"requests": {"storage": "2Gi"}}}}' && \ kubectl patch pvc pvc-dynamic-ubuntu-test-2 --patch '{"spec": {"resources": {"requests": {"storage": "2Gi"}}}}'
-
Set the number of the
ubuntu-test
controller pods back to 3:kubectl scale statefulset ubuntu-test --replicas=3
-
Make sure PersistentVolume for the
ubuntu-test
controller has increased to 2 GB for each volume:kubectl get pv
Result:
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-603ac129-fe56-400a-8481-feaa******** 2Gi RWO Delete Bound default/pvc-dynamic-ubuntu-test-0 yc-network-hdd 11m pvc-a6fb0761-0771-483c-abfb-d4a8******** 2Gi RWO Delete Bound default/pvc-dynamic-ubuntu-test-1 yc-network-hdd 11m pvc-f479c8aa-426a-4e43-9749-5e0f******** 2Gi RWO Delete Bound default/pvc-dynamic-ubuntu-test-2 yc-network-hdd 11m