訓練
模組
使用 Azure 入口網站建立 Azure Batch 帳戶 - Training
Azure Batch 是可讓您在雲端有效率地執行大規模平行和高效能運算 (HPC) 應用程式的服務。 無需管理或設定基礎結構。 只需排程作業、配置您所需的資源,其餘就交給 Batch 處理。
在本快速入門中,您會使用 Terraform 建立 Azure Batch 帳戶、Azure 儲存體 帳戶和兩個 Batch 集區。 Batch 是雲端式作業排程服務,可將大量數據的處理平行處理並分散到多部計算機上。 它通常用於參數掃掠、蒙特卡洛模擬、財務風險模型和其他高效能運算應用程式。 Batch 帳戶是 Batch 服務中的最上層資源,可提供集區、作業和工作的存取權。 記憶體帳戶可用來儲存和管理 Batch 服務所使用的所有檔案,而兩個 Batch 集區則是執行工作的計算節點集合。
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
注意
本文中的範例程式碼位於 Azure Terraform GitHub 存放庫。 您可以檢視內含目前和舊版 Terraform 測試結果的記錄檔。
建立要在其中測試及執行範例 Terraform 程式代碼的目錄,並將其設為目前目錄。
建立名為 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_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"
}
}
建立名為 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
}
建立名為 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 {}
}
建立名為 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 init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點︰
-upgrade
參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。-out
參數可讓您指定計畫的輸出檔。 使用 -out
參數可確保您所檢閱的方案就是所套用的方案。執行 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 帳戶名稱,並以<resource_group_name>
您的資源群組名稱取代 <batch_account_name>
。
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform plan 並指定 destroy
旗標。
terraform plan -destroy -out main.destroy.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。-out
參數可讓您指定計畫的輸出檔。 使用 -out
參數可確保您所檢閱的方案就是所套用的方案。執行 terraform apply 以套用執行方案。
terraform apply main.destroy.tfplan
針對在 Azure 上使用 Terraform 時的常見問題進行疑難排解。
請參閱更多有關 Batch 帳戶的文章。
訓練
模組
使用 Azure 入口網站建立 Azure Batch 帳戶 - Training
Azure Batch 是可讓您在雲端有效率地執行大規模平行和高效能運算 (HPC) 應用程式的服務。 無需管理或設定基礎結構。 只需排程作業、配置您所需的資源,其餘就交給 Batch 處理。
文件
快速入門:使用 Terraform 建立 Azure Batch 帳戶 - Azure Batch
在本文中,您會使用 Terraform 建立 Azure Batch 帳戶
快速入門:使用 Azure CLI 建立 Batch 帳戶並執行作業 - Azure Batch
遵循本快速入門,使用 Azure CLI 建立 Batch 帳戶、計算節點的集區,以及在集區上執行基本工作的作業。
快速入門:使用 Azure 入口網站建立 Batch 帳戶並執行作業 - Azure Batch
遵循本快速入門,使用 Azure 入口網站建立 Batch 帳戶、計算節點的集區,以及在集區上執行基本工作的作業。