Docs 菜单
Docs 主页
/ /
MongoDB Atlas Kubernetes Operator
/

快速入门

您可以使用 Atlas Kubernetes Operator 管理 Atlas 中的资源,而无需离开 Kubernetes。本教程演示如何使用 Atlas Kubernetes Operator 从 Kubernetes 配置文件在 Atlas 中创建第一个集群。

注意

您想从 Helm 开始吗?

要使用Atlas Kubernetes Operator从 Helm Charts在Atlas中创建第一个集群,请参阅 Helm Charts快速入门。

本教程要求:

  • 正在运行的 Kubernetes 集群,其节点运行 x86-64、AMD64 或 ARM64 架构的处理器。

  • jq 1.6 或更高版本

您可以访问 GitHub 上的 Atlas Kubernetes Operator 项目:

  • https://github.com/mongodb/mongodb-atlas-kubernetes

要使用 Atlas CLI 安装 Atlas Kubernetes Operator, 请运行以下命令:

atlas kubernetes operator install [options]

要了解有关命令语法和参数的更多信息,请参阅Atlas CLI Atlas Kubernetes Operator安装 的 文档。

提示

请参阅:相关链接

重要

自定义资源不再默认删除对象

Atlas Kubernetes Operator 使用自定义资源配置文件来管理 Atlas 配置,但从 Atlas Kubernetes Operator 2.0 开始,在 Kubernetes 中删除的自定义资源将不再从 Atlas 中删除。相反,Atlas Kubernetes Operator 只是不再管理这些资源。例如,如果您在 Kubernetes 中删除 AtlasProject 自定义资源,Atlas Kubernetes Operator 不会再自动从 Atlas 中删除相应项目,从而防止意外删除。要了解更多信息,包括如何将此行为恢复为 Atlas Kubernetes Operator 2.0 之前使用的默认行为,请参阅新默认值:Atlas Kubernetes Operator 2.0 中的删除保护

1

注册新的 Atlas 帐户登录您的 Atlas 帐户。

2

注意

您需要以下公共 API 密钥、私有 API 密钥和组织 ID 信息, 配置 Atlas Kubernetes Operator 对 Atlas 的访问权限。

向组织授予编程访问权限并配置API Access List

您需要以下公共 API 密钥、私有 API 密钥和组织 ID 信息, 配置 Atlas Kubernetes Operator 对 Atlas 的访问权限。

3

在以下场景之一,请将<version>替换为最新版本号:

  • 如果您希望 Atlas Kubernetes Operator 监视 Kubernetes 集群中的所有命名空间,请运行以下命令:

    kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-atlas-kubernetes/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.yaml
    kubectl apply -f https://raw.githubusercontent.com/mongodb/mongodb-atlas-kubernetes/v<version>/deploy/namespaced/namespaced-config.yaml
4

要创建并标记密钥,请使用您的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-system
kubectl label secret mongodb-atlas-operator-api-key atlas.mongodb.com/type=credentials -n mongodb-atlas-system

如果使用外部密钥存储,则无需将敏感信息直接放入Kubernetes 密钥中。要学习;了解更多信息,请参阅配置密钥存储。

5

运行以下命令以创建AtlasProject自定义资源:

注意

以下示例未指定 spec.connectionSecretRef.name 。如果未指定,Atlas Kubernetes Operator 将使用之前使用 API 密钥和组织 ID 设立的默认连接密钥

部署Atlas Kubernetes Operator自定义资源。

cat <<EOF | kubectl apply -f -
apiVersion: atlas.mongodb.com/v1
kind: AtlasProject
metadata:
name: my-project
spec:
name: Test Atlas Operator Project
projectIpAccessList:
- ipAddress: <your-ip-address-range>
comment: "Adding your IP to Atlas access list"
EOF
6

运行以下命令之一以创建AtlasDeployment自定义资源并创建集群:

付费层级:

cat <<EOF | kubectl apply -f -
apiVersion: atlas.mongodb.com/v1
kind: AtlasDeployment
metadata:
name: my-atlas-cluster
spec:
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/v1
kind: AtlasDeployment
metadata:
name: my-atlas-cluster
spec:
projectRef:
name: my-project
deploymentSpec:
clusterType: REPLICASET
name: "Test-cluster"
replicationSpecs:
- regionConfigs:
- regionName: US_EAST_1
providerName: TENANT
backingProviderName: AWS
electableSpecs:
instanceSize: M0
nodeCount: 3
EOF

要创建无服务器实例,请参阅无服务器实例示例

7

P@@ssword%替换为您的密码,然后运行以下命令:

kubectl create secret generic the-user-password --from-literal="password=P@@sword%"
kubectl label secret the-user-password atlas.mongodb.com/type=credentials

如果使用外部密钥存储,则无需将敏感信息直接放入Kubernetes 密钥中。要学习;了解更多信息,请参阅配置密钥存储。

8

运行以下命令以创建AtlasDatabaseUser自定义资源:

注意

spec.passwordSecretRef 必须引用您以前创建的密码。

cat <<EOF | kubectl apply -f -
apiVersion: atlas.mongodb.com/v1
kind: AtlasDatabaseUser
metadata:
name: my-database-user
spec:
roles:
- roleName: "readWriteAnyDatabase"
databaseName: "admin"
projectRef:
name: my-project
username: theuser
passwordSecretRef:
name: the-user-password
EOF
9

运行以下命令,直到收到True响应,这表明数据库用户已准备就绪:

注意

AtlasDatabaseUser自定义资源等待集群准备就绪。 创建新集群最多可能需要 10 分钟。

kubectl get atlasdatabaseusers my-database-user -o=jsonpath='{.status.conditions[?(@.type=="Ready")].status}'
10
  1. 复制以下命令:

    重要

    以下命令需要 jq 1.6 或更高版本。

    kubectl get secret {my-project}-{my-atlas-cluster}-{my-database-user} -o json | jq -r '.data | with_entries(.value |= @base64d)';
  2. 对于自定义资源,用详细信息替换以下占位符:

    my-project

    指定AtlasProject自定义资源metadata字段的值。

    my-atlas-cluster

    指定AtlasDeployment自定义资源metadata字段的值。

    my-database-user

    指定AtlasDatabaseUser自定义资源metadata字段的值。

  3. 运行命令。

    注意

    您的连接字符串将与以下示例不同。

    {
    "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

后退

开始体验