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

部署分片集群

注意

对于此页面上提到的所有 Ops Manager,您都可以将其替换为 Cloud Manager

重要

  • 您可以使用 Kubernetes Operator 通过 Cloud Manager 和 Ops Manager 6.0.x 版或更高版本来部署 MongoDB 资源。

  • 可以使用 Atlas Operator 将 MongoDB 资源部署到 Atlas。

警告

Kubernetes Operator 不支持仲裁节点

sharded cluster provide horizontal scaling for large data sets and enable high throughput operations by distributing the data set across a group of servers.

要了解有关分片的更多信息,请参阅 MongoDB 手册中的分片简介

可以使用该过程部署 Ops Manager 管理的新分片集群。然后,您可以使用 Ops Manager 添加分片,并对集群执行其他维护操作。

由于Kubernetes网络转换的原因, Kubernetes外部的监控代理无法监控Kubernetes内部的MongoDB实例。 因此,不支持在同一项目中部署 k 8和非 k 8 。 使用单独的项目。

当通过 Kubernetes 运算符部署分片集群时,必须选择是否使用 TLS 证书加密连接。

以下是针对 TLS-Encrypted 连接的步骤:

  • 在集群分片之间建立 TLS 加密连接。

  • 在客户端应用程序和 MongoDB 部署之间建立 TLS 加密连接。

  • TLS 加密需要有效证书。

Non-Encrypted Connections(非加密连接)的过程如下:

  • 不要加密集群分片之间的连接。

  • 不会加密客户端应用程序和 MongoDB 部署之间的连接。

  • 与使用 TLS 加密连接的部署相比,需要的设置更少。

注意

你无法在 Kubernetes 集群中保护 MongoDB 的独立实例。

要为副本集设置 TLS 加密,请参阅部署副本集

根据您是否要使用 TLS 加密副本集连接,选择相应的标签页。

要使用对象部署分片集群,您必须:

注意

为避免在单集群Kubernetes部署中存储密钥,您可以将所有密钥迁移到密钥存储工具。多个Kubernetes集群上的部署不支持将密钥存储在密钥存储工具中,例如 HashiCorp Vault

  • 为以下每个组件生成一个 TLS 证书:

    • 分片集群中的每个分片。 确保将托管分片成员的每个 Kubernetes Pod 的SAN添加到证书。

      TLS证书中,每个分片 Pod 的SAN必须使用以下格式:

      <pod-name>.<metadata.name>-sh.<namespace>.svc.cluster.local
    • 您的配置服务器。 确保将托管配置服务器的每个 Kubernetes Pod 的SAN添加到证书。

      TLS证书中,每个配置服务器Pod 的SAN必须使用以下格式:

      <pod-name>.<metadata.name>-cs.<namespace>.svc.cluster.local
    • Your mongos instances. Ensure that you add SANs for each Kubernetes pod that hosts a mongos to the certificate.

      In your TLS certificates, the SAN for each mongos pod must use this format:

      <pod-name>.<metadata.name>-svc.<namespace>.svc.cluster.local
    • 您项目的 MongoDB Agent。对于 MongoDB Agent 证书,请确保您满足以下要求:

      • TLS 证书中的公用名不为空。

      • 每个 TLS 证书中的组合组织和组织单位与副本集节点的 TLS 证书中的组织和组织单位不同。

  • 您必须拥有CA证书和用于签署TLS证书的密钥。

重要

Kubernetes Operator 使用 kubernetes.io/tls secrets 来存储Ops Manager和MongoDB资源的 TLS 证书和私钥。从Kubernetes Operator 版本 1.17.0 开始, Kubernetes Operator 不支持存储为不透明密钥的连接 PEM 文件。

要使用对象部署分片集群,您必须:

注意

为避免在单集群Kubernetes部署中存储密钥,您可以将所有密钥迁移到密钥存储工具。多个Kubernetes集群上的部署不支持将密钥存储在密钥存储工具中,例如 HashiCorp Vault

1

如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 kubectl 命令。

注意

MongoDB Ops Manager如果要在多 Kubernetes 集群 部署中部署MongoDB 资源:

  • context 设置为操作符集群的名称,例如:kubectl config set context "$MDB_CENTRAL_CLUSTER_FULL_NAME"

  • --namespace设置为您用于多 Kubernetes 集群 MongoDB 部署的相同范围,例如: kubectl config --namespace "mongodb"

kubectl config set-context $(kubectl config current-context) --namespace=<metadata.namespace>
2

运行该 kubectl 命令创建用于存储分片集群分片证书的新密钥

kubectl -n mongodb create secret tls <prefix>-<metadata.name>-0-cert \
--cert=<shard-0-tls-cert> \
--key=<shard-0-tls-key>
kubectl -n mongodb create secret tls <prefix>-<metadata.name>-1-cert \
--cert=<shard-1-tls-cert> \
--key=<shard-1-tls-key>

如果您使用 HashiCorp Vault 作为您的 Secret 存储工具,则可以创建 Vault Secret

3

运行该 kubectl 命令以创建用于存储分片集群配置服务器证书的新密钥:

kubectl -n mongodb create secret tls <prefix>-<metadata.name>-config-cert \
--cert=<config-tls-cert> \
--key=<config-tls-key>

如果您使用 HashiCorp Vault 作为您的 Secret 存储工具,则可以创建 Vault Secret

4

Run this kubectl command to create a new secret that stores the sharded cluster mongos certificate:

kubectl -n mongodb create secret tls <prefix>-<metadata.name>-mongos-cert \
--cert=<mongos-tls-cert> \
--key=<mongos-tls-key>

如果您使用 HashiCorp Vault 作为您的 Secret 存储工具,则可以创建 Vault Secret

5

运行 kubectl 命令可创建用于存储代理 TLS 证书的新Secret

kubectl create secret tls <prefix>-<metadata.name>-agent-certs \
--cert=<agent-tls-cert> \
--key=<agent-tls-key>

如果您使用 HashiCorp Vault 作为您的 Secret 存储工具,则可以创建 Vault Secret

6

Run this kubectl command to link your CA to your sharded cluster and specify the CA certificate file that you must always name ca-pem for the MongoDB resource:

kubectl create configmap custom-ca --from-file=ca-pem=<your-custom-ca-file>
7

更改此 YAML 文件的设置,以匹配所需的分片集群配置。

更改设置,以匹配所需的分片集群配置。

1---
2apiVersion: mongodb.com/v1
3kind: MongoDB
4metadata:
5 name: <my-sharded-cluster>
6spec:
7 shardCount: 2
8 mongodsPerShardCount: 3
9 mongosCount: 2
10 configServerCount: 3
11 version: "8.0.0"
12 opsManager:
13 configMapRef:
14 name: <configMap.metadata.name>
15 # Must match metadata.name in ConfigMap file
16 credentials: <mycredentials>
17 type: ShardedCluster
18 persistent: true
19...
19 security:
20 tls:
21 ca: <custom-ca>
22 certsSecretPrefix: <prefix>
23...
8

打开您的首选文本编辑器并将对象规范粘贴到新的文本文件中。

9
类型
说明
例子

字符串

此 Kubernetes 分片集群对象的标签。

资源名称必须少于或等于 44 个字符。

要学习;了解详情,请参阅 和有关metadata.name 名称的 Kubernetes文档。

myproject

整型

要部署的分片数量。

2

整型

每个分片的分片节点数。

3

整型

要部署的分片路由器数量。

2

整型

配置服务器副本集的节点数。

3

字符串

分片集群应运行的 MongoDB 版本。

Community 版的格式应为 X.Y.Z,Enterprise 版的格式应为 X.Y.Z-ent

重要提示:确保选择兼容的MongoDB Server版本。 兼容版本因MongoDB 数据库资源使用的基础映像而异。

要了解有关 MongoDB 版本控制的更多信息,请参阅 MongoDB 手册中的 MongoDB 版本控制

For best results, use the latest available enterprise MongoDB version that is compatible with your Ops Manager version.

字符串

Name of the ConfigMap with the Ops Manager connection configuration. The spec.cloudManager.configMapRef.name setting is an alias for this setting and can be used in its place.

此值必须与待创建的资源位于同一命名空间。

重要提示:Kubernetes Operator 会跟踪该 ConfigMap 的任何更改并协调 MongoDB 资源的状态。

<myproject>

字符串

创建的 Secret 名称,其将作为 Ops Manager API 身份验证凭证,以便 Kubernetes Operator 与 Ops Manager 进行通信。

持有凭证的 Ops Manager Kubernetes Secret 对象必须与您要创建的资源位于同一个命名空间中。

重要提示:Kubernetes Operator 会跟踪该 Secret 的任何更改并协调 MongoDB 资源的状态。

<mycredentials>

字符串

要创建的 MongoDB 资源的类型。

ShardedCluster

字符串

可选。

Flag indicating if this MongoDB resource should use Persistent Volumes for storage. Persistent volumes are not deleted when the MongoDB resource is stopped or restarted.

如果该值为 true,则以下值将设为 16Gi 的默认值:

要更改持久卷声明配置,请配置以下集合以满足部署要求:

WARNING: Grant your containers permission to write to your Persistent Volume. The Kubernetes Operator sets fsGroup = 2000, runAsUser = 2000, and runAsNonRoot = true in securityContext. Kubernetes Operator sets fsgroup equal to runAsUser to make the volume writable for a user that runs the main process in the container. To learn more, see Configure a Security Context for a Pod or Container and the related discussion in the Kubernetes documentation. If redeploying the resource doesn't fix issues with your Persistent Volume, contact MongoDB Support.

If you do not use Persistent Volumes, the Disk Usage and Disk IOPS charts cannot be displayed in either the Processes tab on the Deployment page or in the Metrics page when reviewing the data for this deployment.

true

10

要在您的部署中启用 TLS,请在您 Kubernetes 对象中配置以下设置:

类型
必要性
说明
例子

spec.security
.tls.ca

字符串

必需

添加 ConfigMap 的名称,该名称存储用于签署您部署的 TLS 证书的自定义 CA

<custom-ca>

spec.security
.certsSecretPrefix

字符串

必需

添加 Secret 名称的 <prefix>,其需包含您 MongoDB 部署的 TLS 证书。

For example, if you call your deployment my-deployment and you set the prefix to mdb, you must name the TLS secret for the client TLS communications mdb-my-deployment-cert. Also, you must name the TLS secret for internal cluster authentication (if enabled) mdb-my-deployment-clusterfile.

devDb

11

您还可以将以下任何可选设置添加到 对象 分片集群 部署的规范文件:

警告

You must set spec.clusterDomain if your Kubernetes cluster has a default domain other than the default cluster.local. If you neither use the default nor set the spec.clusterDomain option, the Kubernetes Operator might not function as expected.

For config server

对于分片路由器

对于分片分点

12
13

调用以下 Kubernetes 命令以创建分片集群

kubectl apply -f <sharded-cluster-conf>.yaml

运行此命令后查看日志。如果创建成功,您应看到类似于以下内容的消息:

2018-06-26T10:30:30.346Z INFO operator/shardedclusterkube.go:52 Created! {"sharded cluster": "my-sharded-cluster"}
14

要检查 MongoDB 资源的状态,请使用以下命令:

kubectl get mdb <resource-name> -o yaml -w

设置 -w (观看) 标志后,当配置更改时,输出将立即刷新,直到状态阶段达到 Running 状态。要了解有关资源部署状态的详情,请参阅 Kubernetes Operator 故障排除

在您使用 TLS 加密数据库资源后,可以提高以下认证安全性:

使用以下步骤定期更新 TLS 证书:

1

如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 kubectl 命令。

注意

MongoDB Ops Manager如果要在多 Kubernetes 集群 部署中部署MongoDB 资源:

  • context 设置为操作符集群的名称,例如:kubectl config set context "$MDB_CENTRAL_CLUSTER_FULL_NAME"

  • --namespace设置为您用于多 Kubernetes 集群 MongoDB 部署的相同范围,例如: kubectl config --namespace "mongodb"

kubectl config set-context $(kubectl config current-context) --namespace=<metadata.namespace>
2

运行该 kubectl 命令以更新用于存储分片集群分片证书的现有密钥

kubectl -n mongodb create secret tls <prefix>-<metadata.name>-0-cert \
--cert=<shard-0-tls-cert> \
--key=<shard-0-tls-key> \
--dry-run=client \
-o yaml |
kubectl apply -f -
kubectl -n mongodb create secret tls <prefix>-<metadata.name>-1-cert \
--cert=<shard-1-tls-cert> \
--key=<shard-1-tls-key> \
--dry-run=client \
-o yaml |
kubectl apply -f -
3

运行此 kubectl 命令,以更新存储分片集群配置服务器证书的现有密钥

kubectl -n mongodb create secret tls <prefix>-<metadata.name>-config-cert \
--cert=<config-tls-cert> \
--key=<config-tls-key> \
--dry-run=client \
-o yaml |
kubectl apply -f -
4

Run this kubectl command to renew an existing secret that stores the sharded cluster mongos certificates:

kubectl -n mongodb create secret tls <prefix>-<metadata.name>-mongos-cert \
--cert=<mongos-tls-cert> \
--key=<mongos-tls-key> \
--dry-run=client \
-o yaml |
kubectl apply -f -
1

如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 kubectl 命令。

注意

MongoDB Ops Manager如果要在多 Kubernetes 集群 部署中部署MongoDB 资源:

  • context 设置为操作符集群的名称,例如:kubectl config set context "$MDB_CENTRAL_CLUSTER_FULL_NAME"

  • --namespace设置为您用于多 Kubernetes 集群 MongoDB 部署的相同范围,例如: kubectl config --namespace "mongodb"

kubectl config set-context $(kubectl config current-context) --namespace=<metadata.namespace>
2

更改此 YAML 文件的设置,以匹配所需的分片集群配置。

这是一个 YAML 文件,您可以根据所需配置进行修改。 更改设置,以匹配所需的分片集群配置。

1---
2apiVersion: mongodb.com/v1
3kind: MongoDB
4metadata:
5 name: <my-sharded-cluster>
6spec:
7 shardCount: 2
8 mongodsPerShardCount: 3
9 mongosCount: 2
10 configServerCount: 3
11 version: "8.0.0"
12 opsManager:
13 configMapRef:
14 name: <configMap.metadata.name>
15 # Must match metadata.name in ConfigMap file
16 credentials: <mycredentials>
17 type: ShardedCluster
18 persistent: true
19...
3

打开您的首选文本编辑器并将对象规范粘贴到新的文本文件中。

4
类型
说明
例子

字符串

此 Kubernetes 分片集群对象的标签。

资源名称必须少于或等于 44 个字符。

要学习;了解详情,请参阅 和有关metadata.name 名称的 Kubernetes文档。

myproject

整型

要部署的分片数量。

2

整型

每个分片的分片节点数。

3

整型

要部署的分片路由器数量。

2

整型

配置服务器副本集的节点数。

3

字符串

分片集群应运行的 MongoDB 版本。

Community 版的格式应为 X.Y.Z,Enterprise 版的格式应为 X.Y.Z-ent

重要提示:确保选择兼容的MongoDB Server版本。 兼容版本因MongoDB 数据库资源使用的基础映像而异。

要了解有关 MongoDB 版本控制的更多信息,请参阅 MongoDB 手册中的 MongoDB 版本控制

For best results, use the latest available enterprise MongoDB version that is compatible with your Ops Manager version.

字符串

Name of the ConfigMap with the Ops Manager connection configuration. The spec.cloudManager.configMapRef.name setting is an alias for this setting and can be used in its place.

此值必须与待创建的资源位于同一命名空间。

重要提示:Kubernetes Operator 会跟踪该 ConfigMap 的任何更改并协调 MongoDB 资源的状态。

<myproject>

字符串

创建的 Secret 名称,其将作为 Ops Manager API 身份验证凭证,以便 Kubernetes Operator 与 Ops Manager 进行通信。

持有凭证的 Ops Manager Kubernetes Secret 对象必须与您要创建的资源位于同一个命名空间中。

重要提示:Kubernetes Operator 会跟踪该 Secret 的任何更改并协调 MongoDB 资源的状态。

<mycredentials>

字符串

要创建的 MongoDB 资源的类型。

ShardedCluster

字符串

可选。

Flag indicating if this MongoDB resource should use Persistent Volumes for storage. Persistent volumes are not deleted when the MongoDB resource is stopped or restarted.

如果该值为 true,则以下值将设为 16Gi 的默认值:

要更改持久卷声明配置,请配置以下集合以满足部署要求:

WARNING: Grant your containers permission to write to your Persistent Volume. The Kubernetes Operator sets fsGroup = 2000, runAsUser = 2000, and runAsNonRoot = true in securityContext. Kubernetes Operator sets fsgroup equal to runAsUser to make the volume writable for a user that runs the main process in the container. To learn more, see Configure a Security Context for a Pod or Container and the related discussion in the Kubernetes documentation. If redeploying the resource doesn't fix issues with your Persistent Volume, contact MongoDB Support.

If you do not use Persistent Volumes, the Disk Usage and Disk IOPS charts cannot be displayed in either the Processes tab on the Deployment page or in the Metrics page when reviewing the data for this deployment.

true

5

您还可以将以下任何可选设置添加到 对象 分片集群 部署的规范文件:

警告

You must set spec.clusterDomain if your Kubernetes cluster has a default domain other than the default cluster.local. If you neither use the default nor set the spec.clusterDomain option, the Kubernetes Operator might not function as expected.

For config server

对于分片路由器

对于分片分点

6
7

调用以下 Kubernetes 命令以创建分片集群

kubectl apply -f <sharded-cluster-conf>.yaml

运行此命令后查看日志。如果创建成功,您应看到类似于以下内容的消息:

2018-06-26T10:30:30.346Z INFO operator/shardedclusterkube.go:52 Created! {"sharded cluster": "my-sharded-cluster"}
8

要检查 MongoDB 资源的状态,请使用以下命令:

kubectl get mdb <resource-name> -o yaml -w

设置 -w (观看) 标志后,当配置更改时,输出将立即刷新,直到状态阶段达到 Running 状态。要了解有关资源部署状态的详情,请参阅 Kubernetes Operator 故障排除