Auto Scaling adjusts compute resources based on your policies to handle traffic fluctuations and reduce costs. For unpredictable workloads, use event-triggered tasks to monitor metrics such as CPU utilization and automatically add or remove ECS instances when thresholds are reached.
Scenarios
Scenario description
Service demand fluctuates unpredictably, making it difficult to determine the required number of servers in advance.
For example, a news website experiences unpredictable traffic. When a popular news story breaks, traffic surges. After the story is no longer current, traffic drops.
Solution
Use event-triggered tasks with Cloud Monitor to execute scaling rules when monitoring metrics meet specified conditions, automatically scaling servers based on workload.
Benefits
Zero cost for standby servers
Auto Scaling creates and releases ECS instances on demand, eliminating standby servers. You only maintain compute resources required for daily traffic.
Automatic scaling
Cloud Monitor tracks load changes in real time, enabling automatic scaling that meets business demands while reducing resource and O&M costs.
Prerequisites
The first time you use Auto Scaling, you must complete Resource Access Management (RAM) user authorization.
The AliyunServiceRoleForAutoScaling service-linked role is created. For more information, see Service-linked roles.
If you use Auto Scaling as a RAM user, the RAM user must be granted the AliyunESSFullAccess policy. For more information, see Grant permissions to a RAM user.
Step 1: Create a scaling group
A scaling group is a collection of instances that meet your business requirements and serves as the core unit of Auto Scaling. Auto Scaling adjusts the number of instances in scaling groups by adding or removing them as needed.
Go to the Scaling Groups page.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
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.
On the Create by Form tab, configure the scaling group and click Create.
The following table describes the parameter settings used in this topic. Parameters that are not covered in the following table default to their default settings. For information about how to create a scaling group, see Create scaling groups.
Parameter
Example
Description
Scaling Group Name
test
Enter a name for the scaling group. The name must meet the format requirements displayed on the UI.
Type
ECS
Select ECS, which specifies that the scaling group contains ECS instances.
Instance Configuration Source
Create from Scratch
Do not specify the template for automatically creating ECS instances at this stage. After the scaling group is created, you can proceed to create a scaling configuration.
Minimum Number of Instances
1
Specify the minimum number of instances in the scaling group. If the number of instances in the scaling group falls below this value, Auto Scaling will add ECS instances until the desired minimum is reached.
Maximum Number of Instances
5
Specify the maximum number of instances in the scaling group. If the number of instances exceeds this value, Auto Scaling will remove ECS instances until the number of instances falls below the specified limit.
VPC
vpc-bp1jmxxau0lur929p****
Select a VPC for the ECS instances in the scaling group.
vSwitch
vsw-2zeknnyw2ewufbs4z****
vsw-2zesy03h8eaf9fe0l****
Select one or more vSwitches for the ECS instances in the scaling group. We recommend that you select multiple vSwitches to improve the success rate of scale-out events.
Step 2: Create a scaling configuration and enable the scaling configuration and scaling group
A scaling configuration is a template used to create ECS instances during scale-out events. It includes information such as the billing method, instance type, storage, and network settings. After you create a scaling configuration, you can enable the scaling configuration and then enable the scaling group.
Find the scaling group that you want to manage and open its details page in one of the following ways:
In the Scaling Group Name/ID column, click the ID of the scaling group.
In the Actions column, click Details.
At the top of the page, click the Scaling Configurations tab.
On the Scaling Configurations tab, click Create Scaling Configuration.
On the Create Scaling Configuration page, configure parameters to create a scaling configuration and then click Create.
The following table describes the parameter settings used in this topic. Parameters not covered in the following table default to their default settings. For more information about how to create a scaling configuration, see Create a scaling configuration (ECS instance).
Section
Parameter
Example
Description
Basic Information
Scaling Configuration Name
test
Enter a name for the scaling configuration. The name must meet the format requirements displayed on the UI.
Billing Method
Set the value to Pay-as-you-go.
Auto Scaling is free. However, charges apply for ECS instances created during scale-out events. In this example, the pay-as-you-go billing method is used. For more information, see Billing overview of ECS.
Image and Instance
Instance Configuration Mode
Specify Instance Pattern
Select Specify Instance Pattern to choose specifications for ECS instances.
Instance Attribute Combination
2 vCPUs, 4 GiB Memory, Enterprise Level
Select the appropriate number of vCPUs and memory size for ECS instances based on your business requirements.
Select Image
Public Image: Alibaba Cloud Linux 3.2104 LTS 64-bit
Select an image to deploy ECS instances. This example uses a public image. In real-world scenarios, you can choose a custom image specific to your application.
Storage
System Disk
Enterprise SSD (ESSD), 40 GiB, PL0
Select a system disk for ECS instances.
Network and Security Group
Public IP Address
Assign Public IPv4 Address, Pay-by-bandwidth, and 1 Mbit/s bandwidth
Specify whether to assign public IP addresses to ECS instances. Outbound public bandwidth is charged separately, and the fees are included in your ECS instance costs.
Security Group
sg-bp18kz60mefsicfg****
Select an existing security group. For information about how to create a security group, see Create a security group.
Management Settings
Logon Credentials
Set Later
Select Set Later, which requires you to manually configure passwords for ECS instances after you create the instances.
In the Preview Scaling Configuration dialog box, confirm the information and click Create Order.
In the The scaling configuration is created. message, click Enable.
In the Apply Scaling Configuration dialog box, click OK.
NoteIn a scaling group, you must enable one scaling configuration. After you enable a scaling configuration, the scaling configuration enters the Active state.
In the Enable Scaling Group message, click OK.
The scaling group must be enabled to allow Auto Scaling to scale instances automatically based on your business requirements.
In this example, the Minimum Number of Instances parameter is set to 1. When you enable the scaling group, Auto Scaling automatically creates one ECS instance from the enabled scaling configuration. You can go to the Instances tab of the scaling group details page and check the instance information on the Auto Created tab.
Step 3: Create scaling rules
A scaling rule defines the specific action to be taken during a scaling event, such as how many instances to add or remove.
On the details page of the scaling group, click the Scaling Rules and Event-triggered Tasks tab. Then, click the Scaling Rules tab.
Click Create Scaling Rule, configure parameters to create the scaling rule, and then click OK.
In this example, simple scaling rules are created. For more information about how to create a scaling rule, see Configure scaling rules.
Parameter
Description
Rule Name
Enter a name for the scaling rule. The name must meet the format requirements displayed on the UI.
Rule Type
Specify the type of the scaling rule. In this example, select Simple Scaling Rule. For more information about scaling rules, see Scaling rules.
Operation
Specify the number of instances to add or remove when the scaling rule is executed. The number of instances added or removed during each scaling event must not exceed 1,000.
Cooldown Time
Optional. Specify the cooldown period for the scaling rule. Unit: seconds. If you do not configure this parameter, the cooldown period of the scaling group takes effect. For more information, see Cooldown Period.
Repeat this step to create scale-out and scale-in rules. The following table describes the configurations used in this example.
Scaling rule
Example
Scale-out rule
Rule Name: add
Rule Type: Simple Scaling Rule
Operation: Add 1 Instance
Scale-in rule
Rule Name: remove
Rule Type: Simple Scaling Rule
Operation: Remove 1 Instance.
Step 4: Create an event-triggered task
An event-triggered task monitors metrics in real time. When statistics meet specified conditions, an alert triggers and a scaling rule executes.
On the scaling group details page, click the Scaling Rules and Tasks tab.
On the Event-triggered Tasks (System) tab, click Create Event-Triggered Task, configure the task, and then click OK.
NoteThis topic uses system metrics. You can also report custom business metrics to create event-triggered tasks. Overview of event-triggered tasks.
Key configurations are described below. Keep default settings for items not mentioned. Configure an event-triggered task.
Configuration Item
Description
Name
Enter a task name. Format requirements are shown on the console.
Alert Condition
Select one or more system-defined metrics as alert conditions. To add metrics, click Add Metric.
Configuration items:
Metric: A system monitoring metric. For example, (ECS) CPU Utilization monitors the CPU utilization of ECS instances in the scaling group.
Statistical Method: Determines whether the metric exceeds the threshold based on the average, maximum, or minimum value. For example, Average >= 70% triggers an alert when the average CPU utilization in the scaling group reaches 70% or higher.
Alert response rule
Select the scaling rule to execute after an alert is triggered.
Repeat this step to create event-triggered tasks for both the scale-out and scale-in rules. Sample configurations:
Event-triggered Task
Sample Configuration
Alert-triggered scale-out
Name: alarm-add
Alert Condition: (ECS) CPU Utilization > Average > 70%
Rule to Execute: add
Alert-triggered scale-in
Name: alarm-remove
Alert Condition: (ECS) CPU Utilization > Average < 20%
Post-alarm action: remove
Verify the automatic scaling effect
After the event-triggered tasks are created, the system monitors metrics in real time and initiates scaling activities when conditions are met.
Expected results:
When the CPU utilization of ECS instances in the scaling group is greater than 70%, one ECS instance is automatically added.
When the CPU utilization of ECS instances in the scaling group is less than 20%, one ECS instance is automatically removed.
Use a stress testing tool to verify the results:
While an event-triggered task runs, go to the scaling group details page. Click the Instances tab, then the Auto Created tab to observe instance count changes.
After an event-triggered task completes, go to the scaling group details page. On the Scaling Activities tab, verify the scaling activity executed as expected. Click the activity ID for details.