在 Azure 门户中自动缩放虚拟机规模集Automatically scale a virtual machine scale set in the Azure portal

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

本文演示如何在 Azure 门户中创建自动缩放规则,以监视规模集中 VM 实例的性能。This article shows you how to create autoscale rules in the Azure portal that monitor the performance of the VM instances in your scale set. 这些缩放规则根据性能指标增加或减少 VM 实例数。These autoscale rules increase or decrease the number of VM instances in response to these performance metrics. 也可使用 Azure PowerShellAzure CLI 完成这些步骤。You can also complete these steps with Azure PowerShell or the Azure CLI.

先决条件Prerequisites

需要现有虚拟机规模集,才能创建自动缩放规则。To create autoscale rules, you need an existing virtual machine scale set. 可使用 Azure 门户Azure PowerShellAzure CLI 创建规模集。You can create a scale set with the Azure portal, Azure PowerShell, or Azure CLI.

创建规则,以便自动横向扩展Create a rule to automatically scale 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.

  1. 打开 Azure 门户,然后在仪表板的左侧菜单中选择“资源组”。Open the Azure portal and select Resource groups from the menu on the left-hand side of the dashboard.

  2. 选择包含其规模集的资源组,然后从资源列表中选择该规模集。Select the resource group that contains your scale set, then choose your scale set from the list of resources.

  3. 在规模集窗口的左侧菜单中,选择“缩放”。Choose Scaling from the menu on the left-hand side of the scale set window. 选择此按钮“启用自动缩放”:Select the button to Enable autoscale:

    在 Azure 门户中启用自动缩放

  4. 为设置输入名称(例如“自动缩放”),然后选择“添加规则”选项。Enter a name for your settings, such as autoscale, then select the option to Add a rule.

  5. 当平均 CPU 负载在十分钟内均超过 70% 时,请创建规则以增加规模集中的 VM 实例数。Let's create a rule that increases the number of VM instances in a scale set when the average CPU load is greater than 70% over a 10-minute period. 触发规则时,VM 实例数增加 20%。When the rule triggers, the number of VM instances is increased by 20%. 在 VM 实例数较少的规模集中,可将“操作”设置为“增加计数”,然后指定 1 或 2 作为实例计数 。In scale sets with a small number of VM instances, you could set the Operation to Increase count by and then specify 1 or 2 for the Instance count. 在有大量 VM 实例的规模集中,增加 10% 或 20% 的 VM 实例可能更合适。In scale sets with a large number of VM instances, an increase of 10% or 20% VM instances may be more appropriate.

    为规则指定以下设置:Specify the following settings for your rule:

    参数Parameter 说明Explanation Value
    时间聚合Time Aggregation 定义如何聚合已收集的指标以便分析。Defines how the collected metrics should be aggregated for analysis. 平均值Average
    指标名称Metric Name 监视和应用规模集操作的性能指标。The performance metric to monitor and apply scale set actions on. CPU 百分比Percentage CPU
    时间粒度统计信息Time grain statistic 定义如何聚合每个时间粒度中收集的指标,以便进行分析。Defines how the collected metrics in each time grain should be aggregated for analysis. 平均值Average
    “运算符”Operator 用于比较指标数据和阈值的运算符。Operator used to compare the metric data against the threshold. 大于Greater than
    阈值Threshold 使自动缩放规则触发操作的百分比。The percentage that causes the autoscale rule to trigger an action. 7070
    DurationDuration 比较指标与阈值之前监视的时长。The amount of time monitored before the metric and threshold values are compared. 不包含冷却时间段。Does not include cool down period. 10 分钟10 minutes
    操作Operation 定义应用规则时应纵向扩展还是缩减规模集,以及扩展或缩减的量。Defines if the scale set should scale up or down when the rule applies and by what increment. 增加百分比Increase percent by
    实例计数Instance count 规则触发时,应更改 VM 实例的百分比。The percentage of VM instances should be changed when the rule triggers. 2020
    冷却(分钟)Cool down (minutes) 为使自动缩放操作有时间生效,再次应用规则前需要等待的时间。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

    以下示例演示在 Azure 门户中创建的规则,这些规则可与以下设置相匹配:The following examples show a rule created in the Azure portal that matches these settings:

    创建自动缩放规则,以增加 VM 实例数

  6. 若要创建规则,请选择“添加”To create the rule, select Add

创建规则,以便自动横向缩减Create a rule to automatically scale 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.

  1. 再次选择“添加规则”。Choose to Add a rule again.

  2. 创建规则,在平均 CPU 负载在十分钟内下降至 30% 以下时减少规模集中的 VM 实例数。Create a rule that decreases the number of VM instances in a scale set when the average CPU load then drops below 30% over a 10-minute period. 触发规则后,VM 实例数减少 20%。When the rule triggers, the number of VM instances is decreased by 20%.

    使用与上一规则相同的方法。Use the same approach as with the previous rule. 为规则调整以下设置:Adjust the following settings for your rule:

    参数Parameter 说明Explanation Value
    “运算符”Operator 用于比较指标数据和阈值的运算符。Operator used to compare the metric data against the threshold. 小于Less than
    阈值Threshold 使自动缩放规则触发操作的百分比。The percentage that causes the autoscale rule to trigger an action. 3030
    操作Operation 定义应用规则时应纵向扩展还是缩减规模集,以及扩展或缩减的量Defines if the scale set should scale up or down when the rule applies and by what increment 降低百分比Decrease percent by
    实例计数Instance count 规则触发时,应更改 VM 实例的百分比。The percentage of VM instances should be changed when the rule triggers. 2020
  3. 若要创建规则,请选择“添加”To create the rule, select Add

定义自动缩放实例限制Define autoscale instance limits

自动缩放文件必须定义最小、最大和默认的 VM 实例数。Your autoscale profile must define a minimum, maximum, and default number of VM instances. 应用自动缩放规则时,这些实例限制可确保不会横向扩展到最大实例数以外,或横向缩减到最小实例数以内。When your autoscale rules are applied, these instance limits make sure that you do not scale out beyond the maximum number of instances, or scale in beyond the minimum of instances.

  1. 设置以下实例限制:Set the following instance limits:

    最小值Minimum 最大值Maximum 默认Default
    22 1010 22
  2. 若要应用自动缩放规则和实例限制,请选择“保存”。To apply your autoscale rules and instance limits, select Save.

监视规模集中的实例数Monitor number of instances in a scale set

若要查看 VM 实例的数量和状态,请在规模集窗口的左侧菜单中选择“实例”。To see the number and status of VM instances, select Instances from the menu on the left-hand side of the scale set window. 状态指示是随规模集自动横向扩展而创建 VM 实例,还是随规模集自动缩减而删除 VM 实例 。The status indicates if the VM instance is Creating as the scale set automatically scales out, or is Deleting as the scale automatically scales in.

查看规模集 VM 实例的列表

按计划自动缩放Autoscale based on a schedule

前述示例通过 CPU 使用情况等基本主机指标自动横向扩展或缩减规模集。The previous examples automatically scaled a scale set in or out with basic host metrics such as CPU usage. 还可以根据计划创建自动缩放规则。You can also create autoscale rules based on schedules. 通过这些基于计划的规则,可在应用程序需求按预期增加(如核心工作时间)之前自动横向扩展 VM 实例数,在预期需求减少时(例如周末)自动横向缩减实例数。These schedule-based rules allow you to automatically scale out the number of VM instances ahead of an anticipated increase in application demand, such as core work hours, and then automatically scale in the number of instances at a time that you anticipate less demand, such as the weekend.

  1. 在规模集窗口的左侧菜单中,选择“缩放”。Choose Scaling from the menu on the left-hand side of the scale set window. 若要删除以前示例中创建的现有自动缩放规则,请选择回收站图标。To delete the existing autoscale rules created in the previous examples, choose the trash can icon.

    删除现有的自动缩放规则

  2. 选择“添加缩放条件”。Choose to Add a scale condition. 选择规则名称旁的铅笔图标,并为其提供一个名称,例如“在每个工作日横向扩展”。Select the pencil icon next to rule name, and provide a name such as Scale out during each work day.

    重命名默认的自动缩放规则

  3. 选择“单选按钮”,缩放到特定的实例计数。Select the radio button to Scale to a specific instance count.

  4. 若要纵向扩展实例数,请输入“10”作为实例计数。To scale up the number of instances, enter 10 as the instance count.

  5. 为计划类型选择“在特定日期重复” 。Choose Repeat specific days for the Schedule type.

  6. 选择所有工作日,即星期一到星期五。Select all the work days, Monday through Friday.

  7. 选择适当的时区,然后将“开始时间”指定为“09:00”。Choose the appropriate timezone, then specify a Start time of 09:00.

  8. 再次选择“添加缩放条件”。Choose to Add a scale condition again. 重复此过程,创建名为“在夜间横向缩减”的计划,该计划在每天 18:00 开始将规模集缩减到 3 个实例 。Repeat the process to create a schedule named Scale in during the evening that scales to 3 instances, repeats every weekday, and starts at 18:00.

  9. 若要应用基于计划的自动缩放规则,请选择“保存”。To apply your schedule-based autoscale rules, select Save.

    创建按计划进行缩放的自动缩放规则

若要查看如何应用自动缩放规则,请通过顶部的缩放窗口选择“运行历史记录” 。To see how your autoscale rules are applied, select Run history across the top of the Scaling window. 图和事件列表显示触发自动缩放规则的时间和规模集中增加或减少的 VM 实例数。The graph and events list shows when the autoscale rules trigger and the number of VM instances in your scale set increases or decreases.

后续步骤Next steps

有关如何管理 VM 实例的信息,请参阅使用 Azure PowerShell 管理虚拟机规模集For information on how to manage your VM instances, see Manage virtual machine scale sets with Azure PowerShell.

若要了解如何在触发自动缩放规则时生成警报,请参阅在 Azure Monitor 中使用自动缩放操作发送电子邮件和 Webhook 警报通知To learn how to generate alerts when your autoscale rules trigger, see Use autoscale actions to send email and webhook alert notifications in Azure Monitor. 还可以在 Azure Monitor 中使用审核日志发送电子邮件和 Webhook 警报通知You can also Use audit logs to send email and webhook alert notifications in Azure Monitor.