This topic describes how to use Auto Scaling to scale elastic container instances in an efficient manner.
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 daily business requirements. Each elastic container instance must have 4 vCPUs and 8 GiB memory. | |
Unpredictable increase in demand |
| |
Predictable increase in demand from 18:00 to 22:00 every day |
|
Preparations
The first time you use Auto Scaling, you must make the following preparations:
The AliyunServiceRoleForAutoScaling role is created for Auto Scaling when Auto Scaling is activated.
If you use a RAM user to log on to the Auto Scaling console, 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. The scaling group is the core unit in Auto Scaling. Auto Scaling automatically adds instances to or removes instances from scaling groups.
- Log on to the Auto Scaling console.
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.
- In the top navigation bar, select the region where Auto Scaling is activated.
In the upper-left corner of the Scaling Groups page, click Create.
Configure parameters of the scaling group and click OK.
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.
Parameter
Example
Description
Scaling Group Name
Enter MyFirstECIScalingGroup.
None
Type
Set the value to ECI.
A value of ECI specifies that the scaling group contains only elastic container instances.
Instance Configuration Source
Set the value to Create from Scratch.
After you create the scaling group, you must create a scaling configuration.
Minimum Number of Instances
Set the value to 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 elastic container instance to meet the required minimum number of instances.
Maximum Number of Instances
Set the value to 5.
The scaling group can contain up to five elastic container instances. Auto Scaling automatically removes excess instances from the scaling group to help Company A reduce costs.
VPC
Select 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.
vSwitch
Select vsw-bp155oak33mqngov2****.
In the Create Scaling Group dialog box, click OK.
Step 2: Create a scaling configuration
A scaling configuration is a template that describes information about the instance configuration, such as the instance type, disk, public IP address, and security group. You can use a scaling configuration to create elastic container instances during scale-outs. Auto Scaling uses the scaling configuration to create elastic container instances that have the same configurations.
- 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.
In the upper part of the scaling group details page, click the Instance Configuration Sources tab.
On the Scaling Configurations tab, click Create Scaling Configuration.
In the Basic Settings step and Other Settings (Optional) step, configure parameters as prompted. In the Confirm step, click Create.
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.
Step
Parameter
Example
Description
Basic Settings
Billing Method
Set the value to Pay-as-you-go.
Auto Scaling is free of charge. However, you are charged 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 overview.
Security Group
Select sg-bp18kz60mefsicfg****.
Select an existing security group. For information about how to create a security group, see Create a security group.
Container Group Configurations
vCPU: Select 4 vCPUs.
Memory: Select 8 GiB.
Name: Enter container-group-a.
All containers in the container group share the resources of the container group.
NoteIn this example, the cost optimization feature is disabled. For information about the cost optimization feature, see Enable the cost optimization feature for scaling groups of the Elastic Container Instance type.
Container Configurations
Container Name: Enter container-1.
Image:
Select Container Image: Select eci_open/centos on the Common Images tab.
Select Image Tag: Select 7.
Image Pull Policy: Set this parameter to Always or If Not Present.
Startup Command:
Executable Command: Enter sleep.
Parameter: Enter 999999.
The image that runs on the container and the startup command of the container.
Other Settings (Optional)
Scaling Configuration Name
Enter MyFirstECIScalingConfiguration
None
In the Success message, click Enable.
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 enters the Active state.
In the Enable Scaling Group message, click OK.
You must enable the scaling group. This allows Auto Scaling to add or remove instances to or from the scaling group.
NoteIn 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 the instances in a scaling group.
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 scaling rules are created to meet the business requirements:
Scale-out rule: adds one elastic container instance.
Scale-in rule: removes one elastic container 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.
In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab.
On the Scaling Rules tab, create a scale-out rule.
In the upper-left corner of the Scaling Rules tab, click Create Scaling Rule.
In the Create Scaling Rule dialog box, configure parameters of the scale-out rule and click OK.
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.
Parameter
Example
Description
Rule Name
Enter Add1.
None
Rule Type
Select 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.
Operation
Set the value to Add 1 Instances.
One elastic container instance is added to the scaling group during a scale-out.
On the Scaling Rules tab, create a scale-in rule.
In the upper-left corner of the Scaling Rules tab, click Create Scaling Rule.
In the Create Scaling Rule dialog box, configure parameters of the scale-in rule and click OK.
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.
Parameter
Description
Description
Rule Name
Enter Remove1.
None
Rule Type
Select 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.
Operation
Set the value to Remove 1 Instances.
One elastic container instance is removed from the scaling group during a scale-in.
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 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 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 average CPU utilization reaches or drops below 20%, Auto Scaling automatically executes the scaling rule that removes one elastic container instance from the scaling group.
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.
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.
Create an event-triggered task for a scale-out.
Click Create Event-triggered Task.
Configure parameters of the event-triggered task and click OK.
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.
Parameter
Example
Description
Name
Enter EventTriggeredECIScalingOut.
None
Description
Adds one elastic container instance when the average CPU utilization reaches or exceeds 70%.
None
Resource Monitored
Set the value to asg-bp1a5rlpy7yrlr1o****.
The ID of the scaling group whose metrics you want to monitor. In this example, the scaling group is MyFirstScalingGroup.
Monitoring Type
Set the value to System Monitoring.
The default value is System Monitoring. You do not need to configure this parameter.
Alert Condition
CPU Utilization > Average (Average) >= 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 CPU Utilization metric, the CPU utilization of all elastic container 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 specify Average (Average) >= 70%, an alert is triggered when the average CPU utilization of all elastic container instances in the scaling group reaches or exceeds 70%.
Report Alerts When
Select 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 Period
Set the value to 1 Minute.
Auto Scaling collects, summarizes, and compares data based on the specified statistical period. The shorter the statistical period, the more frequently alerts are triggered. Configure this parameter based on your business requirements. For example, if you want to collect data once every minute, you can set the Statistical Period parameter to 1 Minute.
Triggered After
Set the value to 3 Times.
If the average CPU utilization of the elastic container instances in the scaling group reaches or exceeds 70% for three consecutive times, an alert is triggered.
Effective Period
Select Not Set.
The effective period for 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 always effective.
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 Rule
Select Add1.
When the alert is triggered, Auto Scaling executes the scaling rule that adds one elastic container instance to the scaling group.
Create an event-triggered task for a scale-in.
Click Create Event-triggered Task.
Configure parameters of the event-triggered task and click OK.
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.
Parameter
Example
Description
Name
Enter 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
Set the value to asg-bp1a5rlpy7yrlr1o****.
The ID of the scaling group whose metrics you want to monitor. In this example, the scaling group is MyFirstScalingGroup.
Monitoring Type
Set the value to System Monitoring.
The default value is System Monitoring. You do not need to configure this parameter.
Alert Condition
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 CPU Utilization metric, the CPU utilization of all elastic container 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 specify Average (Average) <= 20%, an alert is triggered when the average CPU utilization of all elastic container instances in the scaling group reaches or drops below 20%.
Report Alerts When
Select 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 Period
Set the value to 1 Minute.
Auto Scaling collects, summarizes, and compares data based on the specified statistical period. The shorter the statistical period, the more frequently alerts are triggered. Configure this parameter based on your business requirements. For example, if you want to collect metric data once every minute, you can set the Statistical Period parameter to 1 Minute.
Triggered After
Set the value to 3 Times.
If the average CPU utilization of all elastic container instances in the scaling group reaches or drops below 20% for three consecutive times, an alert is triggered.
Effective Period
Select Not Set.
The effective period for 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 always effective.
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 Rule
Select 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 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 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.
In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab.
Click the Scheduled and Event-triggered Tasks tab.
On the Scheduled Task tab, create a scheduled task for a scale-out.
Click Create Scheduled Task.
Configure parameters of the scheduled task and click OK.
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.
Parameter
Example
Description
Task Name
Enter ScheduledECIScalingOut.
None
Description
Adds one elastic container instance at 17:55:00 every day.
None
Executed At
Set the value to Nov 10, 2021 5:55 PM.
The point in time at which the task is executed. In this example, the scheduled task is executed at 17:55:00, which is 5 minutes before the peak hours.
Scaling Method
Set the value to Select Existing Scaling Rule.
None
Simple Scaling Rule
Set the value to Add1.
The task triggers the scaling rule that adds one elastic container instance to the scaling group.
Recurrence
Set the value to By Day.
A value of By Day specifies that the 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:00 every day.
On the Scheduled Task tab, create a scheduled task for a scale-in.
Click Create Scheduled Task.
Configure parameters of the scheduled task and click OK.
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.
Parameter
Example
Description
Task Name
Enter ScheduledECIScalingIn.
None
Description
Removes one elastic container instance from the scaling group at 22:05:00 every day.
None
Executed At
Set the value to Nov 1, 2021 10:05 PM.
The point in time at which the task is executed. In this example, the scheduled task is executed at 22:05:00, which is 5 minutes after the peak hours.
Scaling Method
Set the value to Select Existing Scaling Rule.
None
Simple Scaling Rule
Select Remove1.
This task triggers the scaling rule that removes one elastic container instance from the scaling group.
Recurrence
Set the value to By Day.
A value of By Day specifies that the 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:00 every day.
Result
Auto Scaling automatically scales elastic container instances based on your configurations to meet the following requirements:
After you enable the scaling group, 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 adds one elastic container instance at 17:55:00 every day to meet Requirement 4.
Auto Scaling removes one elastic container instance from the scaling group at 22:05:00 every day to meet Requirement 5.
On the Instances tab, you can view the added elastic container instances. The configurations of the scaling configuration used by the scaling group are the same as the configurations of the automatically created elastic container instances. For more information, see View the instances in a scaling group.