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

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 different business scenarios. The following table describes the requirements and operations required in different business scenarios.

Business scenario Requirement Procedure
Stable demand Requirement 1: At least two ECS instances of the ecs.g7.large instance type are required 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 peak hours.
  • Requirement 3: To reduce resource costs, the ECS instance that is prepared for unpredictable peak hours 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 a scaling rule
  4. Step 4: Create an event-triggered task
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 before predictable peak hours.
  • Requirement 5: To reduce resource costs, the ECS instance that is added for predictable peak hours 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 a scaling rule
  4. Step 5: Create a scheduled task

Preparations

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

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

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

Step 1: Create a scaling group

A scaling group is a group of instances that can meet your business requirements. Scaling groups are 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 a region.
  4. In the upper-left corner of the Scaling Groups page, click Create.
  5. Configure the parameters for the scaling group and click OK.
    Scaling groups

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

    Parameter Example Description
    Scaling Group Name MyFirstScalingGroup None
    Type ECS Set Type to ECS. Then, the scaling group contains only ECS instances.
    Instance Configuration Source Create from Scratch After the scaling group is created, you must create a scaling configuration.
    Minimum Number of Instances 2

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

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

    Maximum Number of Instances 5 The scaling group can contain up to five ECS instances. Auto Scaling automatically removes the extra instances from the scaling group to help Company A minimize costs.
    Network Type VPC When you create a scaling configuration, you can select only the instance types that support virtual private clouds (VPCs).
    VPC vpc-bp1jmxxau0lur929p**** The VPC and vSwitch of the scaling group that you want to create. For information about how to create a VPC and vSwitch, see Create an IPv4 VPC.
    Select VSwitch 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 a scale-out activity. Auto Scaling uses this scaling configuration to create ECS instances with the same configurations.

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

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

    Configuration step Parameter Example Description
    Basic Configurations Billing Method Pay-As-You-Go Auto Scaling is free of charge. However, you must pay for the ECS instances that are added to the scaling group based on the pricing of ECS. For more information, see Billing overview.
    Instance Type ecs.g7.large The computing power provided by the instance.
    Image Public image: CentOS 7.6 64-bit After the instance is started, the operating system and application data of the image are copied to the system disk.
    Public IP Address Select Assign Public IPv4 Address. By default, pay-by-traffic is used. Adjust the peak bandwidth to 1 Mbit/s. You are charged for the outbound Internet traffic. The peak bandwidth is 1 Mbit/s.
    Security Group sg-bp18kz60mefsicfg**** Select the security group that you created. For information about how to create a security group, see Create a security group.
    System Configurations Logon Credentials Set Later After an ECS instance is created, you can manually specify the password for the ECS instance.
    Preview Scaling Configuration Name MyFirstScalingConfiguration None
  6. In the Success message, click Enable.
  7. 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.

  8. 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 creates two ECS instances to meet the business requirements of Company A. For information about how to view the instances in a scaling group, see View ECS instances.

Step 3: Create a scaling rule

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 two scaling rules are required to meet the business requirements:

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

After you create the scaling rules, you can select methods to trigger scaling activities based on the business requirements. You can manually execute scaling rules or allow Auto Scaling to automatically execute scaling rules based on scheduled tasks or event-triggered tasks.

  1. In the upper part of the page, click the Scaling Rules and Activities tab, and then click the Scaling Rules tab.
  2. Create a scaling rule that adds instances to 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 the parameters and click OK.
      Scaling rules

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

      Parameter Example Description
      Rule Name Add1 None
      Rule Type Simple Scaling Rule You can specify a simple scaling rule based on which Auto Scaling increases or decreases the number of instances in a scaling group, or a simple scaling rule to increase or decrease the number of instances to a specific number.
      Operation Add 1 Instances One ECS instance is added to the scaling group during a scale-out activity.
  3. Create a scaling rule that removes instances from 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 the parameters and click OK.
      Scaling rule 2The following table describes the settings used in this example. For parameters that are not described in the following table, use the default values.
      Parameter Example Description
      Rule Name Remove1 None
      Rule Type Simple Scaling Rule You can specify a simple scaling rule based on which Auto Scaling increases or decreases the number of instances in a scaling group, or a simple scaling rule to increase or decrease the number of instances to a specific number.
      Operation Remove 1 Instances One ECS instance is removed from the scaling group during a scale-in activity.

Step 4: Create an event-triggered task

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 required to meet requirement 2 and requirement 3:

  • To meet requirement 2, create an event-triggered task named EventTriggeredECIScalingOut. When the CPU utilization reaches or exceeds 70%, Auto Scaling automatically executes the scaling rule that adds one ECS instance to the scaling group.
  • To meet requirement 3, create an event-triggered task named EventTriggeredECIScalingIn. When the CPU utilization reaches or drops below 20%, Auto Scaling automatically executes the scaling rule that removes one ECS instance from the scaling group.
  1. In the upper part of the scaling group details page, click the Scaling Rules and Scaling Activities 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 the event-triggered task. For more information, see Custom monitoring event-triggered tasks.
  3. Create an event-triggered task for scale-out.
    1. Click Create Event-triggered Task.
    2. Configure the parameters for the event-triggered task and click OK.
      Task-01The following table describes the settings used in this example. For parameters that are not described in the following table, use the default values.
      Parameter Example Description
      Name EventTriggeredScalingOut None
      Description Adds one ECS instance when the average CPU utilization reaches or exceeds 70%. None
      Resource Monitored 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 Type System Monitoring This task monitors the system metrics.
      Monitoring Metric (ECS) CPU Utilization If you set Monitoring Metric to (ECS) CPU Utilization, the CPU utilization of the ECS instances in the scaling group is monitored.
      Reference Period 1 Minute Data is collected once every minute.
      Condition Average(Average) >= 70% If the average CPU utilization of the ECS instances in the scaling group reaches or exceeds 70%, data is collected once.
      Trigger After 3 Times If the average CPU utilization of the ECS instances in the scaling group reaches or exceeds 70% three times in a row, an alert is triggered.
      Trigger Rule 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 scale-in.
    1. Click Create Event-triggered Task.
    2. Configure the parameters for the event-triggered task and click OK.
      task-02The following table describes the settings used in this example. For parameters that are not described in the following table, use the default values.
      Parameter Example Description
      Name EventTriggeredScalingIn None
      Description Removes one ECS instance from the scaling group when the average CPU utilization reaches or drops below 20%. None
      Resource Monitored 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 Type System Monitoring None
      Monitoring Metric (ECS) CPU Utilization If you set Monitoring Metric to (ECS) CPU Utilization, the CPU utilization of the ECS instances in the scaling group is monitored.
      Reference Period 1 Minute Data is collected once every minute.
      Condition Average(Average) <= 20% If the average CPU utilization of the ECS instances in the scaling group reaches or drops below 20%, data is collected once.
      Trigger After 3 Times If the average CPU utilization of ECS instances in the scaling group reaches or drops below 20% three times in a row, an alert is triggered.
      Trigger Rule Remove1 When the alert is triggered, Auto Scaling executes the scaling rule that removes one ECS instance from the scaling group.

Step 5: Create a scheduled task

A scheduled task is a preset task that can execute a specified scaling rule at a specified time. In this example, the following scheduled tasks are required to meet requirement 4 and requirement 5:
  • To meet requirement 4, create a scheduled task named ScheduledECIScalingOut for peak hours. This task triggers the scaling rule that adds 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 scaling rule that removes one ECS instance from the scaling group.
  1. Create a scheduled task for scale-out.
    1. Click Create Scheduled Task.
    2. Configure the parameters for the scheduled task and click OK.
      Scheduled task-01

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

      Parameter Example Description
      Task Name ScheduledScalingOut None
      Description Adds one ECS instance at 17:55 every day. None
      Executed At 17:55 November 1, 2021 This parameter specifies the time at which the task is executed. In this example, the scheduled task is executed at 17:55 every day, which is five minutes before the peak hours.
      Scaling Method Select Existing Scaling Rule None
      Simple Scaling Rule Add1 This task triggers the scaling rule that adds one ECS instance to the scaling group.
      Recurrence By Day This scheduled task is executed once a day. In this example, the scaling rule that adds one ECS instance to the scaling group is executed at 17:55 every day.
  2. Create a scheduled task for scale-in.
    1. Click Create Scheduled Task.
    2. Configure the parameters for the scheduled task and click OK.
      Scheduled task-02

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

      Parameter Example Description
      Task Name ScheduledScalingIn None
      Description Removes one ECS instance from the scaling group at 22:05 every day. None
      Executed At 22:05 November 1, 2021 This parameter specifies the time at which the task is executed. In this example, the scheduled task is executed at 22:05 every day, which is five minutes after the peak hours.
      Scaling Method Select Existing Scaling Rule None
      Simple Scaling Rule Remove1 This task triggers the scaling rule that removes one ECS instance from the scaling group.
      Recurrence By Day This scheduled task is executed once a day. In this example, the scaling rule that removes one ECS instance from the scaling group is executed at 22:05 every day.

Result

Auto Scaling automatically scales ECS instances based on your 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 every day to meet requirement 4.
  • Auto Scaling removes one ECS instance from the scaling group at 22:05 every day to meet requirement 5.

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

Instances