If you deploy your application in Elastic Compute Service (ECS) instances and the access traffic to your application fluctuates, you can configure Auto Scaling to quickly scale ECS instances to cope with the changing business demands. Regardless of whether the access traffic has specific patterns or not, you can use Auto Scaling to ensure stable service delivery at minimum resource costs.
Scenarios
Auto Scaling is suitable for both scenarios with fluctuating business traffic and scenarios with stable business traffic. For more information, see Scenarios.
This topic provides examples on how to use Auto Scaling to scale ECS instances efficiently. Company A is used in this topic. The following table describes the requirements and operations required in different business scenarios. If you want to meet the requirements of all the business scenarios that are described in the following table, perform the operations in sequence.
Business scenario | Requirement | Procedure |
Daily business operation without significant fluctuations in access traffic | Requirement 1: At least two ECS instances of the ecs.g7.large instance type are required to meet daily business requirements. | |
Daily business operation with sudden surges in access traffic and great challenges to determine the actual number of instances that are required |
| |
Expected increase of access traffic from 18:00:00 to 22:00:00 every day |
|
Prerequisites
The first time you use Auto Scaling, make sure that the following prerequisites are met:
The AliyunServiceRoleForAutoScaling service-linked role is created. For more information, see Manage the service-linked role for Auto Scaling.
If you want to use Auto Scaling as a Resource Access Management (RAM) user, the AliyunESSFullAccess permission must be granted to the RAM user. For more information, see Grant permissions to a RAM user.
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.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
In the upper-left corner of the Scaling Groups page, click Create.
Configure parameters for 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
MyFirstScalingGroup
The name of the scaling group. The name must be 2 to 64 characters in length and can contain letters, digits, periods (.), underscores (_), and hyphens (-). It must start with a letter or digit.
Type
ECS
The type of the scaling group. In this example, a scaling group of the ECS type is created.
Instance Configuration Source
Create from Scratch
The source of instance configurations. If you select Create from Scratch, you must create a scaling configuration after you create the scaling group.
Minimum Number of Instances
2
The minimum number of instances that must be contained in the scaling group. In this example, 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 Instances
5
The maximum number of instances that can be contained in the scaling group. In this example, the scaling group can contain up to five ECS instances. If the number of ECS instances in the scaling group exceeds five, Auto Scaling automatically removes excess instances from the scaling group to control resource costs.
Network Type
VPC
The type of the network in which you want to create the scaling configuration. 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 more information about how to create a VPC and vSwitch, see Create a VPC with an IPv4 CIDR block.
vSwitch
vsw-bp155oak33mqngov2****
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. Auto Scaling uses this scaling configuration to create ECS 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.
On the Create Scaling Configuration page, configure parameters for the scaling configuration and 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.
Section
Parameter
Example
Description
Basic Information
Scaling Configuration Name
MyFirstScalingConfiguration
The name of the scaling configuration that you want to create.
Billing Method
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 of ECS.
Instance and Image
Instance Configuration Mode
Specify Instance Type
You can set this parameter to Specify Instance Type or Specify Instance Pattern.
Select Instance Type
ecs.n1.large
The computing power that is provided by the instances.
You can select Measure Scaling Group Capacity By Using vCPUs. If you use this feature, the capacity of the scaling group is recalculated, which may trigger new scaling activities in the scaling group.
Select Image
Public Image and CentOS 7.6 64-bit
After you start an ECS instance, the OS and application data of the image are copied to the system disk. You can set this parameter to one of the following values: Public Images, Custom Images, Shared Images, and Marketplace Images.
Storage
System Disk
Standard SSD | 40 GiB
The category and size of the system disks used by ECS instances to store data.
NoteYou can also select one or more data disks to store data. Auto Scaling allows you to specify system disks or data disks of multiple categories to improve the success rate of scale-outs.
Network and Security Group
Public IP address
Assign public network IPv4 address is selected. The Bandwidth Billing parameter is set to Pay-by-bandwidth. The default value of the Bandwidth parameter is 1 Mbit/s.
You are charged for the outbound public bandwidth. The bandwidth fees are built into your ECS instance fees.
Security Group
sg-bp18kz60mefsicfg****
The security group with which you want to associate the scaling group. Select the security group that you prepared in advance. For more information about how to create a security group, see Create a security group.
Management Settings
Logon Credentials
Set Later
The logon credentials of ECS instances. If you select Set Later, you must manually specify passwords for ECS instances after the ECS instances are created.
In the Preview Scaling Configuration dialog box, click Create.
In the The scaling configuration is created. 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 is in the Active state.
In the Enable Scaling Group dialog box, click OK.
You must enable the scaling group. Auto Scaling can scale ECS instances based on your configurations only in scaling groups that are in the Enabled state. In this example, Auto Scaling scales out two ECS instances to meet Requirement 1 of Company A. For information about how to view an instance, 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 allowed minimum or maximum number of instances in a scaling group.
After you create scaling rules, you can select methods to trigger scaling activities based on business scenarios. You can manually execute scaling rules or allow Auto Scaling to automatically execute scaling rules based on scheduled tasks or event-triggered tasks.
In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab, and then click the Scaling Rules tab.
Create two scaling rules.
In this example, a scale-out rule and a scale-in rule are created.
Scale-out rule
In this example, a scale-out rule named Add1 is created. When Auto Scaling triggers a scale-out, one ECS instance is added to the scaling group.
In the upper-left corner of the Scaling Rules tab, click Create Scaling Rule.
In the Create Scaling Rule dialog box, configure parameters for 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
Add1
None.
Rule Type
Simple Scaling Rule
You can create 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
Add 1 Instance
One ECS instance is added to the scaling group during a scale-out.
Scale-in rule
In this example, a scale-in rule named Remove1 is created. When Auto Scaling triggers a scale-in, one ECS instance is removed from the scaling group.
In the upper-left corner of the Scaling Rules tab, click Create Scaling Rule.
In the Create Scaling Rule dialog box, configure parameters for 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
Example
Description
Rule Name
Remove1
None.
Rule Type
Simple Scaling Rule
You can create 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
Remove 1 Instance
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 and collect metric values in real time. When the metric values meet the alert conditions, alerts are triggered and Auto Scaling executes the specified scaling rules.
In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab, click the Event-triggered Tasks tab, and then create an event-triggered task of the system monitoring or custom monitoring type based on your business requirements.
To create an event-triggered task of the system monitoring type, click the Event-triggered Tasks (System) tab.
To create an event-triggered task of the custom monitoring type, click the Event-triggered Tasks (Custom) tab.
Create two event-triggered tasks.
In this example, an event-triggered task for scale-out and an event-triggered task for scale-in are created.
Event-triggered task for scale-out
In this example, an event-triggered task named EventTriggeredScalingOut is created to meet Requirement 2 of Company A. Add1 is specified in EventTriggeredScalingOut. When the CPU utilization of the scaling group reaches or exceeds 70%, Auto Scaling runs EventTriggeredScalingOut to execute Add1.
Click Create Event-triggered Task.
Configure parameters for 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
EventTriggeredScalingOut
None.
Description
Adds one ECS instance to the scaling group when the average CPU utilization reaches or exceeds 70%.
None.
Resource Monitored
asg-bp1g75opkec1fq2y****
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.
NoteIn 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 of the custom monitoring type. For more information, see Event-triggered tasks of the custom monitoring type.
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 (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 When
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
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. In this example, data is collected once every minute.
Triggered After
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 Period
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. Valid values:
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 more information about the cron expressions, see Cron expression.
Trigger Rule
Add1
When an alert is triggered, Auto Scaling executes Add1 to add one ECS instance to the scaling group.
Event-triggered task for scale-in
In this example, an event-triggered task named EventTriggeredScalingIn is created to meet Requirement 3 of Company A. Remove1 is specified in EventTriggeredScalingIn. When the CPU utilization of the scaling group reaches or drops below 20%, Auto Scaling runs EventTriggeredScalingIn to execute Remove1.
Click Create Event-triggered Task.
Configure parameters for 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
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****
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 specify 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 (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 When
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
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. In this example, data is collected once every minute.
Triggered After
3 Times
If the average CPU utilization of all ECS instances in the scaling group reaches or drops below 20% for three consecutive times, an alert is triggered.
Effective Period
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. Valid values:
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 more information about the cron expressions, see Cron expression.
Trigger Rule
Remove1
When an alert is triggered, Auto Scaling executes Remove1 to remove one ECS instance from the scaling group.
Step 5: Create scheduled tasks
A scheduled task is a preset task that can execute a specified scaling rule at a specified time.
In the upper part of the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab, and then click the Scaling Rules tab.
Create two scheduled tasks.
In this example, one scheduled task for scale-out and one scheduled task for scale-in are created.
Scheduled task for scale-out
In this example, a scheduled task named ScheduledScalingOut is created to meet Requirement 4 of Company A. During peak hours, Auto Scaling runs ScheduledScalingOut to automatically add one ECS instance.
Click Create Scheduled Task.
Configure parameters for 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
ScheduledScalingOut
None.
Description
Adds one ECS instance at 17:55:00 every day.
None.
Executed At
Nov 1, 2021 5:55 PM
The point in time at which the task is run. In this example, the scheduled task is run at 17:55:00 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 Add1 rule to add one ECS instance to the scaling group.
Recurrence
By Day
A value of By Day specifies that the scheduled task is run once a day. In this example, the Add1 rule is executed to add one ECS instance at 17:55:00 every day.
Expired At
Nov 3, 2021 5:55 PM
The end time of the scheduled task. The first point of time at which the scheduled task is run must be earlier than the end time of the cycle.
NoteThis parameter is available only if you set Recurrence to By Day, By Week, By Month, or Cron Expression.
Scheduled task for scale-in
In this example, a scheduled task named ScheduledScalingIn is created to meet Requirement 5 of Company A. During off-peak hours, Auto Scaling runs ScheduledScalingIn to automatically removes one ECS instance from the scaling group.
Click Create Scheduled Task.
Configure parameters for 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
ScheduledScalingIn
None.
Description
Removes one ECS instance from the scaling group at 22:05:00 every day.
None.
Executed At
Nov 1, 2021 10:05 PM
The point in time at which the task is run. In this example, the scheduled task is run at 22:05:00 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 Remove1 rule to remove one ECS instance from the scaling group.
Recurrence
By Day
This scheduled task is run once a day. In this example, Remove1 is executed to remove one ECS instance at 22:05:00 every day.
Expired At
Nov 3, 2021 5:55 PM
The end time of the scheduled task. The first point of time at which the scheduled task is run must be earlier than the end time of the cycle.
NoteThis parameter is available only if you set Recurrence to By Day, By Week, By Month, or Cron Expression.
View the results
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.