使用 Azure PowerShell 管理虚拟机规模集Manage a virtual machine scale set with Azure PowerShell

在虚拟机规模集的整个生命周期内,可能需要运行一个或多个管理任务。Throughout the lifecycle of a virtual machine scale set, you may need to run one or more management tasks. 此外,可能还需要创建自动执行各种生命周期任务的脚本。Additionally, you may want to create scripts that automate various lifecycle-tasks. 本文详细介绍了执行这些任务常用的一些 Azure PowerShell cmdlet。This article details some of the common Azure PowerShell cmdlets that let you perform these tasks.

如果需要创建虚拟机规模集,可以使用 Azure PowerShell 创建规模集If you need to create a virtual machine scale set, you can create a scale set with Azure PowerShell.

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

查看有关规模集的信息View information about a scale set

若要查看有关规模集的全部信息,请使用 Get-AzVmssTo view the overall information about a scale set, use Get-AzVmss. 以下示例将获取有关 myResourceGroup 资源组中 myScaleSet 规模集的信息 。The following example gets information about the scale set named myScaleSet in the myResourceGroup resource group. 按如下所示输入自己的名称:Enter your own names as follows:

Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

查看规模集中的 VMView VMs in a scale set

要在规模集中查看 VM 实例的列表,请使用 Get-AzVmssVMTo view a list of VM instance in a scale set, use Get-AzVmssVM. 以下示例列出名为 myScaleSet 的规模集和 myResourceGroup 资源组中的所有 VM 实例。The following example lists all VM instances in the scale set named myScaleSet and in the myResourceGroup resource group. 为这些名称提供自己的值:Provide your own values for these names:

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

要查看与特定 VM 实例有关的其他信息,请将 -InstanceId 参数添加到 Get-AzVmssVM,并指定要查看的实例。To view additional information about a specific VM instance, add the -InstanceId parameter to Get-AzVmssVM and specify an instance to view. 以下示例将查看与 myScaleSet 规模集和 myResourceGroup 资源组中 VM 实例“0”有关的信息 。The following example views information about VM instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. 按如下所示输入自己的名称:Enter your own names as follows:

Get-AzVmssVM -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

还可以通过一个 API 调用获取所有实例的详细 instanceView 信息,这有助于避免大型安装的 API 限制。You can also get detailed instanceView information for all instances in one API call, which can help avoid API throttling for large installations.

Get-AzVmssVM -InstanceView -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"
GET "https://management.chinacloudapi.cn/subscriptions/<sub-id>/resourceGroups/<resourceGroupName>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSSName>/virtualMachines?api-version=2019-03-01&%24expand=instanceView"

更改规模集的容量Change the capacity of a scale set

以上命令显示了与规模集和 VM 实例相关的信息。The preceding commands showed information about your scale set and the VM instances. 要增加或减少规模集中的实例数,可以更改其容量。To increase or decrease the number of instances in the scale set, you can change the capacity. 规模集会自动创建或删除所需数量的 VM,然后配置 VM 以接收应用程序流量。The scale set automatically creates or removes the required number of VMs, then configures the VMs to receive application traffic.

首先,使用 Get-AzVmss 创建的规模集对象,然后为 sku.capacity 指定新的值。First, create a scale set object with Get-AzVmss, then specify a new value for sku.capacity. 若要应用容量更改,请使用 Update-AzVmssTo apply the capacity change, use Update-AzVmss. 以下示例更新 myResourceGroup 资源组中的 myScaleSet,将其容量更新为能容纳 5 个实例 。The following example updates myScaleSet in the myResourceGroup resource group to a capacity of 5 instances. 请按照如下所示,提供值:Provide your own values as follows:

# Get current scale set
$vmss = Get-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet"

# Set and update the capacity of your scale set
$vmss.sku.capacity = 5
Update-AzVmss -ResourceGroupName "myResourceGroup" -Name "myScaleSet" -VirtualMachineScaleSet $vmss

更新规模集容量需要花费数分钟。If takes a few minutes to update the capacity of your scale set. 如果减少规模集的容量,将首先删除实例 ID 最大的 VM。If you decrease the capacity of a scale set, the VMs with the highest instance IDs are removed first.

停止和启动规模集中的 VMStop and start VMs in a scale set

若要停止规模集中的一个或多个 VM,请使用 Stop-AzVmssTo stop one or more VMs in a scale set, use Stop-AzVmss. 通过 -InstanceId 参数,可指定要停止的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to stop. 若不指定实例 ID,则停止规模集中的所有 VM。If you do not specify an instance ID, all VMs in the scale set are stopped. 要停止多个 VM,请用逗号分隔每个实例 ID。To stop multiple VMs, separate each instance ID with a comma.

以下示例将停止 myScaleSet 规模集和 myResourceGroup 资源组中的实例“0” 。The following example stops instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. 请按照如下所示,提供值:Provide your own values as follows:

Stop-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

默认情况下,将取消分配已停止的 VM,这些 VM 不会产生计算费用。By default, stopped VMs are deallocated and do not incur compute charges. 若要在停止 VM 后保持预配状态,请将 -StayProvisioned 参数添加到上面的命令中。If you wish the VM to remain in a provisioned state when stopped, add the -StayProvisioned parameter to the preceding command. 保持预配状态的已停止 VM 会产生常规计算费用。Stopped VMs that remain provisioned incur regular compute charges.

启动规模集中的 VMStart VMs in a scale set

若要启动规模集中的一个或多个 VM,请使用 Start-AzVmssTo start one or more VMs in a scale set, use Start-AzVmss. 通过 -InstanceId 参数,可指定要启动的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to start. 若不指定实例 ID,则启动规模集中的所有 VM。If you do not specify an instance ID, all VMs in the scale set are started. 要启动多个 VM,请用逗号分隔每个实例 ID。To start multiple VMs, separate each instance ID with a comma.

以下示例将启动 myScaleSet 规模集和 myResourceGroup 资源组中的实例“0” 。The following example starts instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. 请按照如下所示,提供值:Provide your own values as follows:

Start-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

重启规模集中的 VMRestart VMs in a scale set

若要重启规模集中的一个或多个 VM,请使用 Restart-AzVmssTo restart one or more VMs in a scale set, use Restart-AzVmss. 通过 -InstanceId 参数,可指定要重启的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to restart. 若不指定实例 ID,则重启规模集中的所有 VM。If you do not specify an instance ID, all VMs in the scale set are restarted. 要重启多个 VM,请用逗号分隔每个实例 ID。To restart multiple VMs, separate each instance ID with a comma.

以下示例将重启 myScaleSet 规模集和 myResourceGroup 资源组中的实例“0” 。The following example restarts instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. 请按照如下所示,提供值:Provide your own values as follows:

Restart-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

删除规模集中的 VMRemove VMs from a scale set

若要删除规模集中的一个或多个 VM,请使用 Remove-AzVmssTo remove one or more VMs in a scale set, use Remove-AzVmss. 通过 -InstanceId 参数,可指定要删除的一个或多个 VM。The -InstanceId parameter allows you to specify one or more VMs to remove. 若不指定实例 ID,规模集中所有 VM 都将删除。If you do not specify an instance ID, all VMs in the scale set are removed. 要删除多个 VM,请用逗号分隔每个实例 ID。To remove multiple VMs, separate each instance ID with a comma.

以下示例将删除 myScaleSet 规模集和 myResourceGroup 资源组中的实例“0” 。The following example removes instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. 请按照如下所示,提供值:Provide your own values as follows:

Remove-AzVmss -ResourceGroupName "myResourceGroup" -VMScaleSetName "myScaleSet" -InstanceId "0"

后续步骤Next steps

规模集的其他常见任务包括如何部署应用程序升级 VM 实例Other common tasks for scale sets include how to deploy an application, and upgrade VM instances. 也可使用 Azure PowerShell 来配置自动缩放规则You can also use Azure PowerShell to configure auto-scale rules.