This topic describes how to use Auto Scaling to build an auto-scalable website application, which can automatically scale out or in instances during short-term high-concurrency scenarios. Such scenarios include campaigns, promotions and flash sales. Your business will maintain optimal resource capacity during traffic fluctuations and stable periods.
Prerequisites
- In the example provided in this topic, the CentOS 7.6 operating system is used. Make sure that you have purchased a Linux instance and built a website as required. For information about how to build a website, see Building website.
- You have completed the preparation steps by following the quick start. The planned resources are as follows:
- CLB instance lb01 and backend server group rs01.
- VPC vpc01 and subnet subnet01.
- The port used by the instance to provide services externally is 8080. Traffic is expected to be evenly distributed across all instances.
- The created instance is not associated with an EIP or load balancer, and does not belong to any scaling group.
Step 1: Create a custom image of the website
You need to create a custom image based on the instance where your website is already deployed. When Auto Scaling automatically scales out instances, it will create new instances with the pre-deployed website environment.
- Clear Cloud-Init configuration.
- Execute the following command to delete the /var/lib/cloud file. This way, the Cloud-init configuration is cleared. If you do not delete the file, information such as the hostname and password cannot be injected into ECS instances created by using the custom image.
- Create a custom image from the instance.
- On the instance list page, click "
> Volumes & Image > Create custom image" in the "Actions" column of the target instance.
- In the pop-up window, enter the name of the custom image.
- Click the "Confirm" button and wait for the custom image to be created. Approximately 1 to 10 minutes are required to create a custom image.
Step 2: Create an instance launch template
An instance launch template is a collection of instance configuration information used for quick instance purchase. We recommend that you use an instance launch template as the scaling configuration source.
- In the top menu bar, select the region of the target instance.
- On the Instances page, click "... > Instance settings > Save as Template" in the "Actions" column of the instance.
- Confirm the configuration information. If the configuration does not meet your business requirements, click the
icon to go to the corresponding configuration page. Pay special attention to the following configuration information to avoid subsequent operation failure:
- The image selected for the instance is the custom image created in Step 1.
- The login method should be "Key Pair", and a key pair must be selected.
- After you confirm that all information is correct, enter the name of the launch template.
- Click the "Create Now" button to complete the operation.
Step 3: Create a scaling group
- Click the "Create Scaling Group" button to enter the scaling group creation page.
- Configure the basic information of the scaling group.
- Configure the instance configuration source for the scaling group. In this example, "Launch Template" is selected and the template created in Step 2 is selected.
- Configure the quantity information of the scaling group.
- Configure the network configuration information for the scaling group.
- Configure the policy configuration information for the scaling group.
- Configure the advanced configuration information for the scaling group.
- Click the "Confirm" button to complete the creation of the scaling group.
Step 4: Enable the scaling group
After you enable the scaling group, scaling operations will be triggered immediately. The scaling group will scale out based on the minimum number of instances you specify, and monitor the instance status.
- In the scaling group list, click the "Enable" button corresponding to the created scaling group.
- Click the "Confirm" button to enable the scaling group.
Step 5: Add existing instances to the scaling group
This operation adds existing instances to the scaling group, where they will be subject to scaling group health checks to ensure normal business operation.
- Click the name of the scaling group and select the "Instance Management" tab.
- Click the "Add existing instance" button and select the instances you have created.
- Do not select "Enable the scaling group to manage the instance lifecycle".
- Click the "Confirm" button to complete the operation.
Step 6: Create alert-based scaling rules
Alert-based scaling rules monitor instance metric data and check if instance operation meets expectations once per minute. This enables dynamic scaling for sudden or unpredictable business scenarios, such as breaking hot news.
- Click the name of the scaling group and select the "Scaling Rules" tab.
- Click the "Create scaling rules" button to go to the page for adding scaling rules.
- Configure the relevant parameters based on the table below.
- Click the "Confirm" button to complete the operation.
- Repeat this step to create a scaling rule with the monitoring metric "Average out-of-band CPU utilization < 80%" and the adjustment "Reduce instances, 2".
Execution results
The scaling group checks the operation status of each instance every minute and performs related operations based on the results:
- When the average out-of-band CPU usage of all instances exceeds 80%, the scaling group automatically creates 2 new instances based on the instance launch template configuration.
- When the average out-of-band CPU usage of all instances is below 80%, the scaling group automatically reduces 2 instances.
- When an instance is in the Error state, it is marked as abnormal and automatically deleted.
- When the number of instances in the scaling group is less than the minimum instance count requirement, the scaling group automatically creates new instances to meet the configured number.
Subsequent operations
We recommend configuring scaling notifications for the scaling group. This allows you to learn the scaling status of instances in the group timely and manage scaling operations efficiently.