对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

增加持久卷的存储空间

The Ops Manager, MongoDB Database, AppDB and Backup Daemon custom resources that comprise a standard Kubernetes Operator deployment are each deployed as Kubernetes statefulSets. The Kubernetes Operator supports increasing the storage associated with these specific resources by increasing the capacity of their respective Kubernetes persistentVolumeClaims when the underlying Kubernetes storageClass supports Kubernetes persistentVolume expansion.

根据特定的资源类型,您可以通过以下两种方式之一增加存储。 您可以手动增加存储,也可以利用Kubernetes Operator 的简易存储扩展功能。 下表说明了给定的自定义资源类型支持这两个过程中的哪一个。

自定义资源类型
手动存储扩展
轻松存储扩展

AppDB

备份守护进程

MongoDB 数据库

MongoDB多集群

Ops Manager

确保持久卷使用的 StorageClass 和卷插件提供商程序支持调整大小:

kubectl patch storageclass/<my-storageclass> --type='json' \
-p='[{"op": "add", "path": "/allowVolumeExpansion", "value": true }]'

如果您没有支持调整大小的storageClass ,请向Kubernetes管理员寻求帮助。

注意

简易扩展机制需要Kubernetes Operator 中包含的默认RBAC。 具体来说,它需要getlistwatchpatchupdate persistantVolumeClaims 。 如果您自定义了任何Kubernetes Operator RBAC 资源,则可能需要调整权限,以允许Kubernetes Operator 调整Kubernetes集群中存储资源的大小。

此进程会导致Kubernetes集群中的MongoDB自定义资源滚动重启。

1

使用现有的数据库资源或创建具有持久存储的新数据库资源。 等待持久卷进入Running状态。

例子

具有持久存储的数据库资源包括:

1apiVersion: mongodb.com/v1
2kind: MongoDB
3metadata:
4 name: <my-replica-set>
5spec:
6 members: 3
7 version: "8.0.0"
8 project: my-project
9 credentials: my-credentials
10 type: ReplicaSet
11 podSpec:
12 persistence:
13 single:
14 storage: "1Gi"
2
  1. Start mongo in the Kubernetes cluster.

    $kubectl exec -it <my-replica-set>-0 \
    /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-8.0.0/bin/mongo
  2. 将数据插入test数据库。

    <my-replica-set>:PRIMARY> use test
    switched to db test
    <my-replica-set>:PRIMARY> db.tmp.insertOne({"foo":"bar"})
    {
    "acknowledged" : true,
    "insertedId" : ObjectId("61128cb4a783c3c57ae5142d")
    }
3

重要

只能增加现有存储资源的磁盘大小,而不能减少。 减小存储大小会导致协调阶段出错。

  1. 更新磁盘大小。 打开您首选的文本编辑器并进行与此示例类似的更改:

    例子

    要将副本集的磁盘大小更新为2 GB,请更改数据库资源规范中的storage值:

    1apiVersion: mongodb.com/v1
    2kind: MongoDB
    3metadata:
    4 name: <my-replica-set>
    5spec:
    6 members: 3
    7 version: "8.0.0"
    8 project: my-project
    9 credentials: my-credentials
    10 type: ReplicaSet
    11 podSpec:
    12 persistence:
    13 single:
    14 storage: "2Gi"
  2. 使用新的卷大小更新MongoDB自定义资源。

    kubectl apply -f my-updated-replica-set-vol.yaml
  3. 等待此 StatefulSet 达到Running状态。

4

如果重复使用持久卷,则可以在存储在持久卷中的数据库上找到在步骤 2中插入的数据:

$ kubectl describe mongodb/<my-replica-set> -n mongodb

以下输出表示正在处理您的 PVC 大小调整请求。

status:
clusterStatusList: {}
lastTransition: "2024-08-21T11:03:52+02:00"
message: StatefulSet not ready
observedGeneration: 2
phase: Pending
pvc:
- phase: PVC Resize - STS has been orphaned
statefulsetName: multi-replica-set-pvc-resize-0
resourcesNotReady:
- kind: StatefulSet
message: 'Not all the Pods are ready (wanted: 2, updated: 1, ready: 1, current:2)'
name: multi-replica-set-pvc-resize-0
version: ""
5

If you reuse Persistent Volumes, you can find the data that you inserted in Step 2 on the databases stored in Persistent Volumes:

$ kubectl exec -it <my-replica-set>-1 \
/var/lib/mongodb-mms-automation/mongodb-linux-x86_64-8.0.0/bin/mongo
<my-replica-set>:PRIMARY> use test
switched to db test
<my-replica-set>:PRIMARY> db.tmp.count()
1
1

使用现有数据库资源或创建具有持久存储的新数据库资源。 等待持久卷进入Running状态。

例子

具有持久存储的数据库资源包括:

1apiVersion: mongodb.com/v1
2kind: MongoDB
3metadata:
4 name: <my-replica-set>
5spec:
6 members: 3
7 version: "8.0.0"
8 project: my-project
9 credentials: my-credentials
10 type: ReplicaSet
11 podSpec:
12 persistence:
13 single:
14 storage: "1Gi"
2
  1. Start mongo in the Kubernetes cluster.

    $kubectl exec -it <my-replica-set>-0 \
    /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-8.0.0/bin/mongo
  2. 将数据插入test数据库。

    <my-replica-set>:PRIMARY> use test
    switched to db test
    <my-replica-set>:PRIMARY> db.tmp.insertOne({"foo":"bar"})
    {
    "acknowledged" : true,
    "insertedId" : ObjectId("61128cb4a783c3c57ae5142d")
    }
3

为整个副本集调用以下命令:

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"}}}}'

等到每个 持久卷声明 达到以下条件:

- 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

更新Kubernetes Operator部署定义并将更改应用到Kubernetes集群,以便Kubernetes 扩展缩减为0个副本。 将Kubernetes Operator 缩减到0副本可避免出现争用情况,在争用情况下, Kubernetes Operator 会尝试恢复手动更新资源的状态以与资源的原始定义保持一致。

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-kubernetes-operator
namespace: mongodb
spec:
replicas: 0
5

注意

此步骤仅删除StatefulSet。Pod 保持不变并运行。

删除 StatefulSet 资源。

kubectl delete sts --cascade=false <my-replica-set>
6
  1. 更新磁盘大小。 打开您首选的文本编辑器并进行与此示例类似的更改:

    例子

    要将副本集的磁盘大小更新为2 GB,请更改数据库资源规范中的storage值:

    1apiVersion: mongodb.com/v1
    2kind: MongoDB
    3metadata:
    4 name: <my-replica-set>
    5spec:
    6 members: 3
    7 version: "8.0.0"
    8 project: my-project
    9 credentials: my-credentials
    10 type: ReplicaSet
    11 podSpec:
    12 persistence:
    13 single:
    14 storage: "2Gi"
  2. 使用新的卷大小重新创建StatefulSet资源。

    kubectl apply -f my-replica-set-vol.yaml
  3. 等待MongoDB自定义资源处于Running状态。

7

调用以下命令:

kubectl rollout restart sts <my-replica-set>

新 Pod 会挂载调整大小后的卷。

8

如果重复使用了持久卷,则可以在存储在持久卷中的数据库中找到您在步骤 2中插入的数据:

$ kubectl exec -it <my-replica-set>-1 \
/var/lib/mongodb-mms-automation/mongodb-linux-x86_64-8.0.0/bin/mongo
<my-replica-set>:PRIMARY> use test
switched to db test
<my-replica-set>:PRIMARY> db.tmp.count()
1