您可以为 Kubernetes Operator 选择密钥存储工具。 密钥存储工具是一个安全的位置,用于存储 Kubernetes 操作符托管的组件的敏感信息。这包括 MongoDB 数据库、Ops Manager 和 AppDB 的密钥。
配置密钥存储后,Kubernetes 操作符会访问该工具,检索密钥,并使用它们安全地建立连接。
支持的密钥存储工具
Kubernetes 操作符 支持以下密钥存储工具:
HashiCorp Vault :将敏感信息存储在第三方密钥管理服务 Vault 中。
您可以存储的密钥
您可以将任何受支持的密钥存储工具用于Kubernetes Operator 的MongoDB控制器文档中的任何密钥,但限制中列出的工具除外。
重要
配置后,Kubernetes Operator 将您选择的密钥存储工具用于所有密钥,但限制中列出的密钥除外。 您不能混合和匹配密钥存储工具。
限制
支持的密钥存储工具存在以下限制:
- 某些注册表(例如 OpenShift)需要使用 imagePullSecrets 从存储库中提取映像。Kubernetes Operator 无法提供来自HashiCorp Vault 的 imagePullSecrets。您可以指定 kubelet 映像凭证提供商来检索使用Kubernetes 的容器映像注册表的凭证。
设置密钥存储工具
要设置密钥存储工具,请选择以下选项之一:
要使用HashiCorp Vault存储Kubernetes Operator 的密钥,请完成以下步骤。
先决条件
在开始之前,您必须:
启用 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.
步骤
将 Vault 角色绑定到 Kubernetes Operator 及其组件的 Vault 策略。
使用以下四个命令将 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}
将注释添加到Kubernetes部署文件中。
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.yaml或mongodb-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
在Kubernetes中定义环境变量。
将以下突出显示的行添加到Kubernetes Operator部署文件的spec.env部分。 对于大多数用户来说,此文件的名称是mongodb-kubernetes.yaml或mongodb-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
创建一个包含 Vault 配置信息的文件。
使用您首选的文本编辑应用程序,创建一个名为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}
替换 Vault 配置信息中的占位符。
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创建的命名空间。 默认命名空间为 |
{VaultServerAddress} | Kubernetes Operator 连接到 Vault 时应使用的地址。 |
{TLSSecret} | 包含 |
手动迁移不会自动迁移的密钥
您必须手动迁移以下密钥才能将其存储在保管库中:
任何现有的用户创建的密钥,包括存储为Kubernetes密钥的 Operator凭证(如果适用)
Kubernetes Operator 创建的 gen-key 密钥
MongoDB Ops Manager管理员档案/管理员密钥Kubernetes Operator 创建
TLS 密钥
要手动迁移或创建新密钥,请将其添加到 Vault 。 将它们添加到 Vault 后,您可以从Kubernetes中删除它们。
Kubernetes Operator 创建的所有其他密钥都会自动迁移,并且Kubernetes Operator 使用 Vault 来保存新密钥。 用户创建的密钥必须添加到 Vault 中。
后续步骤
为Kubernetes Operator 的MongoDB控制器配置密钥存储工具后,您可以: