Use Auto Scaling to scale an NLB server group
Auto Scaling integrates with Network Load Balancer (NLB) to automatically add or remove Elastic Compute Service (ECS) instances from NLB server groups based on CloudMonitor alerts. This keeps your backend capacity aligned with actual traffic without manual intervention.
How it works
When a scaling group is associated with an NLB server group, any ECS instance that Auto Scaling creates or removes is automatically synced to the server group. NLB distributes requests to backend servers based on traffic volume and health check status.
In this tutorial, NLB forwards requests to a single ECS instance (ECS01). Two event-triggered tasks monitor CPU utilization:
-
If CPU utilization reaches 60% or above, Auto Scaling adds one ECS instance to the server group.
-
If CPU utilization drops to 30% or below, Auto Scaling removes the extra instance.
Prerequisites
Before you begin, ensure that you have:
-
An NLB instance in Active status. For more information, see Create and manage NLB instances.
-
A server group in Available status with at least one backend server and health checks enabled. This tutorial uses a server group named RS1 with a backend server named ECS01.
-
A custom image created from ECS01. For more information, see Create a custom image from an instance.
-
At least one listener configured for the NLB instance. For more information, see Add a TCP listener.
-
The NLB instance and the scaling group in the same virtual private cloud (VPC).
Step 1: Create a scaling group
Log on to the Auto Scaling console.
Log on to the NLB console.
-
In the left-side navigation pane, click Scaling Groups.
-
In the top navigation bar, select a region.
-
On the Scaling Groups page, click Create and configure the following parameters.
Parameter
Description
Instance Configuration Source
Select Create from Scratch.
Minimum Number of Instances
Set to 1. Auto Scaling scales out automatically if the instance count falls below this value. This ensures at least one instance (ECS01) is always running.
Maximum Number of Instances
Set to 2. Auto Scaling scales in automatically if the instance count exceeds this value. In addition to ECS01, at most one more instance can be added during scale-out events.
Default cooldown (seconds)
Set to 0. The scaling group pauses scaling activities for the cooldown period after each scaling event. Set this to a higher value in production to prevent rapid, repeated scaling.
VPC and vSwitch
Select the VPC and vSwitch used by ECS01.
Associated ALB, NLB, and GWLB Server Groups
Select the server group RS1 and specify a port and weight.
-
Configure other parameters based on your requirements and click Create.
Step 2: Create a scaling configuration
A scaling configuration defines the instance specifications that Auto Scaling uses when creating new ECS instances.
-
On the Scaling Groups page, click Details in the Actions column for your scaling group. On the Instance Configuration Sources tab, click Scaling Configurations.
-
Click Create Scaling Configuration and configure the following parameters.
Parameter
Description
Billing Method
Select Pay-as-you-go.
Instance Configuration Mode
Select Specify Instance Type.
Select Instance Type
Select the same instance type as ECS01.
Select Image
Select the custom image you created from ECS01.
Security Group
Select the security group used by ECS01.
-
Click Create, then click Confirm in the preview dialog.
-
In the confirmation dialog, click Enable Configuration to activate the scaling configuration and the scaling group.
Step 3: Create scaling rules
Create two scaling rules: one to add instances on scale-out, one to remove instances on scale-in.
Scale-out rule:
-
On the Scaling Groups page, click Details in the Actions column. On the Scaling Rules and Event-triggered Tasks tab, click the Scaling Rules tab.
-
Click Create Scaling Rule and configure the following parameters. Click OK.
Parameter
Value
Rule Name
Enter a descriptive name, such as Automatically Adds One ECS Instance.
Rule Type
Select Simple Scaling Rule.
Scaling Activity
Select Add 1 Instance.
Scale-in rule:
-
Click Create Scaling Rule again and configure the following parameters. Click OK.
Parameter
Value
Rule Name
Enter a descriptive name, such as Automatically Removes One ECS Instance.
Rule Type
Select Simple Scaling Rule.
Scaling Activity
Select Remove 1 Instance.
Step 4: Create event-triggered tasks
Event-triggered tasks connect CloudMonitor alerts to scaling rules. Create two tasks to handle scale-out and scale-in.
Scale-out task:
-
In the left-side navigation pane, click Event-triggered Tasks.
-
On the System Monitoring tab, click Create Event-triggered Task and configure the following parameters. Click OK.
Parameter
Value
Name
Enter a name, such as Automatically Adds One ECS Instance.
Resource Monitored
Select the scaling group created in Step 1.
Alert Condition
Set to (Agent) CPU Utilization, Maximum (Maximum) >= 60%.
Reference Period
Select 1 Minute.
Trigger After
Select 1 Time.
Scaling Rule Triggered Upon Alerting
Select the scale-out rule created in Step 3.
Scale-in task:
-
Click Create Event-triggered Task again and configure the following parameters. Click OK.
Parameter
Value
Name
Enter a name, such as Automatically Removes One ECS Instance.
Resource Monitored
Select the scaling group created in Step 1.
Alert Condition
Set to (Agent) CPU Utilization, Maximum (Maximum) <= 30%.
Reference Period
Select 1 Minute.
Trigger After
Select 1 Time.
Scaling Rule Triggered Upon Alerting
Select the scale-in rule created in Step 3.
Step 5: Add ECS01 to the scaling group
Auto Scaling monitors instances in the scaling group to determine when to scale. Add ECS01 so Auto Scaling can track its CPU utilization and trigger the event-triggered tasks.
-
On the Scaling Groups page, click Details in the Actions column.
-
On the Instances tab, click the Manually Added tab, then click Add Existing Instance.
-
In the dialog, select ECS01 and click Add. ECS01 now appears on the Manually Added tab.
Step 6: Test automatic scaling
Run a stress test to verify that scaling triggers correctly in both directions.
This example uses Alibaba Cloud Linux 3.2104. Your environment and results may vary.
Trigger scale-out:
-
Log on to ECS01 and run the following command to install the stress tool:
sudo yum install -y stress -
Run the following command to start a 60-second stress test:
sudo stress --cpu 1 --io 4 --vm 2 --vm-bytes 128M --timeout 60s & -
In the Auto Scaling console, go to the Alert Task page and wait until the Status of the scale-out task changes to Alert.
-
On the Scaling Groups page, check the Instances/Capacity column. The Total Instances count should increase by one, confirming that Auto Scaling created and added a new instance.
-
Log on to the NLB console. In the left-side navigation pane, choose NLB > Server Group.
-
Click the server group ID and open the Backend Servers tab. Confirm that two backend servers are listed. The instance named
ESS-XXis the one Auto Scaling added.
Verify scale-in:
-
After the 60-second stress test ends, return to the Auto Scaling console and NLB console to confirm that the extra ECS instance is automatically removed from the server group once CPU utilization drops below 30%.
Clean up resources
After completing the tutorial, remove the resources created to avoid incurring unnecessary charges.
-
In the Auto Scaling console, delete the event-triggered tasks created in Step 4.
-
Delete the scaling rules created in Step 3.
-
Remove ECS01 from the scaling group and delete the scaling group.
-
If you no longer need the scaling configuration, delete it from the Scaling Configurations tab.
-
In the NLB console, release the NLB instance if it is no longer needed.
What's next
-
Scheduled scaling: If your traffic follows predictable patterns (such as business-hour peaks), use scheduled tasks to pre-scale capacity before demand arrives — more reliable than waiting for a CPU alert to fire. See Configure instance numbers by using scheduled tasks.
-
Target tracking and step scaling: If your workload is dynamic and the fixed-increment Simple Scaling Rule in this tutorial feels too blunt, target tracking and step scaling policies adjust capacity more precisely. See Configure scaling rules.
-
Auto Scaling overview: Learn about the full range of Auto Scaling capabilities. See What is Auto Scaling?