教程:使用 Azure CLI 自动缩放虚拟机规模集Tutorial: Automatically scale a virtual machine scale set with the Azure CLI

创建规模集时,可定义想运行的 VM 实例数。When you create a scale set, you define the number of VM instances that you wish to run. 若应用程序需要更改,可自动增加或减少 VM 实例数。As your application demand changes, you can automatically increase or decrease the number of VM instances. 通过自动缩放功能,可随客户需求的改变而进行调整,或在应用的整个生命周期内响应应用程序性能更改。The ability to autoscale lets you keep up with customer demand or respond to application performance changes throughout the lifecycle of your app. 本教程介绍如何执行下列操作:In this tutorial you learn how to:

  • 对规模集使用自动缩放Use autoscale with a scale set
  • 创建和使用自动缩放规则Create and use autoscale rules
  • 对 VM 实例进行压力测试并触发自动缩放规则Stress-test VM instances and trigger autoscale rules
  • 在需求下降时自动横向缩减Autoscale back in as demand is reduced

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

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.32 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.32 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 scale set

使用 az group create 创建资源组,如下所示:Create a resource group with az group create as follows:

az group create --name myResourceGroup --location chinanorth2

现在,使用 az vmss create 创建虚拟机规模集。Now create a virtual machine scale set with az vmss create. 以下示例创建实例计数为 2 的规模集,并生成 SSH 密钥(如果不存在):The following example creates a scale set with an instance count of 2, and generates SSH keys if they do not exist:

az vmss create \
  --resource-group myResourceGroup \
  --name myScaleSet \
  --image UbuntuLTS \
  --upgrade-policy-mode automatic \
  --instance-count 2 \
  --admin-username azureuser \
  --generate-ssh-keys

定义自动缩放配置文件Define an autoscale profile

若要在规模集上启用自动缩放,首先要定义自动缩放配置文件。To enable autoscale on a scale set, you first define an autoscale profile. 此配置文件定义默认、最小和最大规模集容量。This profile defines the default, minimum, and maximum scale set capacity. 这些限制可让你通过不继续创建 VM 实例来控制成本,并可使用缩小事件中保留的最小数量的实例均衡可接受的性能。These limits let you control cost by not continually creating VM instances, and balance acceptable performance with a minimum number of instances that remain in a scale-in event. 使用 az monitor autoscale create 创建自动缩放配置文件。Create an autoscale profile with az monitor autoscale create. 以下示例设置了默认值,以及最小容量 2 个 VM 实例、最大容量 10 个 VM :The following example sets the default, and minimum, capacity of 2 VM instances, and a maximum of 10:

az monitor autoscale create \
  --resource-group myResourceGroup \
  --resource myScaleSet \
  --resource-type Microsoft.Compute/virtualMachineScaleSets \
  --name autoscale \
  --min-count 2 \
  --max-count 10 \
  --count 2

创建规则,以便自动横向扩展Create a rule to autoscale out

如果应用程序需求提高,规模集中 VM 实例上的负载将会增大。If your application demand increases, the load on the VM instances in your scale set increases. 如果这种负载增大持续稳定,而不只是短暂的需求,那么可以配置自动缩放规则来增加规模集中的 VM 实例数。If this increased load is consistent, rather than just a brief demand, you can configure autoscale rules to increase the number of VM instances in the scale set. 创建这些 VM 实例并部署应用程序后,规模集会开始通过负载均衡器将流量分配到这些实例和应用程序。When these VM instances are created and your applications are deployed, the scale set starts to distribute traffic to them through the load balancer. 可以控制要监视的指标(例如 CPU 或磁盘)、应用程序负载必须处于给定阈值内的时间,以及要添加到规模集的 VM 实例数。You control what metrics to monitor, such as CPU or disk, how long the application load must meet a given threshold, and how many VM instances to add to the scale set.

让我们使用 az monitor autoscale rule create 创建一个规则,当平均 CPU 负载在 5 分钟内超过 70% 时,该规则会增加规模集中的 VM 实例数。Let's create a rule with az monitor autoscale rule create that increases the number of VM instances in a scale set when the average CPU load is greater than 70% over a 5-minute period. 触发规则时,VM 实例数增加 3。When the rule triggers, the number of VM instances is increased by three.

az monitor autoscale rule create \
  --resource-group myResourceGroup \
  --autoscale-name autoscale \
  --condition "Percentage CPU > 70 avg 5m" \
  --scale out 3

创建规则,以便自动横向缩减Create a rule to autoscale in

在夜间或周末,应用程序需求可能会降低。On an evening or weekend, your application demand may decrease. 如果这种负载降低在一段时间内持续稳定,可以配置自动缩放规则来减少规模集中的 VM 实例数。If this decreased load is consistent over a period of time, you can configure autoscale rules to decrease the number of VM instances in the scale set. 这种横向缩减操作可以减少运行规模集所需的成本,因为只运行满足当前需求所需的实例数。This scale-in action reduces the cost to run your scale set as you only run the number of instances required to meet the current demand.

让我们使用 az monitor autoscale rule create 创建另一个规则,当平均 CPU 负载随后在 5 分钟内低于 30% 时,该规则会减少规模集中的 VM 实例数。Create another rule with az monitor autoscale rule create that decreases the number of VM instances in a scale set when the average CPU load then drops below 30% over a 5-minute period. 以下示例定义将 VM 实例数横向缩减 1 的规则:The following example defines the rule to scale in the number of VM instances by one:

az monitor autoscale rule create \
  --resource-group myResourceGroup \
  --autoscale-name autoscale \
  --condition "Percentage CPU < 30 avg 5m" \
  --scale in 1

在规模集上生成 CPU 负载Generate CPU load on scale set

若要测试自动缩放规则,请在规模集的 VM 实例上生成一些 CPU 负载。To test the autoscale rules, generate some CPU load on the VM instances in the scale set. 这种模拟的 CPU 负载会导致自动缩放以横向扩展的方式增加 VM 实例数。This simulated CPU load causes the autoscales to scale out and increase the number of VM instances. 随着模拟的 CPU 负载下降,自动缩放规则会进行横向缩减,减少 VM 实例数。As the simulated CPU load is then decreased, the autoscale rules scale in and reduce the number of VM instances.

首先,请使用 az vmss list-instance-connection-info 列出用于连接到规模集中的 VM 实例的地址和端口:First, list the address and ports to connect to VM instances in a scale set with 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 Network Address Translation (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 own public IP address and port number with the -p parameter, as shown from the preceding command:

ssh azureuser@13.92.224.66 -p 50001

登录后,安装 stress 实用工具。Once logged in, install the stress utility. 启动 10 个生成 CPU 负载的 stress 辅助角色。Start 10 stress workers that generate CPU load. 这些辅助角色运行 420 秒,此时间足以让自动缩放规则实施所需的操作。These workers run for 420 seconds, which is enough to cause the autoscale rules to implement the desired action.

sudo apt-get update
sudo apt-get -y install stress
sudo stress --cpu 10 --timeout 420 &

stress 显示类似于 stress: info: [2688] dispatching hogs:10 cpu, 0 io, 0 vm, 0 hdd 的输出时,按 Enter 键返回到提示符。When stress shows output similar to stress: info: [2688] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd, press the Enter key to return to the prompt.

若要确认 stress 是否生成了 CPU 负载,请使用 top 实用工具检查活动的系统负载:To confirm that stress generates CPU load, examine the active system load with the top utility:

top

退出 top,然后关闭与 VM 实例的连接。Exit top, then close your connection to the VM instance. stress 继续在 VM 实例上运行。stress continues to run on the VM instance.

Ctrl-c
exit

连接到第二个 VM 实例,所使用的端口号是前面的 az vmss list-instance-connection-info 列出的:Connect to second VM instance with the port number listed from the previous az vmss list-instance-connection-info:

ssh azureuser@13.92.224.66 -p 50003

安装并运行 stress,然后在这第二个 VM 实例上启动十个辅助角色。Install and run stress, then start ten workers on this second VM instance.

sudo apt-get -y install stress
sudo stress --cpu 10 --timeout 420 &

stress 再次显示类似于 stress: info: [2713] dispatching hogs:10 cpu, 0 io, 0 vm, 0 hdd 的输出时,按 Enter 键返回到提示符。Again, when stress shows output similar to stress: info: [2713] dispatching hogs: 10 cpu, 0 io, 0 vm, 0 hdd, press the Enter key to return to the prompt.

关闭与第二个 VM 实例的连接。Close your connection to the second VM instance. stress 继续在 VM 实例上运行。stress continues to run on the VM instance.

exit

监视活动的自动缩放规则Monitor the active autoscale rules

若要监视规模集中的 VM 实例数,请使用 watchTo monitor the number of VM instances in your scale set, use watch. 自动缩放规则需要 5 分钟的时间才能开始横向扩展过程,以便响应由每个 VM 实例上的 stress 生成的 CPU 负载:It takes 5 minutes for the autoscale rules to begin the scale-out process in response to the CPU load generated by stress on each of the VM instances:

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

达到 CPU 阈值以后,自动缩放规则会增加规模集中的 VM 实例数。Once the CPU threshold has been met, the autoscale rules increase the number of VM instances in the scale set. 以下输出显示,在规模集进行自动横向扩展时创建了 3 个 VM:The following output shows three VMs created as the scale set autoscales out:

Every 2.0s: az vmss list-instances --resource-group myResourceGroup --name myScaleSet --output table

  InstanceId  LatestModelApplied    Location    Name          ProvisioningState    ResourceGroup    VmId
------------  --------------------  ----------  ------------  -------------------  ---------------  ------------------------------------
           1  True                  chinanorth2      myScaleSet_1  Succeeded            myResourceGroup  4f92f350-2b68-464f-8a01-e5e590557955
           2  True                  chinanorth2      myScaleSet_2  Succeeded            myResourceGroup  d734cd3d-fb38-4302-817c-cfe35655d48e
           4  True                  chinanorth2      myScaleSet_4  Creating             myResourceGroup  061b4c90-0d73-49fc-a066-19eab0b3d95c
           5  True                  chinanorth2      myScaleSet_5  Creating             myResourceGroup  4beff8b9-4e65-40cb-9652-43899309da27
           6  True                  chinanorth2      myScaleSet_6  Creating             myResourceGroup  9e4133dd-2c57-490e-ae45-90513ce3b336

stress 在初始 VM 实例上停止后,平均 CPU 负载会回到正常。Once stress stops on the initial VM instances, the average CPU load returns to normal. 另一个 5 分钟后,自动缩放规则会横向缩减 VM 实例数。After another 5 minutes, the autoscale rules then scale in the number of VM instances. 横向缩减操作会首先删除 ID 值最高的 VM 实例。Scale in actions remove VM instances with the highest IDs first. 如果规模集使用可用性集,则缩减操作将均匀分布到这些 VM 实例上。When a scale set uses Availability Sets, scale in actions are evenly distributed across those VM instances. 以下示例输出显示,在规模集进行自动横向缩减时删除了一个 VM 实例:The following example output shows one VM instance deleted as the scale set autoscales in:

           6  True                  chinanorth2      myScaleSet_6  Deleting             myResourceGroup  9e4133dd-2c57-490e-ae45-90513ce3b336

使用 Ctrl-c 退出 watchExit watch with Ctrl-c. 规模集继续每 5 分钟横向缩减一次,每次删除一个 VM 实例,直至达到最小实例计数 2。The scale set continues to scale in every 5 minutes and remove one VM instance until the minimum instance count of two is reached.

清理资源Clean up resources

若要删除规模集和其他资源,请使用 az group delete 删除资源组及其所有资源。To remove your scale set and additional resources, delete the resource group and all its resources with az group delete. --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 --yes --no-wait

后续步骤Next steps

本教程介绍了如何使用 Azure CLI 自动进行规模集的横向缩减或扩展:In this tutorial, you learned how to automatically scale in or out a scale set with the Azure CLI:

  • 对规模集使用自动缩放Use autoscale with a scale set
  • 创建和使用自动缩放规则Create and use autoscale rules
  • 对 VM 实例进行压力测试并触发自动缩放规则Stress-test VM instances and trigger autoscale rules
  • 在需求下降时自动横向缩减Autoscale back in as demand is reduced