你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本快速入门介绍如何使用 Terraform 通过性能路由方法创建具有外部终结点的流量管理器配置文件。
Terraform 支持云基础结构的定义、预览和部署。 使用 Terraform,可以使用 HCL 语法创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。
在这篇文章中,你将学会如何:
- 使用 random_pet为 Azure 资源组名称创建随机值。
- 使用 azurerm_resource_group 创建 Azure 资源组。
- 使用 random_string为 Azure 流量管理器配置文件名称创建随机值。
- 使用 random_string 为 Azure 流量管理器配置文件 DNS 配置相对名称创建随机值。
- 使用 azurerm_traffic_manager_profile创建 Azure 流量管理器配置文件。
- 使用 azurerm_traffic_manager_external_endpoint 创建两个 Azure 流量管理器外部终结点。
先决条件
实现 Terraform 代码
注释
本文的示例代码位于 Azure Terraform GitHub 存储库中。 可以查看包含 当前版本和早期版本的 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 "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { name = random_pet.rg_name.id location = var.resource_group_location } resource "random_string" "azurerm_traffic_manager_profile_name" { length = 25 upper = false numeric = false special = false } resource "random_string" "azurerm_traffic_manager_profile_dns_config_relative_name" { length = 10 upper = false numeric = false special = false } resource "azurerm_traffic_manager_profile" "profile" { name = random_string.azurerm_traffic_manager_profile_name.result resource_group_name = azurerm_resource_group.rg.name traffic_routing_method = "Performance" dns_config { relative_name = random_string.azurerm_traffic_manager_profile_dns_config_relative_name.result ttl = 30 } monitor_config { protocol = "HTTPS" port = 443 path = "/" expected_status_code_ranges = ["200-202", "301-302"] } } resource "azurerm_traffic_manager_external_endpoint" "endpoint1" { profile_id = azurerm_traffic_manager_profile.profile.id name = "endpoint1" target = "www.contoso.com" endpoint_location = "eastus" weight = 50 } resource "azurerm_traffic_manager_external_endpoint" "endpoint2" { profile_id = azurerm_traffic_manager_profile.profile.id name = "endpoint2" target = "www.fabrikam.com" endpoint_location = "westus" weight = 50 }创建名为
variables.tf的文件并插入下列代码:variable "resource_group_location" { type = string description = "Location for all resources." default = "eastus" } variable "resource_group_name_prefix" { type = string description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." default = "rg" }创建名为
outputs.tf的文件并插入下列代码:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "azurerm_traffic_manager_profile_name" { value = azurerm_traffic_manager_profile.profile.name } output "azurerm_traffic_manager_profile_fqdn" { value = azurerm_traffic_manager_profile.profile.fqdn }
初始化 Terraform
运行 terraform init 以初始化 Terraform 部署。 此命令将下载管理 Azure 资源所需的 Azure 提供程序。
terraform init -upgrade
要点:
- 参数
-upgrade可将必要的提供程序插件升级到符合配置版本约束的最新版本。
创建 Terraform 执行计划
运行 terraform 计划 以创建执行计划。
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。
验证结果
获取 Azure 资源组名称。
resource_group_name=$(terraform output -raw resource_group_name)获取流量管理器配置文件名称。
traffic_manager_profile_name=$(terraform output -raw azurerm_traffic_manager_profile_name)运行 az network traffic-manager profile show 来显示有关新流量管理器配置文件的信息。
az network traffic-manager profile show \ --resource-group $resource_group_name \ --name $traffic_manager_profile_name
清理资源
不再需要通过 Terraform 创建的资源时,请执行以下步骤:
运行 terraform 计划 并指定
destroy标志。terraform plan -destroy -out main.destroy.tfplan要点:
-
terraform plan命令将创建一个执行计划,但不会执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。 - 使用可选
-out参数可以为计划指定输出文件。 使用-out参数可以确保所查看的计划与所应用的计划完全一致。
-
运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan
Azure 上的 Terraform 故障排除
排查在 Azure 上使用 Terraform 时的常见问题