This topic describes how to use Auto Scaling to scale Elastic Compute Service (ECS) instances in an efficient manner.
Background information
This topic provides examples on how to use Auto Scaling to scale ECS instances. Company A is used in this topic. The operations required to scale ECS instances vary based on business scenarios. The following table describes the requirements and operations required in different business scenarios.
Business scenario | Requirement | Procedure |
---|---|---|
Stable demand | Requirement 1: At least two ECS instances of the ecs.g7.large instance type must be prepared to meet daily business requirements. | |
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.
- If you use Auto Scaling as a RAM user, 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. 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 MyFirstScalingGroup. None Type Set the value to ECS. If you set the Type parameter to ECS, the scaling group contains only ECS instances. Instance Configuration Source Set the value to Create from Scratch. After the scaling group is created, you must create a scaling configuration. Minimum Number of Instances Set the value to 2. 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 Set the value to 5. The scaling group can contain up to five ECS instances. Auto Scaling automatically removes excess instances from the scaling group to help Company A reduce costs. Network Type Set the value to VPC. When you create a scaling configuration, you can select only the instance types that support virtual private clouds (VPCs). 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 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 scale-outs. Auto Scaling uses this scaling configuration to create ECS instances with 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 page, click the Instance Configuration Sources tab.
- On the Scaling Configurations tab, click Create Scaling Configuration.
- Follow the on-screen instructions to complete the configurations. In the Preview step, confirm the configurations 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.
Step Parameter Example Description Basic Configurations Billing Method Set the value to 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. Instance Configuration Mode Set the value to Specify Instance Type. You can select Specify Instance Type or Intelligent Configuration. Instance Type Select ecs.g7.large. The computing power that is provided by the instance. Image Set the value to Public Image and specify CentOS 7.6 64-bit. After the instance is started, the OS and application data of the image are copied to the system disk. Valid values: Public Image, Custom Image, Shared Image, and Marketplace Image. Storage Set the System Disk parameter to Enhanced SSD (ESSD) and specify 40 GiB of memory. Select a system disk or one or more data disks for ECS instances to store data. Public IP Address Select Assign Public IPv4 Address. By default, the Bandwidth Billing parameter is set to Pay-By-Traffic. Set the peak bandwidth to 1 Mbit/s. You are charged for outbound traffic to the Internet. The peak bandwidth is 1 Mbit/s. Security Group Select sg-bp18kz60mefsicfg****. Select the security group that you prepared in advance. For information about how to create a security group, see Create a security group. System Configurations (Optional) Logon Credentials Set the value to Set Later. After ECS instances are created, you can specify passwords for the ECS instances. Preview Scaling Configuration Name Enter MyFirstScalingConfiguration. None - In the The scaling configuration is created message, click Enable Configuration.
- 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 message, click OK.
You must enable the scaling group. Auto Scaling can scale ECS instances only in scaling groups that are in the Enabled state.
Note In this example, Auto Scaling scales out two ECS 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 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 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 of Company A:
- Scale-out rule: adds one ECS instance.
- Scale-in rule: removes one ECS 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.
- Create a scale-in rule for the scaling group.
Step 4: Create event-triggered tasks
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 scale-out rule to add one ECS 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 scale-in rule to remove one ECS 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 an event-triggered task. For more information, see Custom monitoring event-triggered tasks.
- Create an event-triggered task for a scale-out.
- Create an event-triggered task for a scale-in.
Step 5: Create scheduled tasks
- To meet Requirement 4, create a scheduled task named ScheduledECIScalingOut for peak hours. This task triggers the scale-out rule to add one ECS instance to the scaling group.
- To meet Requirement 5, create a scheduled task named ScheduledECIScalingIn for off-peak hours. This task triggers the scale-in rule to remove one ECS 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 Tasks tab, create a scheduled task for a scale-out.
- On the Scheduled Task tab, create a scheduled task for a scale-in.
Result
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.
