This topic describes how to use Auto Scaling to scale Elastic Compute Service (ECS) instances in an efficient manner.

Background information

This topic provides examples on how to use Auto Scaling to scale ECS instances. Company A is used in this topic. The operations required to scale ECS instances vary based on business scenarios. The following table describes the requirements and operations required in different business scenarios.

Business scenarioRequirementProcedure
Stable demand Requirement 1: At least two ECS instances of the ecs.g7.large instance type must be prepared to meet daily business requirements.
  1. Step 1: Create a scaling group
  2. Step 2: Create a scaling configuration
Unpredictable increase in demand
  • Requirement 2: One ECS instance of the ecs.g7.large instance type must be prepared for unpredictable increase in demand.
  • Requirement 3: To reduce resource costs, the ECS instance that is prepared for unpredictable increase in demand must be removed when the demand drops.
  1. Step 1: Create a scaling group
  2. Step 2: Create a scaling configuration
  3. Step 3: Create scaling rules
  4. Step 4: Create event-triggered tasks
Predictable increase in demand from 18:00 to 22:00 every day
  • Requirement 4: One ECS instance of the ecs.g7.large instance type must be added for predictable increase in demand.
  • Requirement 5: To reduce resource costs, the ECS instance that is added for predictable increase in demand must be removed during off-peak hours.
  1. Step 1: Create a scaling group
  2. Step 2: Create a scaling configuration
  3. Step 3: Create scaling rules
  4. Step 5: Create scheduled tasks

Preparations

The first time you use Auto Scaling, you must make the following preparations:

  • The AliyunServiceRoleForAutoScaling role is created.
  • If you use Auto Scaling as a RAM user, you must attach the AliyunESSFullAccess policy to the RAM user.

For more information, see Manage the service-linked role for Auto Scaling.

Step 1: Create a scaling group

A scaling group is a group of instances that can meet your business requirements. Scaling group is the core unit in Auto Scaling. Auto Scaling automatically adds instances to or removes instances from scaling groups.

  1. Log on to the Auto Scaling console.
  2. In the left-side navigation pane, click Scaling Groups.
    In this example, the operations are performed on the Scaling Groups page. If you do not have Auto Scaling resources in the region that you specify, a welcome page is displayed after you log on to the Auto Scaling console. Follow the on-screen instructions to create Auto Scaling resources.
  3. In the top navigation bar, select the region where Auto Scaling is activated.
  4. In the upper-left corner of the Scaling Groups page, click Create.
  5. Configure parameters of the scaling group and click OK.
    Figure - Create scaling groups

    The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.

    ParameterExampleDescription
    Scaling Group NameEnter MyFirstScalingGroup.None
    TypeSet the value to ECS.If you set the Type parameter to ECS, the scaling group contains only ECS instances.
    Instance Configuration SourceSet the value to Create from Scratch.After the scaling group is created, you must create a scaling configuration.
    Minimum Number of InstancesSet the value to 2.

    The scaling group must contain at least two ECS instances to meet Requirement 1 of Company A.

    If the number of ECS instances in the scaling group is less than two, Auto Scaling automatically creates an instance to meet the required minimum number of instances.

    Maximum Number of InstancesSet the value to 5.The scaling group can contain up to five ECS instances. Auto Scaling automatically removes excess instances from the scaling group to help Company A reduce costs.
    Network TypeSet the value to VPC.When you create a scaling configuration, you can select only the instance types that support virtual private clouds (VPCs).
    VPCSelect vpc-bp1jmxxau0lur929p****.The VPC and vSwitch of the scaling group that you want to create. For information about how to create a VPC and a vSwitch, see Create a VPC with an IPv4 CIDR block.
    vSwitchSelect vsw-bp155oak33mqngov2****.
  6. In the Create Scaling Group dialog box, click OK.

Step 2: Create a scaling configuration

A scaling configuration is a template that defines instance configuration information such as the instance type, disk, public IP address, and security group. The template can be used to create ECS instances during scale-outs. Auto Scaling uses this scaling configuration to create ECS instances with the same configurations.

  1. Find a scaling group and use one of the following methods to go to the scaling group details page:
    • Click the ID of the scaling group in the Scaling Group Name/ID column.
    • Click Details in the Actions column.
  2. In the upper part of the page, click the Instance Configuration Sources tab.
  3. On the Scaling Configurations tab, click Create Scaling Configuration.
  4. Follow the on-screen instructions to complete the configurations. In the Preview step, confirm the configurations and click Create.
    Figure - Scaling configuration

    The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.

    StepParameterExampleDescription
    Basic ConfigurationsBilling MethodSet the value to Pay-as-you-go.Auto Scaling is free of charge. However, you are charged for the ECS instances that are added to the scaling group based on the pricing of ECS. For more information, see Billing overview.
    Instance Configuration ModeSet the value to Specify Instance Type.You can select Specify Instance Type or Intelligent Configuration.
    Instance TypeSelect ecs.g7.large.The computing power that is provided by the instance.
    ImageSet the value to Public Image and specify CentOS 7.6 64-bit.After the instance is started, the OS and application data of the image are copied to the system disk. Valid values: Public Image, Custom Image, Shared Image, and Marketplace Image.
    StorageSet the System Disk parameter to Enhanced SSD (ESSD) and specify 40 GiB of memory.Select a system disk or one or more data disks for ECS instances to store data.
    Public IP AddressSelect Assign Public IPv4 Address. By default, the Bandwidth Billing parameter is set to Pay-By-Traffic. Set the peak bandwidth to 1 Mbit/s. You are charged for outbound traffic to the Internet. The peak bandwidth is 1 Mbit/s.
    Security GroupSelect sg-bp18kz60mefsicfg****.Select the security group that you prepared in advance. For information about how to create a security group, see Create a security group.
    System Configurations (Optional)Logon CredentialsSet the value to Set Later.After ECS instances are created, you can specify passwords for the ECS instances.
    PreviewScaling Configuration NameEnter MyFirstScalingConfiguration.None
  5. In the The scaling configuration is created message, click Enable Configuration.
  6. In the Enable Scaling Configuration message, click OK.

    In a scaling group, you must enable one scaling configuration. After you enable the scaling configuration, the scaling configuration is in the Active state.

  7. In the Enable Scaling Group message, click OK.

    You must enable the scaling group. Auto Scaling can scale ECS instances only in scaling groups that are in the Enabled state.

    Note In this example, Auto Scaling scales out two ECS instances to meet Requirement 1 of Company A. For information about how to view the instances in a scaling group, see View the instances in a scaling group.

Step 3: Create scaling rules

A scaling rule specifies the action that adds instances to or removes instances from a scaling group. The scaling rule can also help you determine the required minimum or maximum number of instances in a scaling group.

In this example, the following scaling rules are created to meet the business requirements of Company A:

  • Scale-out rule: adds one ECS instance.
  • Scale-in rule: removes one ECS instance.

After you create the preceding scaling rules, you can trigger scaling activities. You can manually execute scaling rules to trigger scaling activities. You can also create scheduled tasks or event-triggered tasks to allow Auto Scaling to execute scaling rules.

  1. In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab.
  2. On the Scaling Rules tab, create a scale-out rule.
    1. In the upper-left corner of the Scaling Rules tab, click Create Scaling Rule.
    2. In the Create Scaling Rule dialog box, configure parameters of the scaling rule and click OK.
      Figure - Create a scaling rule

      The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.

      ParameterExampleDescription
      Rule NameEnter Add1.None
      Rule TypeSet the value to Simple Scaling Rule.You can specify a simple scaling rule based on which Auto Scaling scales instances in the scaling group or change the number of instances to a specific number.
      OperationSet the value to Add 1 Instances.One ECS instance is added to the scaling group during a scale-out.
  3. Create a scale-in rule for the scaling group.
    1. In the upper-left corner of the Scaling Rules tab, click Create Scaling Rule.
    2. In the Create Scaling Rule dialog box, configure parameters of the scaling rule and click OK.
      Figure - Create scaling group 2The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.
      ParameterExampleDescription
      Rule NameEnter Remove1.None
      Rule TypeSet the value to Simple Scaling Rule.You can specify a simple scaling rule based on which Auto Scaling scales instances in the scaling group or changes the number of instances to a specific number.
      OperationSet the value to Remove 1 Instances.One ECS instance is removed from the scaling group during a scale-in.

Step 4: Create event-triggered tasks

Event-triggered tasks monitor specific metrics to collect metric values in real time. When metric values meet the alert conditions, alerts are triggered to execute specified scaling rules. In this example, the following event-triggered tasks are created to meet Requirement 2 and Requirement 3 of Company A:

  • To meet Requirement 2, create an event-triggered task named EventTriggeredECIScalingOut. When the average CPU utilization reaches or exceeds 70%, Auto Scaling automatically executes the scale-out rule to add one ECS instance to the scaling group.
  • To meet Requirement 3, create an event-triggered task named EventTriggeredECIScalingIn. When the average CPU utilization reaches or drops below 20%, Auto Scaling automatically executes the scale-in rule to remove one ECS instance from the scaling group.
  1. In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab, and then click the Scheduled and Event-triggered Tasks tab.
  2. Click the Event-triggered Tasks (System) tab.
    In this example, system metrics are used. If you click the Event-triggered Tasks (Custom) tab, you must publish your custom metrics to CloudMonitor before you create an event-triggered task. For more information, see Custom monitoring event-triggered tasks.
  3. Create an event-triggered task for a scale-out.
    1. Click Create Event-triggered Task.
    2. Configure parameters of the event-triggered task and click OK.
      Figure - Create an event-triggered task for scale-out

      The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.

      ParameterExampleDescription
      NameEnter EventTriggeredScalingOut.None
      DescriptionAdds one ECS instance when the average CPU utilization reaches or exceeds 70%.None
      Resource MonitoredSet the value to asg-bp1g75opkec1fq2y****.This parameter specifies the ID of the scaling group whose metrics you want to monitor. In this example, the scaling group is MyFirstScalingGroup.
      Monitoring TypeSet the value to System Monitoring.The default value is System Monitoring. You do not need to configure this parameter.
      Alert Condition(ECS) CPU Utilization(ECS) CPU Utilization>=70%Specify one or more alert conditions that must be met before an alert is triggered. You must specify at least one metric. If you want to specify multiple metrics, click Add Metric.
      Take note of the following items:
      • Metric: the name of the system metric that you want to monitor. For example, if you use the (ECS) CPU Utilization metric, the CPU utilization of all ECS instances in the scaling group is monitored.
      • Statistical method: the method that you want to use to determine whether the metric value exceeds the specified threshold. You can use Average, Maximum, or Minimum as the statistical method. For example, if you use the Average method and specify a rule which is Average >= 70%, an alert is triggered when the average CPU utilization of all ECS instances in the scaling group reaches or exceeds 70%.
      Report Alerts WhenSet the value to All metrics meet the condition.You can set this parameter to All metrics meet the condition or One of the metrics meets the condition.
      Statistical PeriodSet the value to 1 Minute.Auto Scaling collects, summarizes, and compares data based on the specified statistical period. The shorter the period, the more frequently alerts are triggered. Configure the statistical period based on your business requirements. Data is collected once every minute.
      Triggered AfterSet the value to 3 Times.If the average CPU utilization of all ECS instances in the scaling group reaches or exceeds 70% for three consecutive times, an alert is triggered.
      Effective PeriodSet the value to Not Set.The effective period of the event-triggered task. During the effective period, Auto Scaling executes the scaling rule that is specified in the event-triggered task after an alert is triggered. If an alert is triggered beyond the effective period, Auto Scaling does not execute the scaling rule.
      • Not Set: The event-triggered task is effective at all times.
      • Cron Expression: The event-triggered task is effective only within the time range that is specified by the cron expression. For information about cron expressions, see Cron expression.
      Trigger RuleSelect Add1.When the alert is triggered, Auto Scaling executes the scaling rule that adds one ECS instance to the scaling group.
  4. Create an event-triggered task for a scale-in.
    1. Click Create Event-triggered Task.
    2. Configure parameters of the event-triggered task and click OK.
      Figure - Event-triggered task for scale-in

      The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.

      ParameterExampleDescription
      NameEnter EventTriggeredScalingIn.None
      DescriptionRemoves one ECS instance from the scaling group when the average CPU utilization reaches or drops below 20%.None
      Resource MonitoredSet the value to asg-bp1g75opkec1fq2y****.This parameter specifies the ID of the scaling group whose metrics you want to monitor. In this example, the scaling group is MyFirstScalingGroup.
      Monitoring TypeSet the value to System Monitoring.The default value is System Monitoring. You do not need to configure this parameter.
      Alert Condition(ECS) CPU Utilization > Average > <= > 20%Specify one or more alert conditions that must be met before an alert is triggered. You must specify at least one metric. If you want to specify multiple metrics, click Add Metric.
      Take note of the following items:
      • Metric: the name of the system metric that you want to monitor. For example, if you use the (ECS) CPU Utilization metric, the CPU utilization of all ECS instances in the scaling group is monitored.
      • Statistical method: the method that you want to use to determine whether the metric value exceeds the specified threshold. You can use Average, Maximum, or Minimum as the statistical method. For example, if you use the Average method and specify a rule which is Average <= 20%, an alert is triggered when the average CPU utilization of all ECS instances in the scaling group reaches or drops below 20%.
      Report Alerts WhenSet the value to All metrics meet the condition.You can set this parameter to All metrics meet the condition or One of the metrics meets the condition.
      Statistical PeriodSet the value to 1 Minute.Auto Scaling collects, summarizes, and compares data based on the specified statistical period. The shorter the period, the more frequently alerts are triggered. Configure the statistical period based on your business requirements. Data is collected once every minute.
      Triggered AfterSet the value to 3 Times.If the average CPU utilization of ECS instances in the scaling group reaches or drops below 20% for three consecutive times, an alert is triggered.
      Effective PeriodSet the value to Not Set.The effective period of the event-triggered task. During the effective period, Auto Scaling executes the scaling rule that is specified in the event-triggered task after an alert is triggered. If an alert is triggered beyond the effective period, Auto Scaling does not execute the scaling rule.
      • Not Set: The event-triggered task is effective at all times.
      • Cron Expression: The event-triggered task is effective only within the time range that is specified by the cron expression. For information about cron expressions, see Cron expression.
      Trigger RuleSelect Remove1.When the alert is triggered, Auto Scaling executes the scaling rule that removes one ECS instance from the scaling group.

Step 5: Create scheduled tasks

A scheduled task is a task that can execute a specified scaling rule at a specified point in time. In this example, the following scheduled tasks are created to meet Requirement 4 and Requirement 5:
  • To meet Requirement 4, create a scheduled task named ScheduledECIScalingOut for peak hours. This task triggers the scale-out rule to add one ECS instance to the scaling group.
  • To meet Requirement 5, create a scheduled task named ScheduledECIScalingIn for off-peak hours. This task triggers the scale-in rule to remove one ECS instance from the scaling group.
  1. In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab.
  2. Click the Scheduled and Event-triggered Tasks tab.
  3. On the Scheduled Tasks tab, create a scheduled task for a scale-out.
    1. Click Create Scheduled Task.
    2. Configure parameters of the scheduled task and click OK.
      Figure - Create a scheduled task for scale-out

      The following table describes the parameters used in this example. For the parameters that are not described in the following table, use the default settings.

      ParameterExampleDescription
      Task NameEnter ScheduledScalingOut.None
      DescriptionAdds one ECS instance at 17:55:00 every day.None
      Executed AtSet the value to Nov 1, 2021 5:55 PM.This parameter specifies the time at which the task is executed. In this example, the scheduled task is executed at 17:55:00 every day, which is five minutes before the peak hours.
      Scaling MethodSet the value to Select Existing Scaling Rule.None
      Simple Scaling RuleSelect Add1.This task triggers the scaling rule that adds one ECS instance to the scaling group.
      RecurrenceSet the value to By Day.A value of By Day specifies that the scheduled task is executed once a day. In this example, the scale-out rule is executed to add one ECS instance at 17:55:00 every day.
  4. On the Scheduled Task tab, create a scheduled task for a scale-in.
    1. Click Create Scheduled Task.
    2. Configure parameters of the scheduled task and click OK.
      Figure - Create scheduled task for a scale-in

      The following table describes the parameters that are used in this example. For parameters that are not described in the following table, use the default settings.

      ParameterExampleDescription
      Task NameEnter ScheduledScalingIn.None
      DescriptionRemoves one ECS instance from the scaling group at 22:05:00 every day.None
      Executed AtSet the value to Nov 1, 2021 10:05 PM.This parameter specifies the time at which the task is executed. In this example, the scheduled task is executed at 22:05:00 every day, which is five minutes after the peak hours.
      Scaling MethodSet the value to Select Existing Scaling Rule.None
      Simple Scaling RuleSelect Remove1.This task triggers the scaling rule that removes one ECS instance from the scaling group.
      RecurrenceSet the value to By Day.This scheduled task is executed once a day. In this example, the scale-in rule is executed to remove one ECS instance at 22:05:00 every day.

Result

Auto Scaling automatically scales ECS instances based on the preceding configurations to meet the following requirements:

  • After the scaling group is enabled, Auto Scaling automatically adds two ECS instances to meet Requirement 1.
  • If the average CPU utilization of the ECS instances in the scaling group reaches or exceeds 70%, Auto Scaling automatically adds one ECS instance to meet Requirement 2.
  • If the average CPU utilization of the ECS instances in the scaling group reaches or drops below 20%, Auto Scaling automatically removes one ECS instance from the scaling group to meet Requirement 3.
  • Auto Scaling automatically adds one ECS instance at 17:55:00 every day to meet Requirement 4.
  • Auto Scaling removes one ECS instance from the scaling group at 22:05:00 every day to meet Requirement 5.

On the Instances tab, you can view the ECS instances that are added. The configurations of the scaling configuration used by the scaling group are the same as the configurations of the ECS instances that are automatically created. For more information, see View the instances in a scaling group.

Figure - Instance List