使用英语阅读

通过


你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

部署 Azure Batch 帐户和两个池 - Terraform

在本快速入门中,你将使用 Terraform 创建 Azure Batch 帐户、Azure 存储帐户和两个 Batch 池。 Batch 是一种基于云的作业计划服务,它可以将大量数据的处理并行化并分配到多台计算机上。 该服务通常用于参数扫描、Monte Carlo 模拟、财务风险建模和其他高性能计算应用程序。 Batch 帐户是 Batch 服务中的顶级资源,提供对池、作业和任务的访问权限。 存储帐户用于存储和管理 Batch 服务使用和生成的所有文件,而两个 Batch 池是执行任务的计算节点集合。

使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。

  • 指定所需的 Terraform 版本和所需的提供程序。
  • 定义没有其他功能的 Azure 提供程序。
  • 为资源组的位置和资源组名称的前缀定义变量。
  • 使用提供的前缀为资源组生成随机名称。
  • 在指定位置创建具有生成名称的 Azure 资源组。
  • 为存储帐户生成要用作名称的随机字符串。
  • 在创建的资源组中,使用生成的名称在同一位置创建存储帐户,该帐户为标准帐户层级,采用本地冗余存储复制类型。
  • 为 Batch 帐户生成要用作名称的另一个随机字符串。
  • 在创建的资源组中,使用生成的名称在同一位置创建 Batch 帐户,并在存储密钥身份验证模式下将其链接到已创建的存储帐户。
  • 为 Batch 池生成带有“pool”前缀的随机名称。
  • 在创建的资源组中,使用生成的名称创建一个具有缩放功能的 Batch 池,并将其链接到已创建的 Batch 帐户,该池配置为标准 A1 虚拟机 (VM) 大小,采用 Ubuntu 22.04 节点代理 SKU,并包含一个启动任务,该任务会回显“Hello World from $env”,最大重试次数为 1 次,同时要求成功完成任务。
  • 在已创建的资源组中,使用相同的生成名称再创建一个具有自动缩放功能的 Batch 池,并将其链接到已创建的 Batch 帐户,该池配置为标准 A1 VM 大小,采用 Ubuntu 22.04 节点代理 SKU 和自动缩放公式。
  • 输出创建的资源组、存储帐户、Batch 帐户和两个 Batch 池的名称。

先决条件

实现 Terraform 代码

备注

本文中的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。

有关更多示例,请参阅演示如何使用 Terraform 管理 Azure 资源的文章和示例代码

  1. 创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。

  2. 创建名为 main.tf 的文件并插入以下代码:

    Terraform
    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    resource "random_string" "storage_account_name" {
      length  = 8
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_storage_account" "example" {
      name                     = random_string.storage_account_name.result
      resource_group_name      = azurerm_resource_group.rg.name
      location                 = azurerm_resource_group.rg.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    
    resource "random_string" "batch_account_name" {
      length  = 8
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_batch_account" "example" {
      name                                = random_string.batch_account_name.result
      resource_group_name                 = azurerm_resource_group.rg.name
      location                            = azurerm_resource_group.rg.location
      storage_account_id                  = azurerm_storage_account.example.id
      storage_account_authentication_mode = "StorageKeys"
    }
    
    resource "random_pet" "azurerm_batch_pool_name" {
      prefix = "pool"
    }
    
    resource "azurerm_batch_pool" "fixed" {
      name                = "${random_pet.azurerm_batch_pool_name.id}-fixed-pool"
      resource_group_name = azurerm_resource_group.rg.name
      account_name        = azurerm_batch_account.example.name
      display_name        = "Fixed Scale Pool"
      vm_size             = "Standard_A1"
      node_agent_sku_id   = "batch.node.ubuntu 22.04"
    
      fixed_scale {
        target_dedicated_nodes = 2
        resize_timeout         = "PT15M"
      }
    
      storage_image_reference {
        publisher = "Canonical"
        offer     = "0001-com-ubuntu-server-jammy"
        sku       = "22_04-lts"
        version   = "latest"
      }
    
      start_task {
        command_line       = "echo 'Hello World from $env'"
        task_retry_maximum = 1
        wait_for_success   = true
    
        common_environment_properties = {
          env = "TEST"
        }
    
        user_identity {
          auto_user {
            elevation_level = "NonAdmin"
            scope           = "Task"
          }
        }
      }
    
      metadata = {
        "tagName" = "Example tag"
      }
    }
    
    resource "azurerm_batch_pool" "autopool" {
      name                = "${random_pet.azurerm_batch_pool_name.id}-autoscale-pool"
      resource_group_name = azurerm_resource_group.rg.name
      account_name        = azurerm_batch_account.example.name
      display_name        = "Auto Scale Pool"
      vm_size             = "Standard_A1"
      node_agent_sku_id   = "batch.node.ubuntu 22.04"
    
      auto_scale {
        evaluation_interval = "PT15M"
    
        formula = <<EOF
          startingNumberOfVMs = 1;
          maxNumberofVMs = 25;
          pendingTaskSamplePercent = $PendingTasks.GetSamplePercent(180 * TimeInterval_Second);
          pendingTaskSamples = pendingTaskSamplePercent < 70 ? startingNumberOfVMs : avg($PendingTasks.GetSample(180 * TimeInterval_Second));
          $TargetDedicatedNodes=min(maxNumberofVMs, pendingTaskSamples);
    EOF
      }
    
      storage_image_reference {
        publisher = "Canonical"
        offer     = "0001-com-ubuntu-server-jammy"
        sku       = "22_04-lts"
        version   = "latest"
      }
    }
    
  3. 创建名为 outputs.tf 的文件并插入以下代码:

    Terraform
    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "storage_account_name" {
      value = azurerm_storage_account.example.name
    }
    
    output "batch_account_name" {
      value = azurerm_batch_account.example.name
    }
    
    output "batch_pool_fixed_name" {
      value = azurerm_batch_pool.fixed.name
    }
    
    output "batch_pool_autopool_name" {
      value = azurerm_batch_pool.autopool.name
    }
    
  4. 创建名为 providers.tf 的文件并插入以下代码:

    Terraform
    terraform {
      required_version = ">=1.0"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
  5. 创建名为 variables.tf 的文件并插入以下代码:

    Terraform
    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
    }
    

初始化 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

验证结果

运行 az batch account show 以查看 Batch 帐户。

Azure CLI
az batch account show --name <batch_account_name> --resource-group <resource_group_name>

请将 <batch_account_name> 替换为 Batch 帐户的名称,将 <resource_group_name> 替换为资源组的名称。

清理资源

不再需要通过 Terraform 创建的资源时,请执行以下步骤:

  1. 运行 terraform plan 并指定 destroy 标志。

    控制台
    terraform plan -destroy -out main.destroy.tfplan
    

    要点:

    • terraform plan 命令将创建一个执行计划,但不会执行它。 它会确定创建配置文件中指定的配置需要执行哪些操作。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。
    • 使用可选 -out 参数可以为计划指定输出文件。 使用 -out 参数可以确保所查看的计划与所应用的计划完全一致。
  2. 运行 terraform apply 以应用执行计划。

    控制台
    terraform apply main.destroy.tfplan
    

Azure 上的 Terraform 故障排除

排查在 Azure 上使用 Terraform 时遇到的常见问题

后续步骤

注意:作者在 AI 的帮助下创作了此文章。 了解详细信息