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

配置密钥存储

您可以为 Kubernetes Operator 选择密钥存储工具。 密钥存储工具是一个安全的位置,用于存储 Kubernetes 操作符托管的组件的敏感信息。这包括 MongoDB 数据库、Ops Manager 和 AppDB 的密钥。

配置密钥存储后,Kubernetes 操作符会访问该工具,检索密钥,并使用它们安全地建立连接。

Kubernetes 操作符 支持以下密钥存储工具:

  • Kubernetes:将敏感信息存储为密钥(Kubernetes的内置密钥存储)。Kubernetes 密钥 存储身份验证凭证,以便只有Kubernetes才能访问权限它们。

  • HashiCorp Vault :将敏感信息存储在第三方密钥管理服务 Vault 中。

您可以将任何受支持的密钥存储工具用于Kubernetes Operator 的MongoDB控制器文档中的任何密钥,但限制中列出的工具除外。

重要

配置后,Kubernetes Operator 将您选择的密钥存储工具用于所有密钥,但限制中列出的密钥除外。 您不能混合和匹配密钥存储工具。

支持的密钥存储工具存在以下限制:

要设置密钥存储工具,请选择以下选项之一:

默认下,此 MongoDB Controllers for Kubernetes Operator 文档中的所有教程都使用 Kubernetes 密钥。要使用Kubernetes 密钥 存储Kubernetes Operator 的密钥,请继续安装Kubernetes Operator 并按照教程中的步骤进行操作。

要使用HashiCorp Vault存储Kubernetes Operator 的密钥,请完成以下步骤。

在开始之前,您必须:

  • 设置 Vault 实例。 运行 Kubernetes Operator 的 Kubernetes 集群必须有权访问 Vault 实例。

    注意

    确保 Vault 没有开发模式下运行,并且您的 Vault 安装遵循所有适用的配置建议。

  • 启用 Kubernetes 身份验证 用于保管库实例。这允许您使用 Vault 进行身份验证。

  • 在Kubernetes集群中部署 Vault Agent sidecar 注入器。这允许您将 Vault 中的密钥注入Kubernetes Pod 中。

  • 下载四个 Vault 策略文件 适用于Kubernetes Operator、MongoDB 数据库、MongoDB Ops Manager 和 AppDB。

  • Create a role in Vault with the name mongodbenterprise. The configuration of secrets in the Kubernetes Operator relies on the presence of this role and its exact name.

1

使用以下命令将Kubernetes Operator、 MongoDB database 、 MongoDB Ops Manager和 AppDB 资源的策略写入 Vault,并将变量替换为表中的值:

占位符
说明

{PolicyName}

人类可读标签,用于标识您在保管库中创建的策略。

{PathToPolicyFile}

您下载的策略文件的绝对路径。

vault policy write {PolicyName} {PathToPolicyFile}

对要添加到保管库的所有资源重复该命令。

2

使用以下四个命令将 Vault 角色绑定到Kubernetes Operator、 MongoDB database 、 MongoDB Ops Manager和 AppDB 资源的策略,并将变量替换为表中的值:

占位符
说明

{OperatorPolicyName}

人类可读标签,用于标识 Vault 中的 Kubernetes Operator 策略。

{DatabasePolicyName}

人类可读标签,用于标识 Vault 中的MongoDB database策略。

{OpsManagerPolicyName}

人类可读标签,用于标识 Vault 中的MongoDB Ops Manager策略。

{AppDBPolicyName}

人类可读标签,用于标识 Vault 中的 AppDB 策略。

{ServiceAccountNamespace}

用于标识绑定到 Pod 的服务帐户的命名空间的标签。

vault write auth/kubernetes/role/{OperatorPolicyName}
bound_service_account_names=enterprise-operator bound_service_account_namespaces={ServiceAccountNamespace}
vault write auth/kubernetes/role/{DatabasePolicyName}
bound_service_account_names=mongodb-kubernetes-database-pods bound_service_account_namespaces={ServiceAccountNamespace}
vault write auth/kubernetes/role/{OpsManagerPolicyName}
bound_service_account_names=mongodb-kubernetes-ops-manager bound_service_account_namespaces={ServiceAccountNamespace}
vault write auth/kubernetes/role/{AppDBPolicyName}
bound_service_account_names=mongodb-kubernetes-appdb bound_service_account_namespaces={ServiceAccountNamespace}

这些命令确保每个组件的 Pod 仅具有其策略中指定的访问权限。

注意

此步骤授予 Kubernetes Operator 对 Vault 的访问权限。 要将 Vault 与 Kubernetes Operator 不托管的应用程序一起使用,您必须为这些应用程序编写并绑定 Vault 策略。

You can adapt the commands in this step to bind other policies by replacing the name of the service accounts. To configure other applications to use Vault, replace the {ServiceAccountName} in the following command with the service account used for the application's pod:

vault write auth/kubernetes/role/{PolicyName}
bound_service_account_names={ServiceAccountName} bound_service_account_namespaces={ServiceAccountNamespace}
3

Before running commands in this step, ensure that you have created a Vault role with the name mongodbenterprise.

将以下突出显示的行添加到Kubernetes Operator部署文件的spec.template.metadata.annotations部分。 对于大多数用户来说,此文件的名称是mongodb-kubernetes.yamlmongodb-kubernetes-openshift.yaml

注意

如果使用 Helm 安装了 Kubernetes Operator 并将Operator.vaultSecretBackend.enabled设置为true ,则 Kubernetes Operator 会添加以下注解。 您可以继续执行下一步。

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-kubernetes-operator
namespace: production
spec:
replicas: 1
template:
metadata:
annotations:
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/role: "mongodbenterprise"

If you're running Vault in TLS mode, and specified the operator.vaultSecretBackend.tlsSecretRef value, the Kubernetes Operator adds the following annotations. Otherwise, add the following highlighted line to the file, replacing {TLSSecret} with the name of the secret containing a ca.crt entry. The content of the ca.crt entry must match the certificate of the CA used to generate the Vault TLS certificates.

annotations:
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/role: "mongodbenterprise"
vault.hashicorp.com/tls-secret: {TLSSecret}
vault.hashicorp.com/ca-cert: /vault/tls/ca.crt
4

将以下突出显示的行添加到Kubernetes Operator部署文件的spec.env部分。 对于大多数用户来说,此文件的名称是mongodb-kubernetes.yamlmongodb-kubernetes-openshift.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-kubernetes-operator
namespace: production
spec:
env:
- name: OPERATOR_ENV
value: ENVIRONMENT_NAME
- name: SECRET_BACKEND
value: VAULT_BACKEND

这定义了Kubernetes中 Vault 的环境变量

5

使用您首选的文本编辑应用程序,创建一个名为config的文件。 将以下文本粘贴到文件中:

apiVersion: v1
kind: ConfigMap
metadata:
name: secret-configuration
namespace: {Namespace}
data:
VAULT_SERVER_ADDRESS: {VaultServerAddress}
OPERATOR_SECRET_BASE_PATH: mongodbenterprise/operator
DATABASE_SECRET_BASE_PATH: mongodbenterprise/database
OPS_MANAGER_SECRET_BASE_PATH: mongodbenterprise/opsmanager
APPDB_SECRET_BASE_PATH: mongodbenterprise/appdb

此文件中的路径是默认路径。 如果您自定义了Kubernetes Operator 配置,则可以将其替换为您的基本路径。

如果您在TLS模式运行保管库,则还必须将以下突出显示的行添加到文件中:

OPS_MANAGER_SECRET_BASE_PATH: mongodbenterprise/opsmanager
APPDB_SECRET_BASE_PATH: mongodbenterprise/appdb
TLS_SECRET_REF: {TLSSecret}
6

Replace the placeholders in the config file with these values. Save the file with a YAML file type by replacing the .txt file extension with .yaml.

占位符
说明

{Namespace}

您为Kubernetes Operator创建的命名空间。 默认命名空间为mongodb

{VaultServerAddress}

Kubernetes Operator 连接到 Vault 时应使用的地址。

{TLSSecret}

包含ca.crt条目的密钥名称。 ca.crt条目的内容必须与用于生成 Vault TLS 证书的CA证书匹配。

7

发出以下命令以创建包含 Vault 信息的ConfigMap

kubectl create configmap secret-configuration --from-file=config.yaml

This creates a ConfigMap named secret-configuration. This ConfigMap contains the contents of the config file.

8

您必须手动迁移以下密钥才能将其存储在保管库中:

要手动迁移或创建新密钥,请将其添加到 Vault 。 将它们添加到 Vault 后,您可以从Kubernetes中删除它们。

Kubernetes Operator 创建的所有其他密钥都会自动迁移,并且Kubernetes Operator 使用 Vault 来保存新密钥。 用户创建的密钥必须添加到 Vault 中。

注意

如果您从Kubernetes中删除生成的Kubernetes密钥,cert-manager 会自动重新创建这些密钥。您必须手动管理这些密钥,或停止使用 cert-manager 以避免将密钥存储在Kubernetes中。

为Kubernetes Operator 的MongoDB控制器配置密钥存储工具后,您可以: