适用于:✔️ Windows VM
本文介绍如何使用 Terraform 在 Azure 中创建 Windows VM 群集(包含三个 Windows VM 实例)。
- 使用 random_pet为 Azure 资源组名称创建随机值。
- 使用 azurerm_resource_group 创建 Azure 资源组。
- 为 Windows VM 主机名 random_string创建随机值。
- 使用 random_password 为 Windows VM 创建随机密码。
- 使用 计算模块创建 Windows VM。
- 使用 网络模块创建虚拟网络和子网。
先决条件
实现 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" "windows_server_vm_hostname" {
length = 8
lower = true
upper = false
special = false
}
resource "random_pet" "windows_server_public_ip_dns" {
prefix = "dns"
}
resource "random_password" "password" {
length = 16
special = true
lower = true
upper = true
numeric = true
}
# The following module is a Terraform Verified Module.
# For more information about Verified Modules, see
# https://github.com/azure/terraform-azure-modules/
module "windows_server" {
count = 3 # Define 3 Windows Server VMs
source = "Azure/compute/azurerm"
resource_group_name = azurerm_resource_group.rg.name
vnet_subnet_id = module.network.vnet_subnets[0]
is_windows_image = true
vm_hostname = "vm-${random_string.windows_server_vm_hostname.result}-${count.index}"
delete_os_disk_on_termination = true
admin_password = random_password.password.result
vm_os_simple = "WindowsServer"
public_ip_dns = ["${random_pet.windows_server_public_ip_dns.id}-${count.index}"]
}
# The following module is a Terraform Verified Module.
# For more information about Verified Modules, see
# https://github.com/azure/terraform-azure-modules/
module "network" {
source = "Azure/network/azurerm"
resource_group_name = azurerm_resource_group.rg.name
version = "5.2.0"
subnet_prefixes = ["10.0.1.0/24"]
subnet_names = ["subnet1"]
use_for_each = true
}
- 创建名为
variables.tf的文件并插入下列代码:
variable "resource_group_location" {
type = string
default = "chinanorth2"
description = "Location for all resources."
}
variable "resource_group_name_prefix" {
type = string
default = "rg"
description = "Prefix of the resource group name that's combined with a random value so name is unique in your Azure subscription."
}
- 创建名为
outputs.tf的文件并插入下列代码:
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}
output "windows_vm_public_names" {
value = module.windows_server[*].public_ip_dns_name
}
output "vm_public_ip_addresses" {
value = module.windows_server[*].public_ip_address
}
output "vm_private_ip_addresses" {
value = module.windows_server[*].network_interface_private_ip
}
output "vm_hostnames" {
value = module.windows_server[*].vm_names
}
初始化 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。
在 Terraform 的虚拟机创建过程中不会像在 Azure 门户中那样显示成本信息。 若要详细了解虚拟机的成本工作原理,请参阅 “成本优化概述”页。
验证结果
获取 Azure 资源组名称。
resource_group_name=$(terraform output -raw resource_group_name)使用 JMESPath 查询运行 az vm list,以显示在资源组中创建的虚拟机的名称。
az vm list \ --resource-group $resource_group_name \ --query "[].{\"VM Name\":name}" -o table
清理资源
不再需要通过 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 时遇到的常见问题