Azure Virtual Machine Scale Sets (VMSS) を使用すると、一連の同一の仮想マシンを配置して管理できます。仮想マシンの数は、設定可能なスケーリングルールに基づいて自動的に増加または減少します。詳細については、Azure Virtual Machine Scale Sets とはを参照してください
エージェントが事前にインストールされ、事前有効化されている基本VMイメージを含めるようにVMSSを設定できます。VMSSがスケールアップすると、スケールセットの新しい仮想マシンインスタンスにAgentが自動的に含まれます。
AgentをVMSSに追加するには:
- ステップ 1: (推奨) Azure アカウントを Workload Security に追加する
- ステップ2: デプロイスクリプトを準備する
- ステップ 3: カスタムスクリプト拡張機能を使用して VMSS インスタンスにエージェントを追加する
手順1: AzureアカウントをWorkload Securityに追加する (推奨)
Azureアカウントを Workload Securityに追加すると、そのアカウントで作成されたすべてのAzureインスタンスが Workload Security
にロードされ、 [コンピュータ]の下に表示されます。インスタンスは、エージェントがインストールされているかどうかに関係なく表示されます。エージェントを含まないものには、 [ステータス] の [いいえエージェント]があります。エージェントをインストールしてアクティベートすると、その [ステータス] が [マネージド (オンライン)]に変更されます。
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に追加するを参照してください。
手順2: インストールスクリプトを準備する
Workload Securityで、デプロイメントスクリプトを準備します。手順については、デプロイメントスクリプトを使用してコンピュータを追加および保護するを参照してください。このデプロイメントスクリプトは、次に設定するカスタムスクリプト拡張機能で参照されます。
次のVMSSスクリプトを使用してカスタムスクリプトを実行するには、Azure Blobストレージに、または有効なURLを介してアクセス可能な場所にスクリプトを保存する必要があります。Azure
Blobストレージへファイルをアップロードする手順については、Azure PowerShellでAzure Blobストレージ操作を実行するを参照してください。
手順3: カスタムスクリプト拡張機能を介してAgentをVMSSインスタンスに追加する
PowerShellを使用してエージェントを追加する方法の例を次に示します。
両方の例:
- Add-AzureRmVmssExtensionコマンドレット を使用して、VMSSに拡張機能を追加します。
- Azure PowerShellバージョン5.1.1を使用します。
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