教程:使用 Azure CLI 创建和管理虚拟机规模集Tutorial: Create and manage a virtual machine scale set with the Azure CLI

利用虚拟机规模集,可以部署和管理一组相同的、自动缩放的虚拟机。A virtual machine scale set allows you to deploy and manage a set of identical, auto-scaling virtual machines. 在虚拟机规模集的整个生命周期内,可能需要运行一个或多个管理任务。Throughout the lifecycle of a virtual machine scale set, you may need to run one or more management tasks. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 创建和连接虚拟机规模集Create and connect to a virtual machine scale set
  • 选择并使用 VM 映像Select and use VM images
  • 查看和使用特定 VM 实例大小View and use specific VM instance sizes
  • 手动缩放规模集Manually scale a scale set
  • 执行常见的规模集管理任务Perform common scale set management tasks

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don’t have an Azure subscription, create a Trial before you begin.

如果选择在本地安装和使用 CLI,本教程要求运行 Azure CLI 2.0.29 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.29 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。An Azure resource group is a logical container into which Azure resources are deployed and managed. 必须在创建虚拟机规模集前创建资源组。A resource group must be created before a virtual machine scale set. 使用 az group create 命令创建资源组。Create a resource group with the az group create command. 本示例在 chinanorth 区域中创建名为 myResourceGroup 的资源组。In this example, a resource group named myResourceGroup is created in the chinanorth region.

az group create --name myResourceGroup --location chinanorth

在本教程中,此资源组名称是在创建或修改规模集时指定的。The resource group name is specified when you create or modify a scale set throughout this tutorial.

创建规模集Create a scale set

请使用 az vmss create 命令创建虚拟机规模集。You create a virtual machine scale set with the az vmss create command. 以下示例创建名为“myScaleSet”的规模集,并生成 SSH 密钥(如果不存在):The following example creates a scale set named myScaleSet, and generates SSH keys if they do not exist:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

创建和配置所有的规模集资源和 VM 实例需要几分钟时间。It takes a few minutes to create and configure all the scale set resources and VM instances. 若要将流量分配到单独的 VM 实例,则还要创建负载均衡器。To distribute traffic to the individual VM instances, a load balancer is also created.

查看规模集中的 VM 实例View the VM instances in a scale set

若要查看规模集中的 VM 实例的列表,请使用 az vmss list-instances,如下所示:To view a list of VM instances in a scale set, use az vmss list-instances as follows:

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

以下示例输出显示了规模集中的两个 VM 实例:The following example output shows two VM instances in the scale set:

  InstanceId  LatestModelApplied    Location    Name          ProvisioningState    ResourceGroup    VmId
------------  --------------------  ----------  ------------  -------------------  ---------------  ------------------------------------
           1  True                  chinanorth      myScaleSet_1  Succeeded            MYRESOURCEGROUP  c059be0c-37a2-497a-b111-41272641533c
           3  True                  chinanorth      myScaleSet_3  Succeeded            MYRESOURCEGROUP  ec19e7a7-a4cd-4b24-9670-438f4876c1f9

输出中的第一个列显示 InstanceIdThe first column in the output shows an InstanceId. 若要查看特定 VM 实例的其他信息,请将 --instance-id 参数添加到 az vmss get-instance-viewTo view additional information about a specific VM instance, add the --instance-id parameter to az vmss get-instance-view. 以下示例查看 VM 实例 1 的相关信息:The following example views information about VM instance 1:

az vmss get-instance-view \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --instance-id 1

列出连接信息List connection information

系统将公共 IP 地址分配给负载均衡器,由后者将流量路由到各个 VM 实例。A public IP address is assigned to the load balancer that routes traffic to the individual VM instances. 默认情况下,会将网络地址转换 (NAT) 规则添加到 Azure 负载均衡器,由后者将远程连接流量转发给给定端口上的每个 VM。By default, Network Address Translation (NAT) rules are added to the Azure load balancer that forwards remote connection traffic to each VM on a given port. 若要连接到规模集中的 VM 实例,请创建一个可连接到已分配的公共 IP 地址和端口号的远程连接。To connect to the VM instances in a scale set, you create a remote connection to an assigned public IP address and port number.

若要列出规模集中连接到 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:

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

以下示例输出显示了实例名称、负载均衡器的公共 IP 地址,以及可以通过 NAT 规则将流量转发到其中的端口号:The following example output shows the instance name, public IP address of the load balancer, and port number that the NAT rules forward traffic to:

{
  "instance 1": "13.92.224.66:50001",
  "instance 3": "13.92.224.66:50003"
}

通过 SSH 连接到第一个 VM 实例。SSH to your first VM instance. 使用 -p 参数指定公共 IP 地址和端口号,如前述命令所示:Specify your public IP address and port number with the -p parameter, as shown from the preceding command:

ssh azureuser@13.92.224.66 -p 50001

登录到 VM 实例以后,可以根据需要执行一些手动配置更改。Once logged in to the VM instance, you could perform some manual configuration changes as needed. 现在,请按正常方式关闭 SSH 会话:For now, close the SSH session as normal:

exit

了解 VM 实例映像Understand VM instance images

在教程开头创建规模集时,为 VM 实例指定的 --imageUbuntuLTSWhen you created a scale set at the start of the tutorial, a --image of UbuntuLTS was specified for the VM instances. Azure 市场包括许多可用于创建 VM 实例的映像。The Azure marketplace includes many images that can be used to create VM instances. 若要查看最常用映像的列表,请使用 az vm image list 命令。To see a list of the most commonly used images, use the az vm image list command.

az vm image list --output table

以下示例输出显示 Azure 上的最常用 VM 映像。The following example output shows the most common VM images on Azure. 可以使用 UrnAlias 在创建规模集时指定一个最常用的映像。The UrnAlias can be used to specify one of these common images when you create a scale set.

Offer          Publisher               Sku                 Urn                                                             UrnAlias             Version
-------------  ----------------------  ------------------  --------------------------------------------------------------  -------------------  ---------
CentOS         OpenLogic               7.3                 OpenLogic:CentOS:7.3:latest                                     CentOS               latest
CoreOS         CoreOS                  Stable              CoreOS:CoreOS:Stable:latest                                     CoreOS               latest
Debian         credativ                8                   credativ:Debian:8:latest                                        Debian               latest
openSUSE-Leap  SUSE                    42.2                SUSE:openSUSE-Leap:42.2:latest                                  openSUSE-Leap        latest
RHEL           RedHat                  7.3                 RedHat:RHEL:7.3:latest                                          RHEL                 latest
SLES           SUSE                    12-SP2              SUSE:SLES:12-SP2:latest                                         SLES                 latest
UbuntuServer   Canonical               16.04-LTS           Canonical:UbuntuServer:16.04-LTS:latest                         UbuntuLTS            latest
WindowsServer  MicrosoftWindowsServer  2016-Datacenter     MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest     Win2016Datacenter    latest
WindowsServer  MicrosoftWindowsServer  2012-R2-Datacenter  MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest  Win2012R2Datacenter  latest
WindowsServer  MicrosoftWindowsServer  2012-Datacenter     MicrosoftWindowsServer:WindowsServer:2012-Datacenter:latest     Win2012Datacenter    latest
WindowsServer  MicrosoftWindowsServer  2008-R2-SP1         MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest         Win2008R2SP1         latest

若要查看完整列表,请添加 --all 参数。To view a full list, add the --all argument. 还可以按 --publisher--offer 筛选映像列表。The image list can also be filtered by --publisher or --offer. 以下示例从列表中筛选出了产品/服务与“CentOS”相匹配的所有映像:In the following example, the list is filtered for all images with an offer that matches CentOS:

az vm image list --offer CentOS --all --output table

以下浓缩版输出显示了一些可用的 CentOS 7.3 映像:The following condensed output shows some of the CentOS 7.3 images available:

Offer    Publisher   Sku   Urn                                 Version
-------  ----------  ----  ----------------------------------  -------------
CentOS   OpenLogic   7.3   OpenLogic:CentOS:7.3:7.3.20161221   7.3.20161221
CentOS   OpenLogic   7.3   OpenLogic:CentOS:7.3:7.3.20170421   7.3.20170421
CentOS   OpenLogic   7.3   OpenLogic:CentOS:7.3:7.3.20170517   7.3.20170517
CentOS   OpenLogic   7.3   OpenLogic:CentOS:7.3:7.3.20170612   7.3.20170612
CentOS   OpenLogic   7.3   OpenLogic:CentOS:7.3:7.3.20170707   7.3.20170707
CentOS   OpenLogic   7.3   OpenLogic:CentOS:7.3:7.3.20170925   7.3.20170925

若要部署使用特定映像的规模集,请使用“Urn”列中的值。To deploy a scale set that uses a specific image, use the value in the Urn column. 指定映像时,可将映像版本号替换为 latest,以便选择最新的发行版。When you specify the image, the image version number can be replaced with latest, which selects the latest version of the distribution. 在以下示例中,--image 参数用于指定最新版本的 CentOS 7.3 映像。In the following example, the --image argument is used to specify the latest version of a CentOS 7.3 image.

重要

建议使用最新版本的映像。We recommend using the latest image version. 指定“latest”以使用部署时可用的最新版本的映像。Specify 'latest' to use the latest version of an image available at deploy time. 请注意,即使使用的是“latest”,VM 映像部署后也不会自动更新,即使新版本可用也是如此。Note, even if you use 'latest', the VM image will not automatically update after deploy time even if a new version becomes available.

由于只需数分钟即可创建和配置所有的规模集资源和 VM 实例,因此不需部署以下规模集:As it takes a few minutes to create and configure all the scale set resources and VM instances, you don't have to deploy the following scale set:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSetCentOS \
  --image OpenLogic:CentOS:7.3:latest \
  --admin-user azureuser \
  --generate-ssh-keys

了解 VM 实例大小Understand VM instance sizes

VM 实例大小或 SKU 决定了可供 VM 实例使用的计算资源(如 CPU、GPU 和内存)的量。A VM instance size, or SKU, determines the amount of compute resources such as CPU, GPU, and memory that are made available to the VM instance. 需要根据预期的工作负荷适当调整规模集中 VM 实例的大小。VM instances in a scale set need to be sized appropriately for the expected work load.

VM 实例大小VM instance sizes

下表将常用 VM 大小按类别分成了多个用例。The following table categorizes common VM sizes into use cases.

类型Type 常见大小Common sizes 说明Description
常规用途General purpose Dsv3、Dv3、DSv2、Dv2、DS、D、Av2、A0-7Dsv3, Dv3, DSv2, Dv2, DS, D, Av2, A0-7 CPU 与内存之比均衡。Balanced CPU-to-memory. 适用于开发/测试、小到中型应用程序和数据解决方案。Ideal for dev / test and small to medium applications and data solutions.
计算优化Compute optimized Fs, FFs, F 高 CPU 与内存之比。High CPU-to-memory. 适用于中等流量的应用程序、网络设备和批处理。Good for medium traffic applications, network appliances, and batch processes.
内存优化Memory optimized Esv3、Ev3、M、GS、G、DSv2、DS、Dv2、DEsv3, Ev3, M, GS, G, DSv2, DS, Dv2, D 较高的内存核心比。High memory-to-core. 适用于关系数据库、中到大型缓存和内存中分析。Great for relational databases, medium to large caches, and in-memory analytics.
GPUGPU NV, NCNV, NC 专门针对大量图形绘制和视频编辑的 VM。Specialized VMs targeted for heavy graphic rendering and video editing.

查找可用的 VM 实例大小Find available VM instance sizes

若要查看在特定区域可用的 VM 实例大小的列表,请使用 az vm list-sizes 命令。To see a list of VM instance sizes available in a particular region, use the az vm list-sizes command.

az vm list-sizes --location chinanorth --output table

输出类似于以下浓缩版示例,其中显示了分配给每个 VM 大小的资源:The output is similar to the following condensed example, which shows the resources assigned to each VM size:

  MaxDataDiskCount    MemoryInMb  Name                      NumberOfCores    OsDiskSizeInMb    ResourceDiskSizeInMb
------------------  ------------  ----------------------  ---------------  ----------------  ----------------------
                 4          3584  Standard_DS1_v2                       1           1047552                    7168
                 8          7168  Standard_DS2_v2                       2           1047552                   14336
[...]
                 1           768  Standard_A0                           1           1047552                   20480
                 2          1792  Standard_A1                           1           1047552                   71680
[...]
                 4          2048  Standard_F1                           1           1047552                   16384
                 8          4096  Standard_F2                           2           1047552                   32768
[...]
                24         57344  Standard_NV6                          6           1047552                   38912
                48        114688  Standard_NV12                        12           1047552                  696320

创建特定 VM 实例大小的规模集Create a scale set with a specific VM instance size

在教程开头创建规模集时,为 VM 实例提供了默认 VM SKU Standard_D1_v2When you created a scale set at the start of the tutorial, a default VM SKU of Standard_D1_v2 was provided for the VM instances. 可以根据 az vm list-sizes 的输出指定其他 VM 实例大小。You can specify a different VM instance size based on the output from az vm list-sizes. 以下示例会使用 --vm-sku 参数创建一个规模集,以便指定 VM 实例大小 Standard_F1The following example would create a scale set with the --vm-sku parameter to specify a VM instance size of Standard_F1. 由于只需数分钟即可创建和配置所有的规模集资源和 VM 实例,因此不需部署以下规模集:As it takes a few minutes to create and configure all the scale set resources and VM instances, you don't have to deploy the following scale set:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSetF1Sku \
  --image UbuntuLTS \
  --vm-sku Standard_F1 \
  --admin-user azureuser \
  --generate-ssh-keys

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

在教程开头创建规模集时,默认部署了两个 VM 实例。When you created a scale set at the start of the tutorial, two VM instances were deployed by default. 可以使用 az vmss create 来指定 --instance-count 参数,以便更改通过规模集创建的实例数。You can specify the --instance-count parameter with az vmss create to change the number of instances created with a scale set. 若要增加或减少现有规模集中的 VM 实例数,可以手动更改容量。To increase or decrease the number of VM instances in your existing scale set, you can manually change the capacity. 规模集会创建或删除所需数量的 VM 实例,然后配置分发流量所需的负载均衡器。The scale set creates or removes the required number of VM instances, then configures the load balancer to distribute traffic.

若要手动增加或减少规模集中 VM 实例的数目,请使用 az vmss scaleTo manually increase or decrease the number of VM instances in the scale set, use az vmss scale. 以下示例将规模集中 VM 实例的数目设置为 3The following example sets the number of VM instances in your scale set to 3:

az vmss scale \
    --resource-group myResourceGroup \
    --name myScaleSet \
    --new-capacity 3

更新规模集容量需要花费数分钟。If takes a few minutes to update the capacity of your scale set. 若要查看规模集中目前包含的实例数,请使用 az vmss show 并对 sku.capacity 进行查询:To see the number of instances you now have in the scale set, use az vmss show and query on sku.capacity:

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

常见管理任务Common management tasks

现在可以创建规模集、列出连接信息以及连接到 VM 实例。You can now create a scale set, list connection information, and connect to VM instances. 你已经了解如何对 VM 实例使用其他 OS 映像、如何选择其他 VM 大小,或者如何手动缩放实例数。You learned how you could use a different OS image for your VM instances, select a different VM size, or manually scale the number of instances. 在日常管理过程中,可能需要停止、启动或重启规模集中的 VM 实例。As part of day to day management, you may need to stop, start, or restart the VM instances in your scale set.

停止和解除分配规模集中的 VM 实例Stop and deallocate VM instances in a scale set

若要在规模集中停止一个或多个 VM 实例,请使用 az vmss stopTo stop one or more VM instances in a scale set, use az vmss stop. 可以通过 --instance-ids 参数指定一个或多个需要停止的 VM 实例。The --instance-ids parameter allows you to specify one or more VM instances to stop. 若不指定实例 ID,则会停止规模集中的所有 VM 实例。If you do not specify an instance ID, all VM instances in the scale set are stopped. 以下示例停止实例 1The following example stops instance 1:

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

已停止的 VM 实例会保留已分配状态,且会继续产生计算费用。Stopped VM instances remain allocated and continue to incur compute charges. 如果希望 VM 实例保持已解除分配状态,且仅会产生存储费用,请使用 az vmss deallocateIf you instead wish the VM instances to be deallocated and only incur storage charges, use az vmss deallocate. 以下示例停止和解除分配实例 1The following example stops and deallocates instance 1:

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

启动规模集中的 VM 实例Start VM instances in a scale set

若要在规模集中启动一个或多个 VM 实例,请使用 az vmss startTo start one or more VM instances in a scale set, use az vmss start. 可以通过 --instance-ids 参数指定一个或多个需要启动的 VM 实例。The --instance-ids parameter allows you to specify one or more VM instances to start. 若不指定实例 ID,则会启动规模集中的所有 VM 实例。If you do not specify an instance ID, all VM instances in the scale set are started. 以下示例启动实例 1The following example starts instance 1:

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

重启规模集中的 VM 实例Restart VM instances in a scale set

若要在规模集中重启一个或多个 VM 实例,请使用 az vmss restartTo restart one or more VM instances in a scale set, use az vmss restart. 可以通过 --instance-ids 参数指定一个或多个需要重启的 VM 实例。The --instance-ids parameter allows you to specify one or more VM instances to restart. 若不指定实例 ID,则会重启规模集中的所有 VM 实例。If you do not specify an instance ID, all VM instances in the scale set are restarted. 以下示例重启实例 1The following example restarts instance 1:

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

清理资源Clean up resources

删除资源组时,也会删除其中包含的所有资源,例如 VM 实例、虚拟网络和磁盘。When you delete a resource group, all resources contained within, such as the VM instances, virtual network, and disks, are also deleted. --no-wait 参数会使光标返回提示符处,不会等待操作完成。The --no-wait parameter returns control to the prompt without waiting for the operation to complete. --yes 参数将确认是否希望删除资源,不会显示询问是否删除的额外提示。The --yes parameter confirms that you wish to delete the resources without an additional prompt to do so.

az group delete --name myResourceGroup --no-wait --yes

后续步骤Next steps

本教程介绍了如何使用 Azure CLI 执行一些基本的规模集创建和管理任务:In this tutorial, you learned how to perform some basic scale set creation and management tasks with the Azure CLI:

  • 创建和连接虚拟机规模集Create and connect to a virtual machine scale set
  • 选择并使用 VM 映像Select and use VM images
  • 查看和使用特定 VM 大小View and use specific VM sizes
  • 手动缩放规模集Manually scale a scale set
  • 执行常见的规模集管理任务Perform common scale set management tasks

请转到下一教程,了解规模集磁盘。Advance to the next tutorial to learn about scale set disks.