您可以使用 Terraform 通过Atlas Terraform提供商以代码形式管理MongoDB Atlas基础架构。 Atlas Terraform提供商通过简化以代码形式预配、管理和控制Atlas基础架构的进程,实现基础架构部署的自动化。
本指南将向您介绍如何在现有MongoDB Atlas组织中创建和销毁Atlas集群。
配置好测试环境后,请继续使用来自MongoDB和 HashiCorp 的额外资源对 Terraform 托管的基础架构进行试验。
Atlas Infinite 集群支持Atlas Terraform提供商。要学习;了解有关Atlas Infinite 的更多信息,请参阅MongoDB Atlas Infinite:概述。
先决条件
在使用 Terraform 部署MongoDB Atlas之前,您必须:
创建Atlas帐户。
获取您作为 的 组织ID
Organization Owner。提示
您可以在 Atlas 用户界面中组织的 General Settings 下找到您的组织 ID。
配置服务帐户 (SA) 身份验证。需要“组织所有者”角色才能查看“组织ID”、管理服务帐户以及创建或删除集群和其他项目资源。
安装 Terraform。
使用 Terraform 部署MongoDB Atlas
填充支持文件。
将以下内容添加到 provider.tf:
provider "mongodbatlas" { client_id = var.client_id client_secret = var.client_secret }
将以下内容添加到 variables.tf:
variable "client_id" { description = "Atlas Service Account client ID" type = string } variable "client_secret" { description = "Atlas Service Account client secret" type = string sensitive = true } variable "org_id" { description = "Atlas organization ID" type = string }
将以下内容添加到 versions.tf:
terraform { required_providers { mongodbatlas = { source = "mongodb/mongodbatlas" version = "~> 2.2" } } required_version = "~> 1.0" }
配置 main.tf 内容。
注意
以下示例使用项目模块创建项目,并使用 mongodbatlas_advanced_cluster资源创建免费集群。
将以下内容添加到 main.tf文件中:
module "project" { source = "terraform-mongodbatlas-modules/project/mongodbatlas" name = "tenant-example-project" org_id = var.org_id } resource "mongodbatlas_advanced_cluster" "this" { project_id = module.project.id name = "TenantCluster" cluster_type = "REPLICASET" replication_specs = [ { region_configs = [ { provider_name = "TENANT" backing_provider_name = "AWS" region_name = "US_EAST_1" priority = 7 electable_specs = { instance_size = "M0" } } ] } ] }
如果您与不同的云提供商或地区合作,则可以更新以下字段以匹配您的环境。
字段 | 新值 |
|---|---|
| 您的提供商。 可能的值为: |
| 有关您可以使用的所有区域,请参阅云提供商和区域。 |
重要
创建专用集群时,可以在 mongodbatlas_advanced_cluster资源上设立database_edition。您无法事后更改它,也无法将专用集群扩展回免费或灵活集群。 database_edition 不可用于免费集群(例如本示例中的 M0集群)和 Flex 集群。
mongodbatlas_advanced_cluster资源以及单数 (mongodbatlas_advanced_cluster) 和复数 (mongodbatlas_advanced_clusters) 数据源都会公开 database_edition 和只读 effective_database_edition 属性。由于 database_edition 是可选的,因此即使您没有自己设立,也请使用 effective_database_edition 来确认Atlas分配的版本。
要在Atlas Infinite 上创建专用集群,请将 database_edition设立为 "INFINITE",并在 auto_scaling 中设立compute_enabled,如以下示例所示:
resource "mongodbatlas_advanced_cluster" "infinite" { project_id = module.project.id name = "InfiniteCluster" cluster_type = "REPLICASET" database_edition = "INFINITE" replication_specs = [ { region_configs = [ { provider_name = "AWS" priority = 7 region_name = "US_EAST_1" electable_specs = { instance_size = "M10" node_count = 2 } auto_scaling = { compute_enabled = true compute_max_instance_size = "M60_GEN_2" storage_config = { shard_size_limit_gb = 10240 } } } ] } ] }
专用Atlas无限集群仅支持REPLICASET集群类型。
compute_max_instance_size 当 compute_enabled 为 true 时为必填项。 M30+ Atlas无限集群仅在 AWS Gen2 上可用,因此将 _GEN_2 附加到层级名称,如 M60_GEN_2 所示。要学习;了解更多信息,请参阅 Gen2 专用集群。
Atlas Infinite集群有两个可选计算节点,即主节点 (primary node in the replica set)和备用节点,因此将 node_count设立为 2。要学习;了解更多信息,请参阅复制和故障转移。
要提供服务更多读取或将分析查询与操作工作负载隔离,请通过在同一 region_configs对象中设置 read_only_specs 或 analytics_specs 以及 electable_specs 来添加只读节点或分析节点。要学习;了解更多信息,请参阅Atlas无限集群上的工作负载隔离性。
storage_config.shard_size_limit_gb 仅当包含 storage_config 时才为必填项。如果省略 storage_config, Atlas将应用默认的最大存储限制。要学习;了解更多信息,请参阅最大存储限制。
注意
storage_config.shard_size_limit_gb 设置集群可以存储的最大数据量。要学习;了解更多信息,请参阅 Terraform提供商文档。
验证部署
terraform apply 完成后,确认集群已部署。
新集群会出现在项目下的 Atlas 用户界面中。
如果已将输出值添加到
main.tf,则连接字符串会出现在 Terraform 输出中。
终止MongoDB Atlas实例
要删除在 Terraform目录中创建的所有资源,请运行以下命令:
terraform destroy
警告
如果删除了所有资源,则无法恢复。
当出现Do you really want to destroy all resources?提示时,输入yes 。
后续步骤
部署集群后,您可以:
扩展到专用集群:查看集群模块和Atlas Terraform Provider 文档以获取更完整的配置。
连接到集群:使用集群对象或 Terraform 输出值中的连接字符串。
其他资源: