注意
对于此页面上提到的所有 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 添加分片,并对集群执行其他维护操作。
Considerations
不要在 Kubernetes 内部和外部部署监控代理
由于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 Operator 的MongoDB控制器。
注意
为避免在单集群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
您项目的 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 Operator 的MongoDB控制器。
注意
为避免在单集群Kubernetes部署中存储密钥,您可以将所有密钥迁移到密钥存储工具。多个Kubernetes集群上的部署不支持将密钥存储在密钥存储工具中,例如 HashiCorp Vault 。
部署分片集群
将kubectl 配置为默认命名空间。
如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 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>
为您的分片的 TLS 证书创建密钥。
运行该 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。
为您的配置服务器的 TLS 证书创建密钥。
运行该 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。
为您的 Mongo 服务器的 TLS 证书创建密钥。
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。
为代理的 TLS 证书创建密钥。
运行 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。
创建ConfigMap,将CA与部署链接起来。
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>
复制示例分片集群资源。
更改此 YAML 文件的设置,以匹配所需的分片集群配置。
更改设置,以匹配所需的分片集群配置。
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: <my-sharded-cluster> 6 spec: 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 ...
Paste the copied example to create a new sharded cluster resource.
打开您的首选文本编辑器并将对象规范粘贴到新的文本文件中。
按以下方式配置上一步中突出显示的设置。
键 | 类型 | 说明 | 例子 |
|---|---|---|---|
字符串 |
| ||
整型 | 要部署的分片数量。 |
| |
整型 | 每个分片的分片节点数。 |
| |
整型 | 要部署的分片路由器数量。 |
| |
整型 | 配置服务器副本集的节点数。 |
| |
字符串 | 此分片集群应运行的 MongoDB 版本。 Community 版的格式应为 重要提示:确保选择兼容的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 此值必须与待创建的资源位于同一命名空间。 重要提示:Kubernetes Operator 会跟踪该 ConfigMap 的任何更改并协调 |
| |
字符串 | 您创建的 Secret 名称,其将作为 Ops Manager API 身份验证凭证,以便 Kubernetes Operator 与 Ops Manager 进行通信。 持有凭证的 Ops Manager Kubernetes Secret 对象必须与您要创建的资源位于同一个命名空间中。 重要提示:Kubernetes Operator 会跟踪该 Secret 的任何更改并协调 |
| |
字符串 | 要创建的 |
| |
字符串 | 可选。 Flag indicating if this 如果该值为 要更改持久卷声明配置,请配置以下集合以满足部署要求:
WARNING: Grant your containers permission to write to your Persistent Volume. The Kubernetes Operator sets 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. |
|
Configure the TLS settings for your sharded cluster resource using a custom certificate authority (CA).
要在您的部署中启用 TLS,请在您 Kubernetes 对象中配置以下设置:
键 | 类型 | 必要性 | 说明 | 例子 |
|---|---|---|---|---|
| 字符串 | 必需 | 添加 ConfigMap 的名称,该名称存储用于签署您部署的 TLS 证书的自定义 CA。 |
|
| 字符串 | 必需 | 添加 Secret 名称的 For example, if you call your deployment |
|
为分片集群部署添加任何其他可接受的设置。
您还可以将以下任何可选设置添加到 对象 分片集群 部署的规范文件:
警告
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
对于分片路由器
对于分片分点
Track the status of your sharded cluster deployment.
要检查 MongoDB 资源的状态,请使用以下命令:
kubectl get mdb <resource-name> -o yaml -w
设置 -w (观看) 标志后,当配置更改时,输出将立即刷新,直到状态阶段达到 Running 状态。要了解有关资源部署状态的详情,请参阅 Kubernetes Operator 故障排除。
在您使用 TLS 加密数据库资源后,可以提高以下认证安全性:
更新分片群集的 TLS 证书
使用以下步骤定期更新 TLS 证书:
将kubectl 配置为默认命名空间。
如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 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>
更新分片 TLS 证书的密钥。
运行该 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 -
更新配置服务器 TLS 证书的密钥。
运行此 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 -
Renew the secret for your mongos server's TLS certificates.
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 -
将kubectl 配置为默认命名空间。
如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 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>
复制示例分片集群资源。
更改此 YAML 文件的设置,以匹配所需的分片集群配置。
这是一个 YAML 文件,您可以根据所需配置进行修改。 更改设置,以匹配所需的分片集群配置。
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: <my-sharded-cluster> 6 spec: 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 ...
Paste the copied example to create a new sharded cluster resource.
打开您的首选文本编辑器并将对象规范粘贴到新的文本文件中。
按以下方式配置上一步中突出显示的设置。
键 | 类型 | 说明 | 例子 |
|---|---|---|---|
字符串 |
| ||
整型 | 要部署的分片数量。 |
| |
整型 | 每个分片的分片节点数。 |
| |
整型 | 要部署的分片路由器数量。 |
| |
整型 | 配置服务器副本集的节点数。 |
| |
字符串 | 此分片集群应运行的 MongoDB 版本。 Community 版的格式应为 重要提示:确保选择兼容的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 此值必须与待创建的资源位于同一命名空间。 重要提示:Kubernetes Operator 会跟踪该 ConfigMap 的任何更改并协调 |
| |
字符串 | 您创建的 Secret 名称,其将作为 Ops Manager API 身份验证凭证,以便 Kubernetes Operator 与 Ops Manager 进行通信。 持有凭证的 Ops Manager Kubernetes Secret 对象必须与您要创建的资源位于同一个命名空间中。 重要提示:Kubernetes Operator 会跟踪该 Secret 的任何更改并协调 |
| |
字符串 | 要创建的 |
| |
字符串 | 可选。 Flag indicating if this 如果该值为 要更改持久卷声明配置,请配置以下集合以满足部署要求:
WARNING: Grant your containers permission to write to your Persistent Volume. The Kubernetes Operator sets 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. |
|
为分片集群部署添加任何其他可接受的设置。
您还可以将以下任何可选设置添加到 对象 分片集群 部署的规范文件:
警告
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
对于分片路由器
对于分片分点
Track the status of your sharded cluster deployment.
要检查 MongoDB 资源的状态,请使用以下命令:
kubectl get mdb <resource-name> -o yaml -w
设置 -w (观看) 标志后,当配置更改时,输出将立即刷新,直到状态阶段达到 Running 状态。要了解有关资源部署状态的详情,请参阅 Kubernetes Operator 故障排除。