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

在虚拟机规模集的整个生命周期内,可能需要运行一个或多个管理任务。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 CLI 命令。This article details some of the common Azure CLI commands that let you perform these tasks.

若要完成这些管理任务,需要最新的 Azure CLI。To complete these management tasks, you need the latest Azure CLI. 请参阅安装 Azure CLI 了解相关信息。For information, see Install the Azure CLI. 如果需要创建虚拟机规模集,可以使用 Azure CLI 创建规模集If you need to create a virtual machine scale set, you can create a scale set with the Azure CLI.

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

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

az vmss show --resource-group myResourceGroup --name myScaleSet

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

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

az vmss list-instances `
    --resource-group myResourceGroup `
    --name myScaleSet `
    --output table

要查看与特定 VM 实例有关的其他信息,请将 --instance-id 参数添加到 az vmss get-instance-view,并指定要查看的实例。To view additional information about a specific VM instance, add the --instance-id parameter to az vmss get-instance-view 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:

az vmss get-instance-view `
    --resource-group myResourceGroup `
    --name myScaleSet `
    --instance-id 0

VM 连接信息列表List connection information for VMs

若要连接规模集中的 VM,可将 SSH 或 RDP 连接到分配的公共 IP 地址和端口号。To connect to the VMs in a scale set, you SSH or RDP to an assigned public IP address and port number. 默认情况下,会向将远程连接流量转发给每个 VM 的 Azure 负载均衡器添加网络地址转换 (NAT) 规则。By default, network address translation (NAT) rules are added to the Azure load balancer that forwards remote connection traffic to each VM. 若要列出规模集中连接 VM 的地址和端口,请使用 az vmss list-instance-connection-infoTo list the address and ports to connect to VM instances in a scale set, use az vmss list-instance-connection-info. 以下示例将列出 myScaleSet 规模集和 myResourceGroup 资源组中 VM 实例的连接信息。The following example lists connection information for VM instances in the scale set named myScaleSet and in the myResourceGroup resource group. 为这些名称提供自己的值:Provide your own values for these names:

az vmss list-instance-connection-info `
    --resource-group myResourceGroup `
    --name myScaleSet

更改规模集的容量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 creates or removes the required number of VMs, then configures the VMs to receive application traffic.

若要查看规模集中当前包含的实例数,请使用 az vmss show 并查询 “sku.capacity”:To see the number of instances you currently have in a scale set, use az vmss show and query on sku.capacity:

az vmss show `
    --resource-group myResourceGroup `
    --name myScaleSet `
    --query [sku.capacity] `
    --output table

然后,可以使用 az vmss scale 手动增加或减少规模集中虚拟机的数目。You can then manually increase or decrease the number of virtual machines in the scale set with az vmss scale. 以下示例将规模集中 VM 的数目设置为 5:The following example sets the number of VMs in your scale set to 5:

az vmss scale `
    --resource-group myResourceGroup `
    --name myScaleSet `
    --new-capacity 5

更新规模集容量需要花费数分钟。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,请使用 az vmss stopTo stop one or more VMs in a scale set, use az vmss stop. 通过 --instance-ids 参数,可指定要停止的一个或多个 VM。The --instance-ids 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 space.

以下示例将停止 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:

az vmss stop --resource-group myResourceGroup --name myScaleSet --instance-ids 0

已停止的 VM 会保留已分配状态,且会继续产生计算费用。Stopped VMs remain allocated and continue to incur compute charges. 如果想 VM 保持已解除分配状态,且仅会产生存储费用,使用 az vmss deallocateIf you instead wish the VMs to be deallocated and only incur storage charges, use az vmss deallocate. 要解除分配多个 VM,请用空格分隔每个实例 ID。To deallocate multiple VMs, separate each instance ID with a space. 以下示例停止并并解除分配 myScaleSet 规模集和 myResourceGroup 资源组中实例“0”。The following example stops and deallocates instance 0 in the scale set named myScaleSet and the myResourceGroup resource group. 请按照如下所示,提供值:Provide your own values as follows:

az vmss deallocate --resource-group myResourceGroup --name myScaleSet --instance-ids 0

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

要在规模集中启动一个或多个 VM,请使用 az vmss startTo start one or more VMs in a scale set, use az vmss start. 通过 --instance-ids 参数,可指定要启动的一个或多个 VM。The --instance-ids 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 space.

以下示例将启动 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:

az vmss start --resource-group myResourceGroup --name myScaleSet --instance-ids 0

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

若要重启规模集中的一个或多个 VM,请使用 az vmss restartTo restart one or more VMs in a scale set, use az vmss restart. 通过 --instance-ids 参数,可指定要重启的一个或多个 VM。The --instance-ids 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 space.

以下示例将重启 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:

az vmss restart --resource-group myResourceGroup --name myScaleSet --instance-ids 0

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

要在规模集中删除一个或多个 VM,请使用 az vmss delete-instancesTo remove one or more VMs in a scale set, use az vmss delete-instances. 通过 --instance-ids 参数,可指定要删除的一个或多个 VM。The --instance-ids parameter allows you to specify one or more VMs to remove. 若指定实例 ID 的 *,规模集中所有 VM 都将删除。If you specify * for the instance ID, all VMs in the scale set are removed. 要删除多个 VM,请用空格分隔每个实例 ID。To remove multiple VMs, separate each instance ID with a space.

以下示例将删除 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:

az vmss delete-instances --resource-group myResourceGroup --name myScaleSet --instance-ids 0

后续步骤Next steps

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