Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/
Enterprise Kubernetes Operator
/ /

增加持久卷的存储空间

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

根据特定的资源类型,您可以通过以下两种方式之一增加存储。 您可以手动增加存储,也可以利用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. 在 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值:

    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

如果您重复使用 持久卷 ,您可以在 持久卷 中存储的数据库上找到您在 步骤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
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. 在 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 会尝试恢复手动更新资源的状态以与资源的原始定义保持一致。

# Source: enterprise-operator/templates/operator.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-enterprise-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

后退

扩展部署

在此页面上