Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of MongoDB Kubernetes Operator, refer to the upgrade documentation.

Resize Storage for One Database Resource

On this page

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

1

Create or identify a persistent Custom Resource.

Use an existing database resource or create a new one with persistent storage. Wait until the peristent volume gets to the Running state.

Example

A database resource with persistent storage would include:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
  name: <my-replica-set>
spec:
  members: 3
  version: 4.0.4
  project: my-project
  credentials: my-credentials
  type: ReplicaSet
  podSpec:
    memory: 300M
    persistence:
      single:
        storage: 1G
2

Insert data to the database that the resource serves.

  1. Start mongo in the Kubernetes cluster.

    $kubectl exec -it <my-replica-set>-0 \
             /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-4.0.4/bin/mongo
    
  2. Insert data into the test database.

    <my-replica-set>:PRIMARY> use test
    
    switched to db test
    
    <my-replica-set>:PRIMARY> db.tmp.insert({"foo":"bar"})
    
    WriteResult({ "nInserted" : 1 })
    
3

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
4

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>
5

Update the database resource with a new storage value.

  1. 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 storage value in database resource specification:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    apiVersion: mongodb.com/v1
    kind: MongoDB
    metadata:
      name: <my-replica-set>
    spec:
      members: 3
      version: 4.0.4
      project: my-project
      credentials: my-credentials
      type: ReplicaSet
      podSpec:
        memory: 300M
        persistence:
          single:
            storage: 2G
    
  2. Recreate a StatefulSet resource with the new volume size.

    kubectl apply -f my-replica-set-vol.yaml
    
  3. Wait until this StatefulSet achieves the Running state.

6

Update the pods in a rolling fashion.

Invoke the following command:

kubectl rollout restart sts <my-replica-set>

The new pods mount the resized volume.

7

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.0.4/bin/mongo
<my-replica-set>:PRIMARY> use test
switched to db test

<my-replica-set>:PRIMARY> db.tmp.count()
1