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

使用服务账户配置 Atlas 访问

Atlas API Service Accounts provide a secure, token-based alternative to Programmatic API Keys for authenticating Atlas Kubernetes Operator with the Atlas Administration API. Service Accounts use short-lived access tokens derived from a clientId and clientSecret pair, reducing the risk of long-lived credential exposure.

MongoDB 建议对新部署使用服务账户而不是程序化 API 密钥。服务账户具有以下优点:

  • 由操作符托管的带自动令牌刷新的基于令牌的身份验证。

  • 支持组织级别和项目级别范围。

  • 可审计的独立身份,适用于 Atlas Kubernetes Operator 工作负载。

重要

服务帐户密钥过期

与API密钥不同,服务帐户密钥是可过期的。您可以将TTL最长设立为一年。您必须在服务帐户密钥过期之前进行轮换,以保持访问权限不中断。随着过期日期的临近, Atlas会自动引发“服务帐户密钥即将过期”警报。相应地规划轮换安排。

这适用于组织级和项目级服务账户。

在开始之前,请确保您具备以下内容:

  • 具有组织级服务账户的 Organization Owner 角色或项目级服务账户的 Project Owner 角色的 Atlas 账户。

  • 安装了 Atlas Kubernetes Operator 的运行中 Kubernetes 集群。

  • kubectl 访问运行 Atlas Kubernetes Operator 的 Kubernetes 集群。

1

Create a Service Account in Atlas using the Atlas UI or the Atlas Administration API. To learn how to create an organization-level Service Account, see Create a Service Account in an Organization. To learn how to create a project-level Service Account, see Create a Service Account in a Project.

复制clientId和clientSecret。安全存储它们,因为创建后您无法再次检索clientSecret。

2

创建包含 orgId、clientId 和 clientSecret的 Kubernetes secret。

  • 对于 global Atlas Kubernetes Operator 密钥,运行以下命令:

    注意

    global Atlas Kubernetes Operator密钥的名称必须符合预定义的格式。Atlas Kubernetes Operator部署的默认名称是 mongodb-atlas-operator。因此,密钥 应命名为 mongodb-atlas-operator-service-account。

    kubectl create secret generic mongodb-atlas-operator-service-account \
    --from-literal="orgId=<the_atlas_organization_id>" \
    --from-literal="clientId=<the_service_account_client_id>" \
    --from-literal="clientSecret=<the_service_account_client_secret>" \
    -n <operator_namespace>
    kubectl label secret mongodb-atlas-operator-service-account \
    atlas.mongodb.com/type=credentials \
    -n mongodb-atlas-system
  • 对于 project Atlas Kubernetes Operator 密钥,运行以下命令:

    kubectl create secret generic my-project-connection \
    --from-literal="orgId=<the_atlas_organization_id>" \
    --from-literal="clientId=<the_service_account_client_id>" \
    --from-literal="clientSecret=<the_service_account_client_secret>" \
    -n <atlas_project_namespace>
    kubectl label secret my-project-connection \
    atlas.mongodb.com/type=credentials \
    -n <atlas_project_namespace>
3

使用 spec.connectionSecretRef.name 从 自定义资源参考 秘密 。AtlasProject

如果您创建了 global 秘密,则所有项目都会默认使用它,无需进行额外配置。

如果创建了 project 秘密,请将 connectionSecretRef 添加到 AtlasProject 自定义资源中:

apiVersion: atlas.mongodb.com/v1
kind: AtlasProject
metadata:
name: my-project
spec:
name: "My Project"
connectionSecretRef:
name: my-project-connection
4

配置 秘密 后,验证 Atlas Kubernetes Operator 是否成功认证:

kubectl get atlasproject my-project -o jsonpath='{.status.conditions}'

检查 Ready 条件是否为 True。如果 Atlas Kubernetes Operator 无法使用 Service Account 凭证进行身份验证,条件中将显示描述该问题的错误。

以下示例展示为服务账户配置的连接秘密:

apiVersion: v1
kind: Secret
metadata:
name: my-project-connection
namespace: atlas-operator
labels:
atlas.mongodb.com/type: credentials
type: Opaque
stringData:
orgId: "5d39f0719ccf646554345678"
clientId: "mdb_sa_id_67890abcdef1234567890abc"
clientSecret: "mdb_sa_sk_abcdef1234567890abcdef12"

Atlas Kubernetes Operator 自动托管访问令牌。当 Atlas Kubernetes Operator 检测到 Service Account Connection Secret 时,它会从 Atlas Administration API 获取访问令牌,并将其存储在单独的 Kubernetes secret 中。请勿手动编辑或删除 Access Token Secret。Atlas Kubernetes Operator 会根据需要刷新令牌。

在 Atlas 中轮换服务账户秘密时:

  1. 使用新的 clientSecret 更新 Kubernetes 连接秘密。

  2. Atlas Kubernetes Operator 会检测到变更,并在下一个协调周期中获取新的访问令牌。无需重启。

重要

服务帐户密钥的最长TTL为一年。在Atlas中监控服务帐户密钥即将过期警报,并在密钥过期前轮换密钥,以避免身份验证失败。这适用于组织级别和项目级别的服务帐户。

The Service Account's API access list must include the egress IP address or CIDR block used by the Atlas Kubernetes Operator pod, the same as for Programmatic API Keys. If the operator pod's egress IP is not on the Service Account access list, API requests fail.