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

创建规模集时,可定义想运行的 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.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.

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

请使用 Microsoft.insights/autoscalesettings 资源提供程序在 Azure 模板中定义一个自动缩放配置文件。You define an autoscale profile in an Azure template with the Microsoft.insights/autoscalesettings resource provider. 配置文件提供规模集容量以及任何关联规则的详细信息。A profile details the capacity of the scale set, and any associated rules. 下面的示例定义一个名为 Autoscale by percentage based on CPU usage 的配置文件,并设置 2 个 VM 实例的默认值、最小值、容量和最大值 (10):The following example defines a profile named Autoscale by percentage based on CPU usage and sets the default, and minimum, capacity of 2 VM instances, and a maximum of 10:

{
"type": "Microsoft.insights/autoscalesettings",
"name": "Autoscale",
"apiVersion": "2015-04-01",
"location": "[variables('location')]",
"scale": null,
"properties": {
  "profiles": [
    {
      "name": "Autoscale by percentage based on CPU usage",
      "capacity": {
        "minimum": "2",
        "maximum": "10",
        "default": "2"
      }
    }
  ]
}

定义规则,以便自动横向扩展Define 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.

以下示例中定义了一个规则:当平均 CPU 负载在 5 分钟内均超过 70% 时,就会增加规模集中的 VM 实例数。In the following example, a rule is defined 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.

此规则使用以下参数:The following parameters are used for this rule:

参数Parameter 说明Explanation ValueValue
metricNamemetricName 监视和应用规模集操作的性能指标。The performance metric to monitor and apply scale set actions on. CPU 百分比Percentage CPU
timeGraintimeGrain 为进行而收集指标分析的频率。How often the metrics are collected for analysis. 1 分钟1 minute
timeAggregationtimeAggregation 定义如何聚合已收集的指标以便分析。Defines how the collected metrics should be aggregated for analysis. 平均值Average
timeWindowtimeWindow 比较指标与阈值之前监视的时长。The amount of time monitored before the metric and threshold values are compared. 5 分钟5 minutes
operatoroperator 用于比较指标数据和阈值的运算符。Operator used to compare the metric data against the threshold. 大于Greater Than
thresholdthreshold 使自动缩放规则触发操作的值。The value that causes the autoscale rule to trigger an action. 70%70%
directiondirection 定义在应用规则的情况下,规模集应横向缩减还是扩展。Defines if the scale set should scale in or out when the rule applies. 增加Increase
typetype 表明 VM 实例数应该根据特定值进行更改。Indicates that the number of VM instances should be changed by a specific value. 更改计数Change Count
valuevalue 应用规则时应减少或增加多少 VM 实例。How many VM instances should be scaled in or out when the rule applies. 33
cooldowncooldown 为使自动缩放操作有时间生效,再次应用规则前需要等待的时间。The amount of time to wait before the rule is applied again so that the autoscale actions have time to take effect. 5 分钟5 minutes

以下规则将添加到上一部分中的 Microsoft.insights/autoscalesettings 资源提供程序的概要文件节:The following rule would be added into the profile section of the Microsoft.insights/autoscalesettings resource provider from the previous section:

"rules": [
  {
    "metricTrigger": {
      "metricName": "Percentage CPU",
      "metricNamespace": "",
      "metricResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/',  resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', variables('vmssName'))]",
      "metricResourceLocation": "[variables('location')]",
      "timeGrain": "PT1M",
      "statistic": "Average",
      "timeWindow": "PT5M",
      "timeAggregation": "Average",
      "operator": "GreaterThan",
      "threshold": 70
    },
    "scaleAction": {
      "direction": "Increase",
      "type": "ChangeCount",
      "value": "3",
      "cooldown": "PT5M"
    }
  }
]

定义规则,以便自动横向缩减Define 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.

以下示例定义了一个规则,以便在平均 CPU 负载在 5 分钟内下降至 30% 以下时横向缩减规模集中的 VM 实例数。The following example defines a rule to scale in the number of VM instances by one when the average CPU load then drops below 30% over a 5-minute period. 此规则将添加到自动缩放配置文件中用于自动横向扩展的前一个规则之后:This rule would be added to the autoscale profile after the previous rule to scale out:

{
  "metricTrigger": {
    "metricName": "Percentage CPU",
    "metricNamespace": "",
    "metricResourceUri": "[concat('/subscriptions/',subscription().subscriptionId, '/resourceGroups/',  resourceGroup().name, '/providers/Microsoft.Compute/virtualMachineScaleSets/', variables('vmssName'))]",
    "metricResourceLocation": "[variables('location')]",
    "timeGrain": "PT1M",
    "statistic": "Average",
    "timeWindow": "PT5M",
    "timeAggregation": "Average",
    "operator": "LessThan",
    "threshold": 30
  },
  "scaleAction": {
    "direction": "Decrease",
    "type": "ChangeCount",
    "value": "1",
    "cooldown": "PT5M"
  }
}

创建自动缩放规模集Create an autoscaling scale set

让我们使用示例模板创建一个规模集并应用自动缩放规则。Let's use a sample template to create a scale set and apply autoscale rules. 可以查看完整模板,或者参阅模板的 Microsoft.insights/autoscalesettings 资源提供程序节You can review the complete template, or see the Microsoft.insights/autoscalesettings resource provider section of the template.

首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在“chinanorth”位置创建名为“myResourceGroup”的资源组:The following example creates a resource group named myResourceGroup in the chinanorth location:

az group create --name myResourceGroup --location chinanorth

现在,使用 az group deployment create 创建虚拟机规模集。Now create a virtual machine scale set with az group deployment create. 出现提示时,请提供自己的、用作每个 VM 实例的凭据的用户名和密码,例如 azureuserWhen prompted, provide your own username, such as azureuser, and password that is used as the credentials for each VM instance:

az group deployment create \
  --resource-group myResourceGroup \
  --template-uri https://raw.githubusercontent.com/Azure-Samples/compute-automation-configurations/master/scale_sets/autoscale.json

创建和配置所有的规模集资源和 VM 需要几分钟时间。It takes a few minutes to create and configure all the scale set resources and VMs.

在规模集上生成 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 autoscale rules 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                  chinanorth      myScaleSet_1  Succeeded            MYRESOURCEGROUP  4f92f350-2b68-464f-8a01-e5e590557955
           2  True                  chinanorth      myScaleSet_2  Succeeded            MYRESOURCEGROUP  d734cd3d-fb38-4302-817c-cfe35655d48e
           4  True                  chinanorth      myScaleSet_4  Creating             MYRESOURCEGROUP  061b4c90-0d73-49fc-a066-19eab0b3d95c
           5  True                  chinanorth      myScaleSet_5  Creating             MYRESOURCEGROUP  4beff8b9-4e65-40cb-9652-43899309da27
           6  True                  chinanorth      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                  chinanorth      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:

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