ビュー:
Azure Virtual Machine Scale Sets (VMSS) を使用すると、一連の同一の仮想マシンを配置して管理できます。仮想マシンの数は、設定可能なスケーリングルールに基づいて自動的に増加または減少します。詳細については、Azure Virtual Machine Scale Sets とはを参照してください
エージェントが事前にインストールされ、事前有効化されている基本VMイメージを含めるようにVMSSを設定できます。VMSSがスケールアップすると、スケールセットの新しい仮想マシンインスタンスにAgentが自動的に含まれます。
AgentをVMSSに追加するには:
  1. AzureアカウントをWorkload Securityに追加する (推奨)
  2. デプロイメントスクリプトを準備する
  3. カスタムスクリプト拡張を通じてVMSSインスタンスにエージェントを追加する

AzureアカウントをWorkload Securityに追加する (推奨)

AzureアカウントをWorkload Securityに追加すると、そのアカウントで作成されたすべてのAzureインスタンスがWorkload Securityにロードされ、[コンピュータ]に表示されます。これらのインスタンスは、エージェントがインストールされているかどうかに関係なく表示されます。エージェントが含まれていないインスタンスは、[ステータス][Agentなし]です。それらにエージェントをインストールしてアクティブ化すると、[ステータス][管理 (オンライン)]に変わります。
Azureアカウントを追加した後にスケールセットが手動または自動でスケールアップされると、Workload Securityは新しいAzureインスタンスを検出し、[コンピュータ]のリストに追加します。同様に、スケールセットがスケールダウンされると、インスタンスは表示から削除されます。このようにして、Workload Securityは常にスケールセット内の利用可能なAzureインスタンスの現在のリストを表示します。
ただし、Azureアカウントを Workload Securityに追加せず、代わりに別の方法を使用して個々のAzureインスタンスを追加する場合、Workload Security では縮退が検出されず、存在しないAzureも削除されません。インスタンスをリストから削除します。Workload SecurityでAzure VMのリストが拡大するのを防ぐために、いつAzureインスタンスをいつでも使用できるようにするには、Azureアカウントを Workload Securityに追加することを強くお勧めします。
Azureアカウントの追加手順については、Microsoft AzureアカウントをWorkload Securityに追加するを参照してください。

デプロイメントスクリプトを準備する

Workload Securityで、デプロイメントスクリプトを準備します。手順については、デプロイメントスクリプトを使用してコンピュータを追加および保護するを参照してください。このデプロイメントスクリプトは、次に設定するカスタムスクリプト拡張機能で参照されます。
次のVMSSスクリプトを使用してカスタムスクリプトを実行するには、Azure Blobストレージに、または有効なURLを介してアクセス可能な場所にスクリプトを保存する必要があります。Azure Blobストレージへファイルをアップロードする手順については、Azure PowerShellでAzure Blobストレージ操作を実行するを参照してください。

カスタムスクリプト拡張機能を使用してVMSSインスタンスにエージェントを追加する

PowerShellを使用してエージェントを追加する方法の例を次に示します。
  • 例1は、エージェントを含む新しいVMSSを作成する方法を示しています。
  • 例2は、既存のVMSSにエージェントを追加する方法を示しています。
両方の例:
PowerShell cmdletを使用して新しいVMSSを作成する手順については、このMicrosoftチュートリアルを参照してください。Linuxプラットフォームの場合は、https://github.com/Azure/custom-script-extension-linuxを参照してください。

例1: Agentを含む新しいVMSSを作成する

$resourceGroupName = <The resource group of the VMSS>
$vmssname = <The name of the VMSS>

# Create ResourceGroup
New-AzureRmResourceGroup -ResourceGroupName $resourceGroupName -Location EastUS

# Create a config object
$vmssConfig = New-AzureRmVmssConfig `
 -Location EastUS `
 -SkuCapacity 2 `
 -SkuName Standard_DS2 `
 -UpgradePolicyMode Automatic

# Define the script for your Custom Script Extension to run on the Windows Platform
$customConfig = @{
 "fileUris" = (,"A URL of your copy of deployment script, ex. deploymentscript.ps1");
 "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File deploymentscript.ps1"
}

# Define the script for your Custom Script Extension to run on the Linux Platform
#$customConfig = @{
# "fileUris" = (,"A URL of your copy of deployment script, ex. deploymentscript.sh");
# "commandToExecute" = "bash deploymentscript.sh"
#}

# The section is required only if deploymentscript has been located within Azure StorageAccount
$storageAccountName = <StorageAccountName if deploymentscript is locate in Azure Storage>
$key = (Get-AzureRmStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
$protectedConfig = @{
 "storageAccountName" = $storageAccountName;
 "storageAccountKey" = $key
}

# Use Custom Script Extension to install the agent (Windows)
Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmssConfig `
 -Name "customScript" `
 -Publisher "Microsoft.Compute" `
 -Type "CustomScriptExtension" `
 -TypeHandlerVersion 1.8 `
 -Setting $customConfig `
 -ProtectedSetting $protectedConfig

# Use Custom Script Extension to install the agent (Linux)
#Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmssConfig `
# -Name "customScript" `
# -Publisher "Microsoft.Azure.Extensions" `
# -Type "customScript" `
# -TypeHandlerVersion 2.0 `
# -Setting $customConfig `
# -ProtectedSetting $protectedConfig

# Create a public IP address
# Create a frontend and backend IP pool
# Create the load balancer
# Create a load balancer health probe on port 80
# Create a load balancer rule to distribute traffic on port 80
# Update the load balancer configuration
# Reference a virtual machine image from the gallery
# Set up information for authenticating with the virtual machine
# Create the virtual network resources
# Attach the virtual network to the config object

# Create the scale set with the config object (this step might take a few minutes)
New-AzureRmVmss `
 -ResourceGroupName $resourceGroupName `
 -Name $vmssname `
 -VirtualMachineScaleSet $vmssConfig

例2:既存のVMSSにエージェントを追加する

$resourceGroupName = <The resource group of the VMSS>
$vmssname = <The name of the VMSS>

# Get the VMSS model
$vmssobj = Get-AzureRmVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $vmssname

# Show model data if you prefer
# Write-Output $vmssobj

# Define the script for your Custom Script Extension to run on the Windows platform
$customConfig = @{
 "fileUris" = (,"A URL of your copy of deployment script, ex. deploymentscript.ps1");
 "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File deploymentscript.ps1"
}

# Define the script for your Custom Script Extension to run on the Linux platform
#$customConfig = @{
# "fileUris" = (,"A URL of your copy of deployment script, ex. deploymentscript.sh");
# "commandToExecute" = "bash deploymentscript.sh"
#}

# The section is required only if deploymentscript has been located within Azure StorageAccount
$storageAccountName = <StorageAccountName if deploymentscript is locate in Azure Storage>
$key= (Get-AzureRmStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
$protectedConfig = @{
 "storageAccountName" = $storageAccountName;
 "storageAccountKey" = $key
}

# Use Custom Script Extension to install the agent (Windows)
$newvmssobj = Add-AzureRmVmssExtension `
 -VirtualMachineScaleSet $vmssobj `
 -Name "customScript" `
 -Publisher "Microsoft.Compute" `
 -Type "CustomScriptExtension" `
 -TypeHandlerVersion 1.8 `
 -Setting $customConfig `
 -ProtectedSetting $protectedConfig

# Use Custom Script Extension to install the agent (Linux)
#$newvmssobj = Add-AzureRmVmssExtension `
# -VirtualMachineScaleSet $vmssobj `
# -Name "customScript" `
# -Publisher "Microsoft.Azure.Extensions" `
# -Type "customScript" `
# -TypeHandlerVersion 2.0 `
# -Setting $customConfig `
# -ProtectedSetting $protectedConfig

# Update the virtual machine scale set model
Update-AzureRmVmss -ResourceGroupName $resourceGroupName -name $vmssname -VirtualMachineScaleSet $newvmssobj -Verbose

# Get Instance ID for all instances in this VMSS, and decide which instance you'd like to update
# Get-AzureRmVmssVM -ResourceGroupName $resourceGroupName -VMScaleSetName $vmssname

# Now start updating instances
# If upgradePolicy is Automatic in the VMSS, do NOT execute the next command Update-AzureRmVmssInstance. Azure will auto-update the VMSS.
# There's no PowerShell command to update all instances at once. But you could refer to the output of Update-AzureRmVmss, and loop all instances into this command.
Update-AzureRmVmssInstance -ResourceGroupName $resourceGroupName -VMScaleSetName $vmssname -InstanceId 0