Share via


CLI の例: Azure Batch での Windows プールの作成と管理

このスクリプトでは、Azure Batch で Windows コンピューティング ノードのプールを作成して管理するために、Azure CLI で使用できるコマンドの一部を示します。 Windows プールは、クラウド サービスの構成と仮想マシンの構成という 2 つの方法で構成できます。 この例では、クラウド サービスの構成を使用して Windows プールを作成する方法を示します。

Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

前提条件

  • Azure Cloud Shell で Bash 環境を使用します。 詳細については、「Azure Cloud Shell の Bash のクイックスタート」を参照してください。

  • CLI リファレンス コマンドをローカルで実行する場合、Azure CLI をインストールします。 Windows または macOS で実行している場合は、Docker コンテナーで Azure CLI を実行することを検討してください。 詳細については、「Docker コンテナーで Azure CLI を実行する方法」を参照してください。

    • ローカル インストールを使用する場合は、az login コマンドを使用して Azure CLI にサインインします。 認証プロセスを完了するには、ターミナルに表示される手順に従います。 その他のサインイン オプションについては、Azure CLI でのサインインに関するページを参照してください。

    • 初回使用時にインストールを求められたら、Azure CLI 拡張機能をインストールします。 拡張機能の詳細については、Azure CLI で拡張機能を使用する方法に関するページを参照してください。

    • az version を実行し、インストールされているバージョンおよび依存ライブラリを検索します。 最新バージョンにアップグレードするには、az upgrade を実行します。

サンプル スクリプト

Azure Cloud Shell を起動する

Azure Cloud Shell は無料のインタラクティブ シェルです。この記事の手順は、Azure Cloud 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 サブスクリプション ID に置き換えます。 Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

subscription="<subscriptionId>" # add subscription here

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

詳細については、アクティブなサブスクリプションの設定または対話形式のログインに関する記事を参照してください

スクリプトを実行する

# Create and manage a Windows 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-windows"
storageAccount="msdocsstorage$randomIdentifier"
batchAccount="msdocsbatch$randomIdentifier"

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

# Create a general-purpose storage account in your resource group.
echo "Creating $storageAccount"
az storage account create --resource-group $resourceGroup --name $storageAccount --location "$location" --sku Standard_LRS

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

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

# Create a new Windows cloud service platform pool with 3 Standard A1 VMs.
# The pool has a start task that runs a basic shell command. Typically a 
# start task copies application files to the pool nodes.
az batch pool create --id mypool-windows --os-family 4 --target-dedicated 3 --vm-size small --start-task-command-line "cmd /c dir /s" --start-task-wait-for-success 
    
# --application-package-references myapp    
# You can specify an application package reference when the pool is created or you can add it later.
# https://docs.microsoft.com/azure/batch/batch-application-packages.

# Add some metadata to the pool.
az batch pool set --pool-id mypool-windows --metadata IsWindows=true VMSize=StandardA1

# Change the pool to enable automatic scaling of compute nodes.
# This autoscale formula specifies that the number of nodes should be adjusted according
# to the number of active tasks, up to a maximum of 10 compute nodes.
az batch pool autoscale enable --pool-id mypool-windows --auto-scale-formula '$averageActiveTaskCount = avg($ActiveTasks.GetSample(TimeInterval_Minute * 15));$TargetDedicated = min(10, $averageActiveTaskCount);'

# Monitor the resizing of the pool.
az batch pool show --pool-id mypool-windows

# Disable autoscaling when we no longer require the pool to automatically scale.
az batch pool autoscale disable --pool-id mypool-windows

リソースをクリーンアップする

次のように az group delete コマンドを使用して、リソース グループと、それに関連付けられているすべてのリソースを削除します。ただし、これらのリソースが継続的に必要でない場合に限ります。 これらのリソースの一部は、削除や作成に時間がかかる場合があります。

az group delete --name $resourceGroup

サンプル リファレンス

このスクリプトでは、次のコマンドを使用します。 表内の各コマンドは、それぞれのドキュメントにリンクされています。

コマンド メモ
az group create すべてのリソースを格納するリソース グループを作成します。
az batch account create Batch アカウントを作成します。
az batch account login さらに CLI と対話できるように、指定された Batch アカウントを認証します。
az batch pool create コンピューティング ノードのプールを作成します。
az batch pool set プールのプロパティを更新します。
az batch pool autoscale enable プールで自動スケーリングを有効にし、式を適用します。
az batch pool show プールのプロパティを表示します。
az batch pool autoscale disable プールで自動スケーリングを無効にします。
az group delete 入れ子になったリソースすべてを含むリソース グループを削除します。

次のステップ

Azure CLI の詳細については、Azure CLI のドキュメントのページをご覧ください。