Use Auto Scaling to manage backend servers
Auto Scaling can automatically add or remove Elastic Compute Service (ECS) instances in Application Load Balancer (ALB) server groups. By creating event-triggered tasks, you can dynamically scale ECS instances based on real-time metrics, ensuring high availability while reducing costs.
Example
When a news website publishes breaking news, the sudden surge in page views (PVs) may exceed the capacity of the current servers, causing them to fail to respond to requests. Traffic returns to normal as the news fades, but these fluctuations are unpredictable and difficult to manage manually.
To handle this, you can create an event-triggered task that uses CloudMonitor to track resource usage, such as CPU utilization. The system then automatically adds or removes ECS instances in the ALB server group. After the scaling group is associated with an ALB server group, scaling activities are synchronized, and requests are distributed across ECS instances based on traffic volume and health check status.
In the following example, ALB distributes requests to ECS01 based on forwarding rules. When the CPU usage of ECS01 reaches the specified threshold, new ECS instances are created and added to the server group. When the CPU usage drops below the threshold, instances are removed and released.
Prerequisites
-
At least one ALB instance is created and Active. For more information, see Create and manage ALB instances.
-
A server group is created for the ALB instance and an ECS instance is added to the server group. The server group is in the Available state. In this example, the server group is named RS1 and the ECS instance is named ECS01.
-
A custom image is created for ECS01. For more information, see Create a custom image from an instance.
-
Health checks are enabled for the ALB instance. For more information, see Configure and manage health checks.
-
At least one listener is created for the ALB instance. For more information, see Add an HTTP listener.
-
The ALB instance and the scaling group are deployed in the same virtual private cloud (VPC).
Step 1: Create a scaling group
Log on to the Auto Scaling console.
In the navigation pane on the left, click Scaling Groups.
In the top navigation bar, select a region.
-
On the Scaling Groups page, click the Create Scaling Group button to go to the Create Scaling Group page.
-
Set the following parameters. For detailed instructions, seeCreate an ECS scaling group.
Parameter
Description
Instance Configuration Source
Select Create from Scratch.
Minimum Number of Instances
Auto Scaling automatically scales out when the number of instances falls below this value. In this example, 1 is selected, meaning at least one ECS instance is required.
Maximum Number of Instances
Auto Scaling automatically scales in when the number of instances exceeds this value. In this example, 2 is selected, meaning at most one more ECS instance can be added to the server group.
Default cooldown (seconds)
The cooldown period after a scaling activity completes, during which no new scaling activities are triggered. This parameter applies only to event-triggered tasks. In this example, 0 is selected. Specify a value based on your business requirements.
VPC and vSwitch
Select the VPC and vSwitch of ECS01.
Associated ALB, NLB, and GWLB Server Groups
Select a server group and specify a port and a weight.
-
Set the other parameters based on your business requirements and click Create. You can view the scaling group on the Scaling Groups page.
Step 2: Create a configuration template for the scaling group
-
On the Scaling Groups page, click Details in the Actions column. On the Instance Configuration Sources tab, click Scaling Configurations.
-
On the Scaling Configurations tab, click Create Scaling Configuration and set the parameters. The following table describes some of the parameters. Set the other parameters referring to Create a scaling configuration (ECS instance).
Parameter
Description
Billing Method
Select a billing method for ECS instances. In this example, Pay-as-you-go is selected.
Select Image
In this example, the custom image of ECS01 is selected.
Instance Configuration Mode
In this example, Specify Instance Type is selected.
Select Instance Type
In this example, the type of ECS01 is selected.
Security Group
In this example, the security group of ECS01 is selected.
-
Click Create. In the Preview Scaling Configuration message, click Confirm.
-
In the The scaling configuration is created dialog box, click Enable Configuration to enable the configuration and scaling group.
Step 3: Create a scaling rule
-
On the Scaling Groups page, find the scaling group that you want to manage and click Details in the Actions column. On the Scaling Rules and Event-triggered Tasks tab, click the Scaling Rules tab.
-
Create an automatic scale-out rule. On the Scaling Rules tab, click Create Scaling Rule, set some of the parameters based on the following table, set the other parameters referring to Configure scaling rules, then click OK.
Parameter
Description
Rule Name
Enter a name for the scaling rule, such as Automatically Adds One ECS Instance.
Rule Type
In this example, Simple Scaling Rule is selected.
Scaling Activity
In this example, Add 1 Instance is selected.
-
Create an automatic scale-in rule. On the Scaling Rules tab, click Create Scaling Rule, set the following parameters, and then click OK.
Parameter
Description
Rule Name
Enter a name for the scaling rule, such as Automatically Removes One ECS Instance.
Rule Type
In this example, Simple Scaling Rule is selected.
Scaling Activity
In this example, Remove 1 Instance is selected.
Step 4: Create an event-triggered task and associate it with the scaling rule
-
In the left-side navigation pane, choose .
-
On the Alert Task page, on the System Monitoring tab, click Create Event-triggered Task.
-
Create an event-triggered task that triggers an automatic scale-out. Click Create Event-triggered Task. In the dialog box that appears, set some of the parameters based on the following table, set the other parameters referring to Configure event-triggered tasks, and click OK.
Parameter
Description
Name
Enter a name for the task, such as Automatically Adds One ECS Instance.
Resource Monitored
Select the scaling group created in Step 1.
Alert Condition
In this example, the alert condition is set to (Agent) CPU Utilization, Maximum (Maximum) >= 60%.
Reference Period
In this example, 1 Minute is selected.
Trigger After
In this example, 1 Times is selected.
Scaling Rule Triggered Upon Alerting
Select the automatic scale-out rule created in Step 3.
-
Create an event-triggered task that triggers an automatic scale-in. Click Create Event-triggered Task. In the dialog box that appears, set the parameters and click OK. The following table describes some of the parameters.
Parameter
Description
Name
Enter a name for the task, such as Automatically Removes One ECS Instance.
Resource Monitored
Select the scaling group created in Step 1.
Alert Condition
In this example, the alert condition is set to (Agent) CPU Utilization, Maximum (Maximum) <= 30%.
Reference Period
In this example, 1 Minute is selected.
Trigger After
In this example, 1 Times is selected.
Scaling Rule Triggered Upon Alerting
Select the automatic scale-in rule created in Step 3.
Step 5: Add ECS instances to the scaling group
To enable Auto Scaling to monitor and adjust backend servers, you must add the target ECS instances to the scaling group.
-
On the Scaling Groups page, find the scaling group that you want to manage and click Details in the Actions column. On the Instances tab, click the Manually Added tab.
-
Then, click Add Existing Instance. On the page that appears, select ECS01 and click Add.
-
You can view ECS01 on the Manually Added tab.
Step 6: Test whether backend servers can be automatically added and removed
Use a stress testing tool to raise the CPU usage of ECS01 to 60% and trigger the scale-out task. Then verify in the ALB console that a new ECS instance is added to the server group. After the stress test ends and CPU usage drops below 30%, verify that the scale-in task removes the instance automatically.
-
Log on to ECS01 and run the following command to install the stress testing tool:
sudo yum install -y epel-release sudo yum install -y stress -
Run the following command to perform a stress test on ECS01. The stress test lasts 60 seconds.
sudo stress --cpu 1 --io 4 --vm 2 --vm-bytes 128M --timeout 60s & -
Return to the Alert Task page and wait a few minutes until the Status of the event-triggered task becomes Alert.
-
Go to the Scaling Groups page. You can view that the Total Instances displayed in the Instances/Capacity column is increased by one after the scale-out activity. This indicates that an ECS instance is created and added to the scaling group.
Log on to the ALB console.
-
In the left-side navigation pane, choose .
-
Click the ID of the server group, and click the Backend Servers tab. On this tab, you can view that the server group contains two backend servers. The ECS instance named in the
ESS-XXformat is added by Auto Scaling. -
After the 60-second stress test ends, you can log on to the Auto Scaling and ALB consoles to check whether the ECS instance is automatically removed from the server group.
References
-
For more information about Auto Scaling, see What is Auto Scaling?
-
Auto Scaling supports scheduled tasks that can automatically scale servers. For more information, see Configure instance count using a scheduled task.
-
Auto Scaling integrates with CloudMonitor to support resource scaling based on monitoring metrics and predictive values. For more information, seeConfigure scaling rules.