共用方式為


部署具有啟動工作的 Azure Batch 帳戶和兩個集區 - Terraform

在本快速入門中,您會使用 Terraform 建立 Azure Batch 帳戶、Azure 儲存體 帳戶和兩個 Batch 集區。 Batch 是雲端式作業排程服務,可將大量數據的處理平行處理並分散到多部計算機上。 它通常用於轉譯 3D 圖形、分析大型數據集或處理影片等工作。 在此情況下,建立的資源包括 Batch 帳戶(這是分散式處理工作的集中組織實體)、用來保存要處理數據的記憶體帳戶,以及兩個 Batch 集區,也就是執行工作的虛擬機群組。

Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。

  • 指定必要的 Terraform 版本和必要的提供者。
  • 定義沒有其他功能的 Azure 提供者。
  • 定義資源群組位置和名稱前置詞的變數。
  • 產生 Azure 資源群組的隨機名稱。
  • 在指定的位置建立具有產生名稱的資源群組。
  • 產生記憶體帳戶名稱的隨機字串。
  • 在建立的資源群組中,建立具有產生名稱的記憶體帳戶。
  • 產生 Batch 帳戶名稱的隨機字串。
  • 在建立的資源群組中建立具有所產生名稱的 Batch 帳戶,並連結至建立的記憶體帳戶。
  • 產生 Batch 集區的隨機名稱。
  • 在建立的資源群組中建立具有固定規模的 Batch 集區,並連結至建立的 Batch 帳戶。
  • 在建立的資源群組中建立具有自動調整的 Batch 集區,並連結至已建立的 Batch 帳戶。
  • 輸出所建立資源群組、記憶體帳戶、Batch 帳戶和兩個 Batch 集區的名稱。

必要條件

實作 Terraform 程式碼

  1. 建立要在其中測試及執行範例 Terraform 程式代碼的目錄,並將其設為目前目錄。

  2. 建立名為 main.tf的檔案,並插入下列程序代碼:

    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_D4_v3"
      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_D4_v3"
      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的檔案,並插入下列程序代碼:

    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 {
      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的檔案,並插入下列程序代碼:

    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 帳戶。

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

在上述命令中,將 取代為 Batch 帳戶的名稱,並以<batch_account_name>您的資源群組名稱取代 <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 時的常見問題進行疑難排解。

下一步