您可以使用 Atlas Kubernetes Operator 管理 Atlas 中的资源,而无需离开 Kubernetes。本教程演示如何在 Atlas 中使用 Atlas Kubernetes Operator 从 Helm Charts 创建第一个集群。
先决条件
本教程要求:
- 正在运行的 Kubernetes 集群,其节点运行 x86-64、AMD64 或 ARM64 架构的处理器。
您可以访问 GitHub 上的 Atlas Kubernetes Operator 项目:
步骤
重要
自定义资源不再默认删除对象
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 帐户或登录。
Register and Log in to Atlas or Register and Log in to Atlas.
为您的组织创建 API 密钥。
注意
您需要以下公共 API 密钥、私有 API 密钥和组织 ID 信息, 配置 Atlas Kubernetes Operator 对 Atlas 的访问权限。
Grant Programmatic Access to an Organization and configure the API Access List.
您需要以下公共 API 密钥、私有 API 密钥和组织 ID 信息, 配置 Atlas Kubernetes Operator 对 Atlas 的访问权限。
If you want Atlas Kubernetes Operator to create a new Atlas project, Grant Programmatic Access to an Organization. If your organization requires an IP access list for the Atlas Administration API, you must also configure the API access list.
重要
您必须为 API 密钥分配 Organization Project Creator 或更高级别的组织角色。
If you want to work with an existing Atlas project, Add Project Access from a Project. If your organization requires an IP access list for the Atlas Administration API, you must also configure the API access list.
重要
您必须为 API 密钥分配 Project Owner 项目角色。
部署 Atlas Kubernetes Operator。
运行以下其中一组命令:
如果您希望 Atlas Kubernetes Operator 监视 Kubernetes 集群中的所有命名空间,请运行以下命令:
helm repo add mongodb https://mongodb.github.io/helm-charts helm install atlas-operator --namespace=atlas-operator --create-namespace mongodb/mongodb-atlas-operator
部署 Atlas 数据库部署。
自定义 Atlas 项目及其数据库用户。
注意
您可以在Github中找到以下其他 HelmCharts GithubCharts示例:
atlas-basic:一个简单的Atlas Kubernetes Operator部署示例,包括创建Atlas项目、集群和数据库用户。
atlas-advanced:可扩展的高级 Atlas Kubernetes Operator 示例部署,并带有与多区域部署、自动扩展行为等相关的模板。
创建名为
install-values.yaml的文件并粘贴以下示例代码,该代码将执行以下操作:将项目名称设置为
My Project。允许所有 IP 地址 (0.0.0.0) 访问项目。
Creates a database user named
dbadminthat has thedbAdminrole.Creates a database user named
dbuserthat has thereadWriterole.
project: # Project custom values atlasProjectName: "My Project" projectIpAccessList: - cidrBlock: "0.0.0.0/0" users: # Custom database users - username: dbadmin databaseName: admin roles: - databaseName: admin-role roleName: dbAdmin - username: dbuser databaseName: admin roles: - databaseName: user-role roleName: readWrite 运行以下命令。
以下命令中的
--set和--values标志使用您的组织 ID、API 密钥和 Atlas 项目配置覆盖Values.yaml文件值和默认 Helm Charts 值。注意
mongodb/atlas-deployment引用存储库中图表的名称。helm install atlas-deployment \ mongodb/atlas-deployment \ --namespace=my-cluster \ --create-namespace \ --set atlas.secret.orgId='<orgid>' \ --set atlas.secret.publicApiKey='<publicKey>' \ --set atlas.secret.privateApiKey='<privateApiKey>' \ --values install-values.yaml 要了解可用参数的更多信息,请参阅
AtlasDeployment自定义资源。
检查数据库用户的状态。
运行以下命令,等待 dbadmin 数据库用户准备就绪:
kubectl wait --for=condition=ready --timeout=10m -n my-cluster atlasdatabaseusers/atlas-deployment-dbadmin
注意
AtlasDatabaseUser 自定义资源等待集群准备就绪。创建新的 Free 集群不到 15 秒。创建新的 Flex 或专用集群最多可能需要 10 分钟。
检索 Atlas Kubernetes Operator 创建的用于连接到数据库部署的密钥。
重要
以下命令需要 jq 1.6 或更高版本。
运行以下命令检索 dbadmin 数据库用户的连接字符串和密码。您的连接字符串将与示例输出不同。
kubectl get secret -n my-cluster my-project-cluster-name-dbadmin -o json | jq -r '.data | with_entries(.value |= @base64d)';
{ "connectionStringStandard": "mongodb://admin-user:%25SomeLong%25password$foradmin@atlas-cluster-shard-00-00.nlrvs.mongodb.net:27017,atlas-cluster-shard-00-01.nlrvs.mongodb.net:27017,atlas-cluster-shard-00-02.nlrvs.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-11q9bn-shard-0", "connectionStringStandardSrv": "mongodb+srv://admin-user:%25SomeLong%25password$foradmin@atlas-cluster.nlrvs.mongodb.net", "password": "%SomeLong%password$foradmin", "username": "dbadmin" }
您可以在应用程序中使用以下密钥:
containers: - name: test-app env: - name: "CONNECTION_STRING" valueFrom: secretKeyRef: name: my-project-cluster-name-dbadmin key: connectionStringStandardSrv