对于 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 不支持仲裁节点

副本集是一组维护相同数据集的 MongoDB 部署。副本集提供冗余和高可用性,是所有生产部署的基础。

要了解有关副本集的更多信息,请参阅 MongoDB 手册中的复制简介

使用此过程部署 Ops Manager 管理的新副本集。部署后,使用 Ops Manager 管理副本集,包括添加、删除和重新配置成员等操作。

当通过 Kubernetes Operator 部署副本集时,必须选择是否使用 TLS 证书加密连接。

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

  • 在副本集中的 MongoDB 主机之间建立 TLS 加密连接。

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

  • TLS 加密需要有效证书。

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

  • 不加密副本集中 MongoDB 主机之间的连接。

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

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

注意

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

要为分片集群设置 TLS 加密,请参阅部署分片集群

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

要使用对象部署副本集,您必须:

注意

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

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

    • 您的副本集。确保为每个托管证书副本集节点的 Kubernetes 容器添加 SAN

      在您的 TLS 证书中,每个 pod 的 SAN 必须使用以下格式:

      <pod-name>.<metadata.name>-svc.<namespace>.svc.cluster.local

      重要

      If you're using an ACME based service provider such as Let's Encrypt to issue TLS certificates, the provider might prohibit you from adding the Pod's default FQDNs (*.svc.cluster.local) to SANs in the certificate.

      要使用基于ACME的证书,您必须为副本集资源配置证书。 要学习;了解更多信息,请参阅操作步骤中有关基于ACMETLS证书的步骤。

    • 您项目的 MongoDB Agent。对于 MongoDB Agent 证书,请确保您满足以下要求:

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

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

  • You must have the CA certificate file and name it ca-pem.

  • 您必须拥有用于签署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 create secret tls <prefix>-<metadata.name>-cert \
--cert=<replica-set-tls-cert> \
--key=<replica-set-tls-key>

注意

您必须在密钥前面加上 <prefix>-<metadata.name> 作为前缀。

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.

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

要了解有关 Secret 存储的选项,请参阅配置 Secret 存储。

3

运行 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

4

运行此 kubectl 命令将 CA 与副本集关联,并指定 CA 证书文件。

重要

Kubernetes Operator 要求您在 ConfigMap 中将 MongoDB 资源证书命名为 ca-pem

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

更改此 YAML 文件的设立,以匹配所需的副本集配置。

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

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

7
类型
说明
例子

字符串

此 Kubernetes 副本集对象的标签。

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

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

myproject

整型

副本集的节点数。

3

字符串

副本集应运行的 MongoDB 版本。

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

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

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

8.0.0

spec
.opsManager
.configMapRef
.name

字符串

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 资源的状态。

<myconfigmap>

字符串

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

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

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

<mycredentials>

字符串

要创建的 MongoDB 资源的类型。

ReplicaSet

字符串

可选。

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,则 spec.podSpec.persistence.single 设置为默认值 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

8

要在您的部署中启用 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

9

If you're using an ACME based service provider such as Let's Encrypt to issue TLS certificates, the provider might prohibit you from adding the Pod's default FQDNs (*.svc.cluster.local) to SANs in the certificate.

要配置不包含 pod 的 FQDN 的证书:

  1. 为外部域颁发证书。有关详细信息,请参阅 Let's Encrypt 文档或提供商的文档。

  2. 确保您的证书包含您计划在副本集中部署的所有主机名。或者,您可以为 *.<externalDomain> 颁发通配符证书。

  3. 要将仅包含外部域的证书用于副本集部署,必须更改副本集使用的默认主机名:

    • If you prefer to configure the hostname while creating your Kubernetes cluster, change the default domain from cluster.local to the external domain when creating or recreating your Kubernetes cluster. Then, set this domain in your MongoDB resource by using the spec.clusterDomain setting.

    • 否则,请使用 Kubernetes 对象中配置的以下设置来创建 MongoDB 部署:

类型
必要性
说明

spec.externalAccess
.externalDomain

字符串

必需

用于向外部公开您的副本集部署的外部域。

By default, each replica set member uses the Kubernetes Pod's FQDN (*.svc.cluster.local) as the default hostname. However, if you add an external domain to this setting, the replica set uses a hostname that is a subdomain of the specified domain instead. This hostname uses the following format:

<replica-set-name>-<pod-idx>.<externalDomain>

例如:

replica-set-1.example.com

After you deploy the replica set with this setting, the Kubernetes Operator uses the hostname with the external domain to override the processes[n].hostname field in the Ops Manager automation configuration. Then, the MongoDB Agent uses this hostname to connect to mongod.

要指定用于连接到副本集的其他主机名,可以使用 spec.connectivity.replicaSetHorizons 设置。但是,以下连接仍会使用包含外部域的主机名:

WARNING: Specifying this field changes how Ops Manager registers mongod processes. You can't change the value of this field or any processes[n].hostname fields in the Ops Manager automation configuration for a running replica set deployment.

spec.externalAccess
.externalService.spec

集合

Optional

配置 ServiceSpec

当您设置 spec.externalAccess 设置时,Kubernetes Operator 会使用默认值自动创建一个外部负载均衡器服务。您可以根据需要覆盖某些值或添加新值。例如,如果您打算创建 NodePort 服务,并且不需要负载均衡器,则必须在 Kubernetes 规范中配置覆盖项:

externalAccess:
externalService:
annotations:
# cloud-specific annotations for the service
spec:
type: NodePort # default is LoadBalancer
# you can specify other spec overrides if necessary

有关 Kubernetes 规范的更多信息,请参阅 Kubernetes 文档中的 ServiceSpec

spec.externalAccess
.externalService.annotations

集合

Optional

Key-value pairs that let you add cloud provider-specific configuration settings to all clusters in your deployment. To learn more, see annotations and the documentation for your Kubernetes cloud provider.

您可以指定占位符值来自定义注释。要了解更多信息,请参阅 spec.externalAccess.externalService.annotations

11
12

在任何目录下,调用以下 Kubernetes 命令以创建副本集

kubectl apply -f <replica-set-conf>.yaml
13

要检查 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 create secret tls <prefix>-<metadata.name>-cert \
--cert=<replica-set-tls-cert> \
--key=<replica-set-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 文件的设立,以匹配所需的副本集配置。

1---
2apiVersion: mongodb.com/v1
3kind: MongoDB
4metadata:
5 name: <my-replica-set>
6spec:
7 members: 3
8 version: "8.0.0"
9 opsManager:
10 configMapRef:
11 # Must match metadata.name in ConfigMap file
12 name: <configMap.metadata.name>
13 credentials: <mycredentials>
14 type: ReplicaSet
15 persistent: true
16...
3

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

4
类型
说明
例子

字符串

此 Kubernetes 副本集对象的标签。

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

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

myproject

整型

副本集的节点数。

3

字符串

副本集应运行的 MongoDB 版本。

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

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

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

8.0.0

spec
.opsManager
.configMapRef
.name

字符串

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 资源的状态。

<myconfigmap>

字符串

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

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

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

<mycredentials>

字符串

要创建的 MongoDB 资源的类型。

ReplicaSet

字符串

可选。

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,则 spec.podSpec.persistence.single 设置为默认值 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

6
7

在任何目录下,调用以下 Kubernetes 命令以创建副本集

kubectl apply -f <replica-set-conf>.yaml
8

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

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

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