了解自动缩放设置Understand Autoscale settings

使用自动缩放设置有助于确保运行适当数量的资源来处理应用程序负载的波动。Autoscale settings help ensure that you have the right amount of resources running to handle the fluctuating load of your application. 可将自动缩放设置配置为基于指标(指示负载或性能)触发,或者在计划好的日期和时间触发。You can configure Autoscale settings to be triggered based on metrics that indicate load or performance, or triggered at a scheduled date and time. 本文将会深度剖析自动缩放设置。This article takes a detailed look at the anatomy of an Autoscale setting. 本文首先介绍设置的架构和属性,然后逐步讲解可配置的不同配置文件类型。The article begins with the schema and properties of a setting, and then walks through the different profile types that can be configured. 最后讨论 Azure 中的自动缩放功能如何评估要在任意给定时间执行哪个配置文件。Finally, the article discusses how the Autoscale feature in Azure evaluates which profile to execute at any given time.

自动缩放设置的架构Autoscale setting schema

为了演示自动缩放设置的架构,本文使用了以下自动缩放设置。To illustrate the Autoscale setting schema, the following Autoscale setting is used. 请务必注意,此自动缩放设置包含:It is important to note that this Autoscale setting has:

  • 一个配置文件。One profile.
  • 此配置文件包含两条指标规则:一条规则针对横向扩展,另一条规则针对横向缩减。Two metric rules in this profile: one for scale out, and one for scale in.
    • 当虚拟机规模集的平均 CPU 百分比指标在过去 10 分钟大于 85% 时,将触发扩展规则。The scale-out rule is triggered when the virtual machine scale set's average percentage CPU metric is greater than 85 percent for the past 10 minutes.
    • 当虚拟机规模集的平均值在过去 1 分钟小于 60% 时,将触发缩减规则。The scale-in rule is triggered when the virtual machine scale set's average is less than 60 percent for the past minute.

备注

一项设置可以包含多个配置文件。A setting can have multiple profiles. 有关详细信息,请参阅配置文件部分。To learn more, see the profiles section. 还可以在一个配置文件中定义多个扩展规则和缩减规则。A profile can also have multiple scale-out rules and scale-in rules defined. 有关评估方法,请参阅评估部分。To see how they are evaluated, see the evaluation section.

{
  "id": "/subscriptions/s1/resourceGroups/rg1/providers/microsoft.insights/autoscalesettings/setting1",
  "name": "setting1",
  "type": "Microsoft.Insights/autoscaleSettings",
  "location": "China East",
  "properties": {
    "enabled": true,
    "targetResourceUri": "/subscriptions/s1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1",
    "profiles": [
      {
        "name": "mainProfile",
        "capacity": {
          "minimum": "1",
          "maximum": "4",
          "default": "1"
        },
        "rules": [
          {
            "metricTrigger": {
              "metricName": "Percentage CPU",
              "metricResourceUri": "/subscriptions/s1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1",
              "timeGrain": "PT1M",
              "statistic": "Average",
              "timeWindow": "PT10M",
              "timeAggregation": "Average",
              "operator": "GreaterThan",
              "threshold": 85
            },
            "scaleAction": {
              "direction": "Increase",
              "type": "ChangeCount",
              "value": "1",
              "cooldown": "PT5M"
            }
          },
    {
            "metricTrigger": {
              "metricName": "Percentage CPU",
              "metricResourceUri": "/subscriptions/s1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1",
              "timeGrain": "PT1M",
              "statistic": "Average",
              "timeWindow": "PT10M",
              "timeAggregation": "Average",
              "operator": "LessThan",
              "threshold": 60
            },
            "scaleAction": {
              "direction": "Decrease",
              "type": "ChangeCount",
              "value": "1",
              "cooldown": "PT5M"
            }
          }
        ]
      }
    ]
  }
}
部分Section 元素名称Element name 说明Description
设置Setting IDID 自动缩放设置的资源 ID。The Autoscale setting's resource ID. 自动缩放设置属于 Azure 资源管理器资源。Autoscale settings are an Azure Resource Manager resource.
设置Setting namename 自动缩放设置的名称。The Autoscale setting name.
设置Setting locationlocation 自动缩放设置的位置。The location of the Autoscale setting. 此位置可与缩放的资源所在的位置不同。This location can be different from the location of the resource being scaled.
propertiesproperties targetResourceUritargetResourceUri 缩放的资源的资源 ID。The resource ID of the resource being scaled. 针对每个资源,只能使用一项自动缩放设置。You can only have one Autoscale setting per resource.
propertiesproperties 配置文件profiles 自动缩放设置由一个或多个配置文件组成。An Autoscale setting is composed of one or more profiles. 自动缩放引擎每次运行时,将会执行一个配置文件。Each time the Autoscale engine runs, it executes one profile.
个人资料profile namename 配置文件的名称。The name of the profile. 可以选择有助于识别该配置文件的任何名称。You can choose any name that helps you identify the profile.
个人资料profile Capacity.maximumCapacity.maximum 允许的最大容量。The maximum capacity allowed. 此值可以确保自动缩放在执行此配置文件时,不会将资源扩展到超过此数字。It ensures that Autoscale, when executing this profile, does not scale your resource above this number.
个人资料profile Capacity.minimumCapacity.minimum 允许的最小容量。The minimum capacity allowed. 此值可以确保自动缩放在执行此配置文件时,不会将资源缩减到低于此数字。It ensures that Autoscale, when executing this profile, does not scale your resource below this number.
个人资料profile Capacity.defaultCapacity.default 如果读取资源指标(在本例中,为“vmss1”的 CPU)时出现问题,且当前容量低于默认容量,则自动缩放将扩展到默认值。If there is a problem reading the resource metric (in this case, the CPU of “vmss1”), and the current capacity is below the default, Autoscale scales out to the default. 这是为了确保资源可用性。This is to ensure the availability of the resource. 如果当前容量已大于默认容量,则自动缩放不会横向缩减。If the current capacity is already higher than the default capacity, Autoscale does not scale in.
个人资料profile 规则rules 自动缩放使用配置文件中的规则在最大和最小容量之间自动缩放。Autoscale automatically scales between the maximum and minimum capacities, by using the rules in the profile. 可以在一个配置文件中包含多个规则。You can have multiple rules in a profile. 通常包含两个规则:一个用于确定何时横向扩展,另一个用于确定何时横向缩减。Typically there are two rules: one to determine when to scale out, and the other to determine when to scale in.
规则 (rule)rule metricTriggermetricTrigger 定义规则的指标条件。Defines the metric condition of the rule.
metricTriggermetricTrigger metricNamemetricName 指标的名称。The name of the metric.
metricTriggermetricTrigger metricResourceUrimetricResourceUri 发出指标的资源的资源 ID。The resource ID of the resource that emits the metric. 在大多数情况下,它与缩放的资源相同。In most cases, it is the same as the resource being scaled. 在某些情况下,它可能不同。In some cases, it can be different. 例如,可以基于存储队列中的消息数缩放虚拟机规模集。For example, you can scale a virtual machine scale set based on the number of messages in a storage queue.
metricTriggermetricTrigger timeGraintimeGrain 指标采样持续时间。The metric sampling duration. 例如,TimeGrain = "PT1M" 表示应使用 statistic 元素中指定的聚合方法每分钟聚合一次指标。For example, TimeGrain = “PT1M” means that the metrics should be aggregated every 1 minute, by using the aggregation method specified in the statistic element.
metricTriggermetricTrigger statisticstatistic timeGrain 时间段内的聚合方法。The aggregation method within the timeGrain period. 例如,statistic = "Average"timeGrain = "PT1M" 表示每分钟取平均值来聚合指标。For example, statistic = “Average” and timeGrain = “PT1M” means that the metrics should be aggregated every 1 minute, by taking the average. 此属性规定指标的采样方式。This property dictates how the metric is sampled.
metricTriggermetricTrigger timeWindowtimeWindow 查找指标的时间范围。The amount of time to look back for metrics. 例如,timeWindow = "PT10M" 表示自动缩放每次运行时,都会查询过去 10 分钟的指标。For example, timeWindow = “PT10M” means that every time Autoscale runs, it queries metrics for the past 10 minutes. 使用该时间范围可将指标规范化,避免对暂时性的峰值作出反应。The time window allows your metrics to be normalized, and avoids reacting to transient spikes.
metricTriggermetricTrigger timeAggregationtimeAggregation 用于聚合已采样指标的聚合方法。The aggregation method used to aggregate the sampled metrics. 例如,如果 TimeAggregation = "Average" ,则应取平均值来聚合采样的指标。For example, TimeAggregation = “Average” should aggregate the sampled metrics by taking the average. 上例取 10 个 1 分钟样本并求其平均值。In the preceding case, take the ten 1-minute samples, and average them.
规则 (rule)rule scaleActionscaleAction 触发规则的 metricTrigger 时要执行的操作。The action to take when the metricTrigger of the rule is triggered.
scaleActionscaleAction directiondirection “Increase”表示横向扩展,“Decrease”表示横向缩减。"Increase" to scale out, or "Decrease" to scale in.
scaleActionscaleAction value 要将资源容量增大或减小多少。How much to increase or decrease the capacity of the resource.
scaleActionscaleAction cooldowncooldown 在执行缩放操作之后、再次执行缩放操作之前所要等待的时间。The amount of time to wait after a scale operation before scaling again. 例如,如果 cooldown = "PT10M" ,则自动缩放只会在 10 分钟之后才尝试再次执行缩放。For example, if cooldown = “PT10M”, Autoscale does not attempt to scale again for another 10 minutes. 在添加或删除实例之后,cooldown(冷却)可让指标变稳定。The cooldown is to allow the metrics to stabilize after the addition or removal of instances.

自动缩放配置文件Autoscale profiles

有三种类型的自动缩放配置文件:There are three types of Autoscale profiles:

  • 常规配置文件: 最常见的配置文件。Regular profile: The most common profile. 如果不需要根据星期日期或特定的日历日期以不同的方式缩放资源,则可以使用常规配置文件。If you don’t need to scale your resource based on the day of the week, or on a particular day, you can use a regular profile. 然后,可以使用规定何时横向扩展及何时横向缩减的指标规则,来配置此配置文件。This profile can then be configured with metric rules that dictate when to scale out and when to scale in. 只应定义一个常规配置文件。You should only have one regular profile defined.

    本文前面使用的示例配置文件就是一个常规配置文件。The example profile used earlier in this article is an example of a regular profile. 请注意,还可以将配置文件设置为缩放到资源的静态实例计数。Note that it is also possible to set a profile to scale to a static instance count for your resource.

  • 固定日期配置文件: 此配置文件适用于特殊场合。Fixed date profile: This profile is for special cases. 假设你要在 2017 年 12 月 26 日(太平洋标准时间)举办一场重要活动。For example, let’s say you have an important event coming up on December 26, 2017 (PST). 你希望当天的最小和最大资源容量有所不同,但仍可根据相同的指标缩放。You want the minimum and maximum capacities of your resource to be different on that day, but still scale on the same metrics. 在这种情况下,应在设置的配置文件列表中添加一个固定日期配置文件。In this case, you should add a fixed date profile to your setting’s list of profiles. 该配置文件配置为只在活动当天运行。The profile is configured to run only on the event’s day. 在其他任何日期,自动缩放将使用常规配置文件。For any other day, Autoscale uses the regular profile.

    "profiles": [{
    "name": " regularProfile",
    "capacity": {
    ...
    },
    "rules": [{
    ...
    },
    {
    ...
    }]
    },
    {
    "name": "eventProfile",
    "capacity": {
    ...
    },
    "rules": [{
    ...
    }, {
    ...
    }],
    "fixedDate": {
        "timeZone": "Pacific Standard Time",
               "start": "2017-12-26T00:00:00",
               "end": "2017-12-26T23:59:00"
    }}
    ]
    
  • 重复配置文件: 使用此类配置文件可以确保始终在特定的星期日期使用此配置文件。Recurrence profile: This type of profile enables you to ensure that this profile is always used on a particular day of the week. 重复配置文件只包含开始时间。Recurrence profiles only have a start time. 它们会一直运行到下一个重复配置文件或固定日期配置文件设置为启动为止。They run until the next recurrence profile or fixed date profile is set to start. 只包含一个重复配置文件的自动缩放设置只会运行该配置文件,即使相同的设置中定义了常规配置文件。An Autoscale setting with only one recurrence profile runs that profile, even if there is a regular profile defined in the same setting. 以下两个示例演示了此配置文件的用法:The following two examples illustrate how this profile is used:

    示例 1:工作日与周末Example 1: Weekdays vs. weekends

    假设你希望周末的最大容量为 4。Let’s say that on weekends, you want your maximum capacity to be 4. 在工作日,由于预期的负载更大,你希望最大容量为 10。On weekdays, because you expect more load, you want your maximum capacity to be 10. 在这种情况下,可在设置中包含两个重复配置文件,一个在周末运行,另一个在工作日运行。In this case, your setting would contain two recurrence profiles, one to run on weekends and the other on weekdays. 设置如下所示:The setting looks like this:

    "profiles": [
    {
    "name": "weekdayProfile",
    "capacity": {
        ...
    },
    "rules": [{
        ...
    }],
    "recurrence": {
        "frequency": "Week",
        "schedule": {
            "timeZone": "Pacific Standard Time",
            "days": [
                "Monday"
            ],
            "hours": [
                0
            ],
            "minutes": [
                0
            ]
        }
    }}
    },
    {
    "name": "weekendProfile",
    "capacity": {
        ...
    },
    "rules": [{
        ...
    }]
    "recurrence": {
        "frequency": "Week",
        "schedule": {
            "timeZone": "Pacific Standard Time",
            "days": [
                "Saturday"
            ],
            "hours": [
                0
            ],
            "minutes": [
                0
            ]
        }
    }
    }]
    

    上面的设置显示,每个重复配置文件都包含一个计划。The preceding setting shows that each recurrence profile has a schedule. 此计划确定配置文件开始运行的时间。This schedule determines when the profile starts running. 需要运行另一个配置文件时,上述配置文件会停止执行。The profile stops when it’s time to run another profile.

    例如,在上面的设置中,“weekdayProfile”设置为在星期一午夜 12 点启动。For example, in the preceding setting, “weekdayProfile” is set to start on Monday at 12:00 AM. 这意味着,此配置文件将在星期一午夜 12 点开始运行。That means this profile starts running on Monday at 12:00 AM. 它会持续到星期六午夜 12 点,即“weekendProfile”计划开始运行的时间。It continues until Saturday at 12:00 AM, when “weekendProfile” is scheduled to start running.

    示例 2 - 营业时间Example 2: Business hours

    假设你要在营业时间上午 9 点到下午 5 点使用一个指标阈值,并其他所有时间使用另一个指标阈值。Let's say you want to have one metric threshold during business hours (9:00 AM to 5:00 PM), and a different one for all other times. 该设置如下所示:The setting would look like this:

    "profiles": [
    {
    "name": "businessHoursProfile",
    "capacity": {
        ...
    },
    "rules": [{
        ...
    }],
    "recurrence": {
        "frequency": "Week",
        "schedule": {
            "timeZone": "Pacific Standard Time",
            "days": [
                "Monday", “Tuesday”, “Wednesday”, “Thursday”, “Friday”
            ],
            "hours": [
                9
            ],
            "minutes": [
                0
            ]
        }
    }
    },
    {
    "name": "nonBusinessHoursProfile",
    "capacity": {
        ...
    },
    "rules": [{
        ...
    }]
    "recurrence": {
        "frequency": "Week",
        "schedule": {
            "timeZone": "Pacific Standard Time",
            "days": [
                "Monday", “Tuesday”, “Wednesday”, “Thursday”, “Friday”
            ],
            "hours": [
                17
            ],
            "minutes": [
                0
            ]
        }
    }
    }]
    

    在上面的设置中,“businessHoursProfile”在星期一上午 9 点开始运行,并持续到下午 5 点。The preceding setting shows that “businessHoursProfile” begins running on Monday at 9:00 AM, and continues to 5:00 PM. 下午 5 点是“nonBusinessHoursProfile”开始运行的时间。That’s when “nonBusinessHoursProfile” starts running. “nonBusinessHoursProfile”运行到星期二上午 9 点,然后接着运行“businessHoursProfile”。The “nonBusinessHoursProfile” runs until 9:00 AM Tuesday, and then the “businessHoursProfile” takes over again. 此配置文件重复到星期五下午 5 点。This repeats until Friday at 5:00 PM. 然后,“nonBusinessHoursProfile”一直运行到星期一上午 9 点。At that point, “nonBusinessHoursProfile” runs all the way to Monday at 9:00 AM.

备注

Azure 门户中的自动缩放用户界面针对重复配置文件强制实施结束时间,在重复配置文件之间开始运行自动缩放设置的默认配置文件。The Autoscale user interface in the Azure portal enforces end times for recurrence profiles, and begins running the Autoscale setting's default profile in between recurrence profiles.

自动缩放评估Autoscale evaluation

由于自动缩放设置可以包含多个配置文件,并且每个配置文件可以包含多个指标规则,因此,必须了解如何评估自动缩放设置。Given that Autoscale settings can have multiple profiles, and each profile can have multiple metric rules, it is important to understand how an Autoscale setting is evaluated. 每当自动缩放作业运行时,首先会选择适用的配置文件。Each time the Autoscale job runs, it begins by choosing the profile that is applicable. 然后,自动缩放将评估该配置文件中的最小、最大值和任何指标规则,并确定是否需要执行缩放操作。Then Autoscale evaluates the minimum and maximum values, and any metric rules in the profile, and decides if a scale action is necessary.

自动缩放会选择哪个配置文件?Which profile will Autoscale pick?

自动缩放根据以下顺序选择配置文件:Autoscale uses the following sequence to pick the profile:

  1. 首先查找配置为立即运行的任何固定日期配置文件。It first looks for any fixed date profile that is configured to run now. 如果有此类文件,则自动缩放会运行该配置文件。If there is, Autoscale runs it. 如果存在多个需要运行的固定日期配置文件,自动缩放将选择第一个。If there are multiple fixed date profiles that are supposed to run, Autoscale selects the first one.
  2. 如果不存在固定日期配置文件,自动缩放会查找重复配置文件。If there are no fixed date profiles, Autoscale looks at recurrence profiles. 如果找到了重复配置文件,则运行它。If a recurrence profile is found, it runs it.
  3. 如果没有固定日期配置文件或重复配置文件,则自动缩放会运行常规配置文件。If there are no fixed date or recurrence profiles, Autoscale runs the regular profile.

自动缩放如何评估多个规则?How does Autoscale evaluate multiple rules?

自动缩放确定要运行哪个配置文件之后,会评估该配置文件中的所有扩展规则(设置为 direction = “Increase” 的规则)。After Autoscale determines which profile to run, it evaluates all the scale-out rules in the profile (these are rules with direction = “Increase”).

如果触发了一个或多个扩展规则,自动缩放将计算其中每个规则的 scaleAction 确定的新容量。If one or more scale-out rules are triggered, Autoscale calculates the new capacity determined by the scaleAction of each of those rules. 然后,它会扩展到这些容量的最大值,以确保服务可用性。Then it scales out to the maximum of those capacities, to ensure service availability.

例如,假设某个虚拟机规模集的当前容量为 10。For example, let's say there is a virtual machine scale set with a current capacity of 10. 有两条扩展规则:一个规则将容量增大 10%,另一个将容量增大 3%。There are two scale-out rules: one that increases capacity by 10 percent, and one that increases capacity by 3 counts. 第一条规则会导致新容量更改为 11,第二条规导致容量更改为 13。The first rule would result in a new capacity of 11, and the second rule would result in a capacity of 13. 为了确保服务可用性,自动缩放会选择可以产生最大容量的操作,因此选择了第二条规则。To ensure service availability, Autoscale chooses the action that results in the maximum capacity, so the second rule is chosen.

如果未触发扩展规则,自动缩放会评估所有缩减规则(设置为 direction = “Decrease” 的规则)。If no scale-out rules are triggered, Autoscale evaluates all the scale-in rules (rules with direction = “Decrease”). 如果触发了所有缩减规则,自动缩放只会执行某个缩减操作。Autoscale only takes a scale-in action if all of the scale-in rules are triggered.

自动缩放计算其中每个规则的 scaleAction 确定的新容量。Autoscale calculates the new capacity determined by the scaleAction of each of those rules. 然后,选择可产生其中最大容量的缩放操作,以确保服务可用性。Then it chooses the scale action that results in the maximum of those capacities to ensure service availability.

例如,假设某个虚拟机规模集的当前容量为 10。For example, let's say there is a virtual machine scale set with a current capacity of 10. 有两条缩减规则:一个规则将容量减小 50%,另一个将容量减小 3%。There are two scale-in rules: one that decreases capacity by 50 percent, and one that decreases capacity by 3 counts. 第一条规则会导致新容量更改为 5,第二条规导致容量更改为 7。The first rule would result in a new capacity of 5, and the second rule would result in a capacity of 7. 为了确保服务可用性,自动缩放会选择可以产生最大容量的操作,因此选择了第二条规则。To ensure service availability, Autoscale chooses the action that results in the maximum capacity, so the second rule is chosen.

后续步骤Next steps

若要详细了解自动缩放,请参阅以下文章:Learn more about Autoscale by referring to the following: