Prerequisites
Storage Class Must Support Resizing
Make sure the StorageClass and volume plugin provider that the Persistent Volumes use supports resize:
kubectl patch storageclass/<my-storageclass> --type='json' \ -p='[{"op": "add", "path": "/allowVolumeExpansion", "value": true }]'
If you don't have a StorageClass that supports resizing, ask your Kubernetes administrator to help.
Procedure
Create or identify a persistent custom resource.
Use an existing database resource or create a new one with persistent
storage. Wait until the persistent volume gets to the Running
state.
Example
A database resource with persistent storage would include:
1 apiVersion: mongodb.com/v1 2 kind: MongoDB 3 metadata: 4 name: <my-replica-set> 5 spec: 6 members: 3 7 version: "4.4.0" 8 project: my-project 9 credentials: my-credentials 10 type: ReplicaSet 11 podSpec: 12 persistence: 13 single: 14 storage: "1Gi"
Insert data to the database that the resource serves.
Start
mongoin the Kubernetes cluster.kubectl exec -it <my-replica-set>-0 \ /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-4.4.0/bin/mongo Insert data into the
testdatabase.<my-replica-set>:PRIMARY> use test switched to db test <my-replica-set>:PRIMARY> db.tmp.insertOne({"foo":"bar"}) { "acknowledged" : true, "insertedId" : ObjectId("61128cb4a783c3c57ae5142d") }
Patch each persistence volume.
Invoke the following commands for the entire replica set:
kubectl patch pvc/"data-<my-replica-set>-0" -p='{"spec": {"resources": {"requests": {"storage": "2Gi"}}}}' kubectl patch pvc/"data-<my-replica-set>-1" -p='{"spec": {"resources": {"requests": {"storage": "2Gi"}}}}' kubectl patch pvc/"data-<my-replica-set>-2" -p='{"spec": {"resources": {"requests": {"storage": "2Gi"}}}}'
Wait until each Persistent Volume Claim gets to the following condition:
- lastProbeTime: null lastTransitionTime: "2019-08-01T12:11:39Z" message: Waiting for user to (re-)start a pod to finish file system resize of volume on node. status: "True" type: FileSystemResizePending
Remove the StatefulSets.
Note
This step removes the StatefulSet only. The pods remain unchanged and running.
Delete a StatefulSet resource.
kubectl delete sts --cascade=false <my-replica-set>
Update the database resource with a new storage value.
Update the disk size. Open your preferred text editor and make changes similar to this example:
Example
To update the disk size of the replica set to 2 GB, change the
storagevalue in database resource specification:1 apiVersion: mongodb.com/v1 2 kind: MongoDB 3 metadata: 4 name: <my-replica-set> 5 spec: 6 members: 3 7 version: "4.4.0" 8 project: my-project 9 credentials: my-credentials 10 type: ReplicaSet 11 podSpec: 12 persistence: 13 single: 14 storage: "2Gi" Recreate a StatefulSet resource with the new volume size.
kubectl apply -f my-replica-set-vol.yaml Wait until this StatefulSet achieves the
Runningstate.
Validate data exists on the updated Persistent Volume Claim.
If the Persistent Volumes were reused, the data that you inserted in Step 2 can be found on the databases stored in Persistent Volumes:
kubectl exec -it <my-replica-set>-1 \ /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-4.4.0/bin/mongo
<my-replica-set>:PRIMARY> use test switched to db test <my-replica-set>:PRIMARY> db.tmp.count() 1