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

增加持久卷的存储空间

MongoDB Ops ManagerMongoDB构成标准Kubernetes Operator部署的 、 数据库、AppDB 和备份守护程序自定义资源均会部署为KubernetesstatefulSets 。当根本的Kubernetes storageClass支持Kubernetes persistentVolume扩展时, Kubernetes Operator 支持通过增加各自Kubernetes persistentVolumeClaims的容量来增加与这些特定资源关联的存储。

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

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

AppDB

备份守护进程

MongoDB 数据库

MongoDB多集群

Ops Manager

这些程序不涵盖 MongoDB Search 索引存储。要了解如何增加存储空间,请参阅扩展 MongoDB Search 索引存储。

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

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

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

注意

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

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

1

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

例子

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

apiVersion: mongodb.com/v1kind: MongoDBmetadata: name: <my-replica-set>spec: members: 3 version: "8.0.0" project: my-project credentials: my-credentials type: ReplicaSet podSpec: persistence: single: storage: "1Gi"
2
  1. 在 Kubernetes 集群中启动mongo 。

    $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值:

    apiVersion: mongodb.com/v1kind: MongoDBmetadata: name: <my-replica-set>spec: members: 3 version: "8.0.0" project: my-project credentials: my-credentials type: ReplicaSet podSpec: persistence: single: 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

如果重复使用 持久卷 ,则可以在存储在 持久卷 中的数据库上找到在 步骤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 testswitched to db test <my-replica-set>:PRIMARY> db.tmp.count()1
1

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

例子

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

apiVersion: mongodb.com/v1kind: MongoDBmetadata: name: <my-replica-set>spec: members: 3 version: "8.0.0" project: my-project credentials: my-credentials type: ReplicaSet podSpec: persistence: single: storage: "1Gi"
2
  1. 在 Kubernetes 集群中启动mongo 。

    $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/v1kind: Deploymentmetadata: name: mongodb-kubernetes-operator namespace: mongodbspec: replicas: 0
5

注意

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

删除 StatefulSet 资源。

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

    例子

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

    apiVersion: mongodb.com/v1kind: MongoDBmetadata: name: <my-replica-set>spec: members: 3 version: "8.0.0" project: my-project credentials: my-credentials type: ReplicaSet podSpec: persistence: single: 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 testswitched to db test <my-replica-set>:PRIMARY> db.tmp.count()1