This topic describes how to use Auto Scaling to scale elastic container instances.

Background information

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

Business scenario Requirement Operation
Stable demand Requirement 1: At least two elastic container instances are required to meet the daily business requirements. Each elastic container instance must have 4 vCPUs and 8 GiB memory.
  1. Step 1: Create a scaling group
  2. Step 2: Create a scaling configuration
Unpredictable increase in demand
  • Requirement 2: One elastic container instance that has 4 vCPUs and 8 GiB memory must be prepared for unexpected peak hours.
  • Requirement 3: To reduce resource costs, the elastic container instance that is prepared for unexpected peak hours must be removed when the demand for services 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 elastic container instance that has 4 vCPUs and 8 GiB memory must be prepared for predictable peak hours.
  • Requirement 5: To reduce resource costs, the elastic container 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 parameters for the scaling group and click OK.
    ECI-type scaling group

    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
    Scaling Group Name MyFirstECIScalingGroup None
    Type ECI Set Type to ECI. Then, the scaling group contains only elastic container 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 elastic container instances to meet requirement 1 of Company A.

    If the number of elastic container 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 elastic container 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 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 describes the instance configuration information such as the instance type, storage, public IP address, and security group. The template can be used to create elastic container instances during a scale-out activity. Auto Scaling uses this scaling configuration to create elastic container 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. On the Scaling Configurations page, click Create Scaling Configuration.
  4. Follow the on-screen instructions to complete the configurations. In the Confirm step, check the configurations and click Create.
    Configuration

    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.

    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 elastic container instances that are added to the scaling group based on the pricing of the Elastic Container Instance service. For more information, see Billing.
    Security Group sg-bp18kz60mefsicfg**** Select an existing security group. For information about how to create a security group, see Create a security group.
    Container Group Configuration
    • CPU: 4 vCPUs
    • Memory: 8 GiB
    • Name: container-group-a
    All containers in the container group share the resources of the container group.
    Container Configuration
    • Container Name: container-1
    • Image:
      • Select Container Image: Select eci_open/centos on the Common Mirror Image tab.
      • Select Mirror Version: Select 7.
    • Start Command:
      • Execute Command: sleep
      • Parameter: 999999
    This parameter specifies the image that runs on the containers and the startup command of the containers.
    Other Settings (Optional) Configuration Name MyFirstECIScalingConfiguration None
  5. In the Success message, click Enable.
  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 out or scale in only scaling groups that are in the Enabled state.

    Note In this example, Auto Scaling creates two elastic container instances to meet requirement 1 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 users 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 elastic container instance.
  • Scale-in: removes one elastic container 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.
      Rule 1

      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
      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 elastic container 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.
      Rule 2The 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
      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 elastic container 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 elastic container 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 elastic container 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.
      Alert 1The 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
      Name EventTriggeredECIScalingOut None
      Description Adds one elastic container instance when the average CPU utilization reaches or exceeds 70%. None
      Resource Monitored asg-bp1a5rlpy7yrlr1o**** 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 CPU Utilization The CPU utilization of the elastic container 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 elastic container instances in the scaling group reaches or exceeds 70%, data is collected once.
      Triggered After 3 Times If the average CPU utilization of the elastic container 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 elastic container 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.
      Alert 2

      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
      Name EventTriggeredECIScalingIn None
      Description Removes one elastic container instance from the scaling group when the average CPU utilization reaches or drops below 20%. None
      Resource Monitored asg-bp1a5rlpy7yrlr1o**** 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 CPU Utilization The CPU utilization of the elastic container 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 elastic container instances in the scaling group reaches or drops below 20%, data is collected once.
      Triggered After 3 Times If the average CPU utilization of the elastic container 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 elastic container 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 elastic container 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 elastic container 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.
      task 1

      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 ScheduledECIScalingOut None
      Description Adds one elastic container instance at 17:55 every day. None
      Executed At 17:55 Nov 10, 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 elastic container 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 elastic container 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.
      task 2

      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 ScheduledECIScalingIn None
      Description Removes one elastic container instance from the scaling group at 22:05 every day. None
      Executed At 22:05 Nov 10, 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 elastic container 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 elastic container instance from the scaling group is executed at 22:05 every day.

Result

Auto Scaling automatically scales elastic container instances based on your configurations to meet the following requirements:

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

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

ECI instances