Share via


CLI 範例:在 Azure Batch 中建立和管理 Linux 集區

此指令碼示範 Azure CLI 中一些可用的命令,用於建立和管理 Azure Batch 中的 Linux 計算節點集區。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

在 Azure Batch 中建立 Linux 集區

# Create and manage a Linux pool in Azure Batch

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
[[ "$RESOURCE_GROUP" == '' ]] && resourceGroup="msdocs-batch-rg-$randomIdentifier" || resourceGroup="${RESOURCE_GROUP}"
tag="manage-pool-linux"
batchAccount="msdocsbatch$randomIdentifier"

# Create a resource group.
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Create a Batch account.
echo "Creating $batchAccount"
az batch account create --resource-group $resourceGroup --name $batchAccount --location "$location"

# Authenticate Batch account CLI session.
az batch account login --resource-group $resourceGroup --name $batchAccount --shared-key-auth

# Retrieve a list of available images and node agent SKUs.
az batch pool supported-images list --query "[?contains(imageReference.offer,'ubuntuserver') && imageReference.publisher == 'canonical'].{Offer:imageReference.offer, Publisher:imageReference.publisher, Sku:imageReference.sku, nodeAgentSkuId:nodeAgentSkuId}[-1]" --output tsv

# Create a new Linux pool with a virtual machine configuration. The image reference 
# and node agent SKUs ID can be selected from the ouptputs of the above list command.
# The image reference is in the format: {publisher}:{offer}:{sku}:{version} where {version} is
# optional and defaults to 'latest'."

az batch pool create --id mypool-linux --vm-size Standard_A1 --image canonical:ubuntuserver:18_04-lts-gen2 --node-agent-sku-id "batch.node.ubuntu 18.04"

# Resize the pool to start some VMs.
az batch pool resize --pool-id mypool-linux --target-dedicated 5

# Check the status of the pool to see when it has finished resizing.
az batch pool show --pool-id mypool-linux

# List the compute nodes running in a pool.
az batch node list --pool-id mypool-linux

# returns [] if no compute nodes are running

重新啟動批次節點

如果集區中的特定節點發生問題,則可以將其重新啟動,或重新安裝映像。 您可以使用上述清單命令來擷取節點的識別碼。 一般節點識別碼的格式為 tvm-xxxxxxxxxx_1-<timestamp>

az batch node reboot \
    --pool-id mypool-linux \
    --node-id tvm-123_1-20170316t000000z

刪除批次節點

您可以從集區中刪除一或多個計算節點,而且可以將已指派給該節點的任何工作重新配置給另一個節點。

az batch node delete \
    --pool-id mypool-linux \
    --node-list tvm-123_1-20170316t000000z tvm-123_2-20170316t000000z \
    --node-deallocation-option requeue

清除資源

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

az group delete --name $resourceGroup

範例參考

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
az group create 建立用來存放所有資源的資源群組。
az batch account create 建立 Batch 帳戶。
az batch account login 對指定的 Batch 帳戶驗證以進行進一步的 CLI 互動。
az batch pool node-agent-skus list 列出可用的節點代理程式 SKU 和映像資訊。
az batch pool create 建立計算節點的集區。
az batch pool resize 調整指定集區的執行中 VM 數大小。
az batch pool show 顯示集區的屬性。
az batch node list 列出指定集區中的所有計算節點。
az batch node reboot 重新啟動指定的計算節點。
az batch node delete 從指定的集區中刪除列出的節點。
az group delete 刪除資源群組,包括所有的巢狀資源。

下一步

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件