注意
对于此页面上提到的所有 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 管理副本集,包括添加、删除和重新配置成员等操作。
Considerations
当通过 Kubernetes Operator 部署副本集时,必须选择是否使用 TLS 证书加密连接。
以下是针对 TLS-Encrypted 连接的步骤:
在副本集中的 MongoDB 主机之间建立 TLS 加密连接。
在客户端应用程序和 MongoDB 部署之间建立 TLS 加密连接。
TLS 加密需要有效证书。
Non-Encrypted Connections(非加密连接)的过程如下:
不加密副本集中 MongoDB 主机之间的连接。
不会加密客户端应用程序和 MongoDB 部署之间的连接。
与使用 TLS 加密连接的部署相比,需要的设置更少。
注意
你无法在 Kubernetes 集群中保护 MongoDB 的独立实例。
要为分片集群设置 TLS 加密,请参阅部署分片集群。
根据您是否要使用 TLS 加密副本集连接,选择相应的标签页。
先决条件
要使用对象部署副本集,您必须:
拥有或安装适用于Kubernetes Operator 的MongoDB控制器。
注意
为避免在单集群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的证书,您必须为副本集资源配置证书。 要学习;了解更多信息,请参阅操作步骤中有关基于ACME的TLS证书的步骤。
您项目的 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 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 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 存储。
为代理的 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与部署链接起来。
运行此 kubectl 命令将 CA 与副本集关联,并指定 CA 证书文件。
重要
Kubernetes Operator 要求您在 ConfigMap 中将 MongoDB 资源证书命名为 ca-pem。
kubectl create configmap custom-ca --from-file=ca-pem=<your-custom-ca-file>
Copy the sample replica set resource.
更改此 YAML 文件的设立,以匹配所需的副本集配置。
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: <my-replica-set> 6 spec: 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 ...
Paste the copied example to create a new replica set resource.
打开您的首选文本编辑器并将对象规范粘贴到新的文本文件中。
根据您的偏好更改设置的值。
键 | 类型 | 说明 | 例子 |
|---|---|---|---|
字符串 |
| ||
整型 | 副本集的节点数。 |
| |
字符串 | 此副本集应运行的 MongoDB 版本。 Community 版的格式应为 重要提示:确保选择兼容的MongoDB Server版本。 兼容版本因MongoDB 数据库资源使用的基础映像而异。 要了解有关 MongoDB 版本控制的更多信息,请参阅 MongoDB 手册中的 MongoDB 版本控制。 |
| |
| 字符串 | 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 replica set resource using a custom certificate authority (CA).
要在您的部署中启用 TLS,请在您 Kubernetes 对象中配置以下设置:
键 | 类型 | 必要性 | 说明 | 例子 |
|---|---|---|---|---|
| 字符串 | 必需 | 添加 ConfigMap 的名称,该名称存储用于签署您部署的 TLS 证书的自定义 CA。 |
|
| 字符串 | 必需 | 添加 Secret 名称的 For example, if you call your deployment |
|
Optional: Configure ACME based TLS certificates for your replica set resource.
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 的证书:
为外部域颁发证书。有关详细信息,请参阅 Let's Encrypt 文档或提供商的文档。
确保您的证书包含您计划在副本集中部署的所有主机名。或者,您可以为
*.<externalDomain>颁发通配符证书。要将仅包含外部域的证书用于副本集部署,必须更改副本集使用的默认主机名:
If you prefer to configure the hostname while creating your Kubernetes cluster, change the default domain from
cluster.localto the external domain when creating or recreating your Kubernetes cluster. Then, set this domain in your MongoDB resource by using thespec.clusterDomainsetting.否则,请使用 Kubernetes 对象中配置的以下设置来创建 MongoDB 部署:
键 | 类型 | 必要性 | 说明 | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 字符串 | 必需 | 用于向外部公开您的副本集部署的外部域。 By default, each replica set member uses the Kubernetes Pod's FQDN (
例如:
After you deploy the replica set with this setting, the Kubernetes Operator uses the hostname with the external domain to override the 要指定用于连接到副本集的其他主机名,可以使用
WARNING: Specifying this field changes how Ops Manager registers | |||||||
| 集合 | Optional | 配置 ServiceSpec。 当您设置 有关 Kubernetes 规范的更多信息,请参阅 Kubernetes 文档中的 ServiceSpec。 | |||||||
| 集合 | 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. 您可以指定占位符值来自定义注释。要了解更多信息,请参阅 |
为副本集部署添加任何其他可接受的设置。
您还可以将以下任何可选设置添加到 对象 副本集 部署的规范文件:
警告
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.
Save this replica set config file with a .yaml extension.
Start your replica set deployment.
在任何目录下,调用以下 Kubernetes 命令以创建副本集:
kubectl apply -f <replica-set-conf>.yaml
追踪副本集部署的状态。
要检查 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 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 -
将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>
Copy the sample replica set resource.
更改此 YAML 文件的设立,以匹配所需的副本集配置。
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: <my-replica-set> 6 spec: 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 ...
Paste the copied example to create a new replica set resource.
打开您的首选文本编辑器并将对象规范粘贴到新的文本文件中。
根据您的偏好更改设置的值。
键 | 类型 | 说明 | 例子 |
|---|---|---|---|
字符串 |
| ||
整型 | 副本集的节点数。 |
| |
字符串 | 此副本集应运行的 MongoDB 版本。 Community 版的格式应为 重要提示:确保选择兼容的MongoDB Server版本。 兼容版本因MongoDB 数据库资源使用的基础映像而异。 要了解有关 MongoDB 版本控制的更多信息,请参阅 MongoDB 手册中的 MongoDB 版本控制。 |
| |
| 字符串 | 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.
Save this replica set config file with a .yaml extension.
Start your replica set deployment.
在任何目录下,调用以下 Kubernetes 命令以创建副本集:
kubectl apply -f <replica-set-conf>.yaml
追踪副本集部署的状态。
要检查 MongoDB 资源的状态,请使用以下命令:
kubectl get mdb <resource-name> -o yaml -w
设置 -w (观看) 标志后,当配置更改时,输出将立即刷新,直到状态阶段达到 Running 状态。要了解有关资源部署状态的详情,请参阅 Kubernetes Operator 故障排除。