快速入门:将 Azure Cosmos DB 部署到 Azure 容器实例
文章使用以下 Terraform 和 Terraform 提供程序版本进行测试:
使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。
本文介绍如何使用 Terraform 将 Azure Cosmos DB 部署到Azure 容器实例。
在本文中,学习如何:
- 创建 Azure Cosmos DB 实例
- 创建 Azure 容器实例
- 创建适用于这两种资源的应用
注意
本文中的示例代码位于 Microsoft Terraform GitHub 存储库中。
先决条件
- Azure 订阅:如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
配置 Terraform:如果尚未执行此操作,请使用以下选项之一配置 Terraform:
实现 Terraform 代码
创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
创建名为
providers.tf
的文件并插入下列代码:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
创建名为
main.tf
的文件并插入下列代码:resource "azurerm_resource_group" "rg" { name = "${random_pet.rg_name.id}-rg" location = var.resource_group_location } resource "azurerm_cosmosdb_account" "vote_cosmos_db" { name = "${random_pet.rg_name.id}-${random_integer.ri.result}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name offer_type = "Standard" kind = "GlobalDocumentDB" consistency_policy { consistency_level = "BoundedStaleness" max_interval_in_seconds = 10 max_staleness_prefix = 200 } geo_location { location = azurerm_resource_group.rg.location failover_priority = 0 } } resource "random_integer" "ri" { min = 10000 max = 99999 } resource "random_pet" "rg_name" { prefix = var.prefix }
创建名为
aci.tf
的文件并插入下列代码:resource "azurerm_container_group" "main" { name = "${random_pet.rg_name.id}-vote-aci" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_address_type = "Public" dns_name_label = "vote-aci-${random_integer.ri.result}" os_type = "Linux" container { name = "vote-aci" image = "mcr.microsoft.com/azuredocs/azure-vote-front:cosmosdb" cpu = "0.5" memory = "1.5" ports { port = 80 protocol = "TCP" } secure_environment_variables = { "COSMOS_DB_ENDPOINT" = azurerm_cosmosdb_account.vote_cosmos_db.endpoint "COSMOS_DB_MASTERKEY" = azurerm_cosmosdb_account.vote_cosmos_db.primary_key "TITLE" = "Azure Voting App" "VOTE1VALUE" = "Cats" "VOTE2VALUE" = "Dogs" } } }
创建名为
variables.tf
的文件并插入下列代码:variable "resource_group_location" { default = "eastus" description = "Location of the resource group." } variable "prefix" { type = string default = "cosmos-db-aci" description = "Prefix of the resource name" }
创建名为
outputs.tf
的文件并插入下列代码:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "cosmosdb_account_name" { value = azurerm_cosmosdb_account.vote_cosmos_db.name } output "dns" { value = azurerm_container_group.main.fqdn }
初始化 Terraform
运行 terraform init,将 Terraform 部署进行初始化。 此命令将下载管理 Azure 资源所需的 Azure 提供程序。
terraform init -upgrade
要点:
- 参数
-upgrade
可将必要的提供程序插件升级到符合配置版本约束的最新版本。
创建 Terraform 执行计划
运行 terraform plan 以创建执行计划。
terraform plan -out main.tfplan
要点:
terraform plan
命令将创建一个执行计划,但不会执行它。 它会确定创建配置文件中指定的配置需要执行哪些操作。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。- 使用可选
-out
参数可以为计划指定输出文件。 使用-out
参数可以确保所查看的计划与所应用的计划完全一致。
应用 Terraform 执行计划
运行 terraform apply,将执行计划应用到云基础结构。
terraform apply main.tfplan
要点:
- 示例
terraform apply
命令假设你先前运行了terraform plan -out main.tfplan
。 - 如果为
-out
参数指定了不同的文件名,请在对terraform apply
的调用中使用该相同文件名。 - 如果未使用
-out
参数,请调用不带任何参数的terraform apply
。
验证结果
获取资源组名称。
echo "$(terraform output resource_group_name)"
获取 Azure Cosmos DB 帐户名称。
echo "$(terraform output cosmosdb_account_name)"
运行 az cosmosdb sql database list/
az cosmosdb sql database list \ --resource-group <resource_group_name> \ --account-name <cosmosdb_account_name>
测试应用程序
获取 Azure Cosmos DB 帐户名称。
echo "$(terraform output dns)"
浏览到上一步中指示的 URL。 应看到结果类似于以下输出:
清理资源
不再需要通过 Terraform 创建的资源时,请执行以下步骤:
运行 terraform plan 并指定
destroy
标志。terraform plan -destroy -out main.destroy.tfplan
要点:
terraform plan
命令将创建一个执行计划,但不会执行它。 它会确定创建配置文件中指定的配置需要执行哪些操作。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。- 使用可选
-out
参数可以为计划指定输出文件。 使用-out
参数可以确保所查看的计划与所应用的计划完全一致。
运行 terraform apply 以应用执行计划。
terraform apply main.destroy.tfplan
Azure 上的 Terraform 故障排除
排查在 Azure 上使用 Terraform 时遇到的常见问题
后续步骤
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈