您可以使用 Atlas Kubernetes Operator 管理 Atlas 中的资源,而无需离开 Kubernetes。本教程演示如何使用 Atlas Kubernetes Operator 从 Kubernetes 配置文件在 Atlas 中创建第一个集群。
警告
多租户安全指导
切勿在多个租户之间共享单个 Atlas Kubernetes Operator 安装,即使他们使用不同的命名空间。共享操作符的租户可以使用跨命名空间引用来访问其他命名空间中的资源,包括提供对其他租户的 Atlas 配置或其他云资源的访问权限的秘密。要隔离租户,请为每个租户部署单独的 Atlas Kubernetes Operator 实例。
先决条件
本教程要求:
正在运行的 Kubernetes 集群,其节点运行 x86-64、AMD64 或 ARM64 架构的处理器。
jq1.6 或更高版本访问 GitHub 上的Atlas Kubernetes Operator项目:https://github.com/mongodb/mongodb-atlas-kubernetes
步骤
重要
自定义资源不再默认删除对象
Atlas Kubernetes Operator使用自定义资源配置文件来管理Atlas配置,但从Atlas Kubernetes Operator2.0 开始,您在Kubernetes中删除的自定义资源不再(默认)在Atlas中删除。相反, Atlas Kubernetes Operator只是停止管理Atlas中的这些资源。
AtlasProject示例,如果您在Kubernetes中删除 自定义资源,默认默认, Atlas Kubernetes Operator不再自动从Atlas中删除相应的项目。这种行为更改旨在帮助防止意外删除。 要学习;了解详情,包括如何将此行为恢复为在Atlas Kubernetes Operator.2 0之前使用的默认,请参阅新默认值: Atlas Kubernetes2 Operator.0 中的删除保护。同样,如果您使用Atlas Kubernetes Operator从Kubernetes中的Atlas项目中删除团队,则Atlas Kubernetes Operator不会从Atlas中删除团队。
显式定义所需的配置详细信息,以避免隐式使用默认Atlas配置值。 在某些情况下,继承Atlas默认值可能会导致协调循环,从而阻止自定义资源实现
READY状态。 示例,在AtlasDeployment自定义资源中显式定义所需的自动伸缩行为(如所包含的示例所示)可确保自定义资源中的静态实例大小不会重复应用于已启用自动伸缩的Atlas部署。autoScaling: diskGB: enabled: true compute: enabled: true scaleDownEnabled: true minInstanceSize: M30 maxInstanceSize: M40
安装Atlas Kubernetes Operator。
选择以下方法之一来安装Atlas Kubernetes Operator:
要使用Atlas CLI安装Atlas Kubernetes Operator ,运行以下命令:
重要
Atlas CLI 创建 API 密钥,但无法检测集群的出站 IP。使用 --ipAccessList 标志将 Kubernetes 集群的 IP 或 CIDR 块加入允许列表,以便 Atlas Kubernetes Operator 能与 Atlas 通信。
To create the API key, the authenticated user or service account must have Organization Owner access to the organization. Without this access, the command fails with an HTTP 401 Unauthorized error from POST /api/atlas/v2/orgs/<orgId>/apiKeys.
atlas kubernetes operator install --ipAccessList <IP_OR_CIDR> [options]注意
要为 Atlas for 政府 安装 Atlas Kubernetes Operator,将 --atlasGov 标志附加到前面的命令中。
此命令会自动执行以下操作:
在Atlas组织中创建新的API密钥。
将您提供的IP/CIDR 添加到API密钥的IP访问列表。
通过从 GitHub 获取清单,将Atlas Kubernetes Operator安装到Kubernetes集群中。
要了解有关命令语法和参数的更多信息,请参阅Atlas CLI Atlas Kubernetes Operator安装 的 文档。
要使用 kubectl 安装Atlas Kubernetes Operator ,请按照以下步骤操作:
将
<VERSION>替换为最新版本号,然后运行以下命令之一:如果您希望 Atlas Kubernetes Operator 监视 Kubernetes 集群中的所有命名空间,运行:
kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-atlas-kubernetes/refs/heads/main/releases/v<VERSION>/deploy/all-in-one.yaml如果您希望 Atlas Kubernetes Operator 仅监视其命名空间,请安装
deploy/namespaced目录中的配置文件:kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-atlas-kubernetes/v<VERSION>/deploy/namespaced/crds.yamlkubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-atlas-kubernetes/v<VERSION>/deploy/namespaced/namespaced-config.yaml
按照 授予对组织的编程访问权限 中的步骤,创建一个具有 权限的 API
Organization Project Creator密钥。
使用您的 API 密钥和组织 ID 创建密钥。
要创建并标记密钥,请使用您的API密钥和组织ID运行以下命令:
kubectl create secret generic mongodb-atlas-operator-api-key \ --from-literal="orgId=<atlas_organization_id>" \ --from-literal="publicApiKey=<atlas_api_public_key>" \ --from-literal="privateApiKey=<atlas_api_private_key>" \ -n mongodb-atlas-systemkubectl label secret mongodb-atlas-operator-api-key atlas.mongodb.com/type=credentials -n mongodb-atlas-system创建 AtlasProject 自定义资源。
运行以下命令以创建AtlasProject自定义资源:
注意
以下示例未指定spec.connectionSecretRef.name。如果未指定,Atlas Kubernetes Operator 将使用之前使用 API 密钥和组织ID 设立的默认连接 密钥。
部署Atlas Kubernetes Operator自定义资源。
cat <<EOF | kubectl apply -f -apiVersion: atlas.mongodb.com/v1kind: AtlasProjectmetadata: name: my-projectspec: name: Test Atlas Operator Project projectIpAccessList: - ipAddress: <your-ip-address-range> comment: "Adding your IP to Atlas access list"EOF创建 AtlasDeployment 自定义资源。
运行以下命令之一以创建AtlasDeployment自定义资源并创建集群:
付费层级:
cat <<EOF | kubectl apply -f -apiVersion: atlas.mongodb.com/v1kind: AtlasDeploymentmetadata: name: my-atlas-clusterspec: projectRef: name: my-project deploymentSpec: clusterType: REPLICASET name: "Test-cluster" tags: - key: "environment" value: "production" replicationSpecs: - zoneName: US-Zone regionConfigs: - electableSpecs: instanceSize: M10 nodeCount: 3 providerName: AWS regionName: US_EAST_1 priority: 7
EOF免费集群:
cat <<EOF | kubectl apply -f -apiVersion: atlas.mongodb.com/v1kind: AtlasDeploymentmetadata: name: my-atlas-clusterspec: projectRef: name: my-project deploymentSpec: clusterType: REPLICASET name: "Test-cluster" replicationSpecs: - regionConfigs: - regionName: US_EAST_1 providerName: TENANT backingProviderName: AWS electableSpecs: instanceSize: M0 nodeCount: 3EOF创建 AtlasDatabaseUser 自定义资源。
运行以下命令以创建AtlasDatabaseUser自定义资源:
注意
spec.passwordSecretRef 必须引用您之前创建的密码。
cat <<EOF | kubectl apply -f -apiVersion: atlas.mongodb.com/v1kind: AtlasDatabaseUsermetadata: name: my-database-userspec: roles: - roleName: "readWriteAnyDatabase" databaseName: "admin" projectRef: name: my-project username: theuser passwordSecretRef: name: the-user-passwordEOF检查数据库用户的状态。
运行以下命令,直到收到True响应,这表明数据库用户已准备就绪:
注意
AtlasDatabaseUser 自定义资源等待集群准备就绪。创建新的 Free 集群不到 15 秒。创建新的 Flex 或专用集群最多可能需要 10 分钟。
kubectl get atlasdatabaseusers my-database-user -o=jsonpath='{.status.conditions[?(@.type=="Ready")].status}'检索 Atlas Kubernetes Operator 创建的用于连接到集群的密钥。
复制以下命令:
重要
以下命令需要
jq1.6 或更高版本。kubectl get secret {my-project}-{my-atlas-cluster}-{my-database-user} -o json | jq -r '.data | with_entries(.value |= @base64d)';对于自定义资源,用详细信息替换以下占位符:
my-project指定
AtlasProject自定义资源的metadata字段的值。my-atlas-cluster指定
AtlasDeployment自定义资源的metadata字段的值。my-database-user指定
AtlasDatabaseUser自定义资源的metadata字段的值。运行命令。
注意
您的连接字符串将与以下示例不同。
{ "connectionStringStandard": "mongodb://theuser:P%40%40sword%25@test-cluster-shard-00-00.peqtm.mongodb.net:27017,test-cluster-shard-00-01.peqtm.mongodb.net:27017,test-cluster-shard-00-02.peqtm.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-pk82fl-shard-0", "connectionStringStandardSrv": "mongodb+srv://theuser:P%40%40sword%25@test-cluster.peqtm.mongodb.net", "password": "P@@sword%", "username": "theuser" }您可以在应用程序中使用此密钥:
containers: - name: test-app env: - name: "CONNECTION_STRING" valueFrom: secretKeyRef: name: test-atlas-operator-project-test-cluster-theuser key: connectionStringStandardSrv