When a scale-in is triggered, Auto Scaling removes instances from your scaling group based on the specified scale-in policy. This topic describes how to create a custom scale-in policy by using Function Compute.
Prerequisites
- Auto Scaling is activated. For information about how to activate Auto Scaling, see Manage the service-linked role for Auto Scaling.
- Function Compute is activated. For information about how to activate Function Compute, see Activate Function Compute.
- Log Service is activated. For information about how to activate Log Service, see Step 2: Grant permissions to the RAM user.
Background information
Auto Scaling supports multiple types of instance scale-in policies. When a scale-in is triggered in a scaling group, Auto Scaling removes instances from the scaling group based on conditions such as the instance creation time and scaling configuration. For more information, see Configure a combination policy for removing instances.
- Scenario 1: Remove instances that have the lowest CPU utilization from your scaling group to reduce business impact from the scale-in.
- Scenario 2: Remove idle instances from your scaling group based on your business scheduling system to save costs without compromising your business.
Introduction to custom scale-in policy
{
"ScalingGroupARN": "acs:ess:cn-hangzhou:160998252992****:scalinggroup/asg-bp12zc8b5m4mxhra****",
"ScalingGroupId": "asg-bp12zc8b5m4mxhra****",
"CapacityToRemove": [
{
"ZoneId": "cn-hangzhou-i",
"Capacity": 1
}
],
"Instances": [
{
"InstanceId": "i-bp11az18341u****t47v",
"ZoneId": "cn-hangzhou-i",
"InstanceType": "ecs.g7.xlarge",
"ChargeType": "PostPaid"
},
{
"InstanceId": "i-bp11mcx562ak****lcn6",
"ZoneId": "cn-hangzhou-i",
"InstanceType": "ecs.g7.xlarge",
"ChargeType": "PostPaid"
},
{
"InstanceId": "i-bp11mcx562aky****lcn7",
"ZoneId": "cn-hangzhou-i",
"InstanceType": "ecs.g7.xlarge",
"ChargeType": "PostPaid"
}
],
"AdjustmentType": "SystemScaleIn"
}
Parameter | Description |
---|---|
ScalingGroupARN | The Alibaba Cloud Resource Name (ARN) of the scaling group, which is used to identify the scaling group. |
ScalingGroupId | The ID of the scaling group. |
CapacityToRemove | The recommended number of instances that can be removed from your scaling group in each zone. The value of this parameter is only for reference. The CapacityToRemove parameter specifies the expected number of instances that are removed from your scaling group during the scale-in. To ensure that the actual number of instances that are removed from your scaling group meets your business requirements, the number of removed instances that are returned in response parameters must be greater than or equal to the value of this parameter. |
Instances | The information about the instances that can be removed from the scaling group. The information includes the ID, zone, instance type, and billing method of each instance. |
AdjustmentType | The adjustment mode of the scaling rule. |
Step 1: Create a function in Function Compute
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region.
- On the Services page, click Create Service.
- In the Create Service panel, configure parameters and click OK. The following table describes the parameters. Note If you want to access a virtual private cloud (VPC) or the Internet, click Show Advanced Options to enable the features that you require. For more information, see Create a service.
Parameter Description Example Name Enter a name for the Function Compute service. The name must be 1 to 128 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or a hyphen (-).
ess_custom_terminate_policy Description Enter a description for the Function Compute service. - Logging Specify whether to enable the logging feature. Valid values: - Enable: Function Compute sends function execution logs to Log Service for persistent storage. You can debug code, troubleshoot issues, and analyze data based on the logs.
- Disable: You cannot use Log Service to store or query function execution logs.
Enable - In the left-side navigation pane, click Functions, and then click Create Function.
- On the Create Function page, configure parameters based on your business requirements and click Create. The following table describes the parameters that are used in this example.Note For parameters that are not described in the following table, you can use the default settings. For more information, see Create a function.
Parameter Description Example Select a method to create the function Valid values: - Use Built-in Runtime
- Use Custom Runtime
- Use Container Image
Use Built-in Runtime Basic Settings Function Name Enter a name for the function. The name must be 1 to 64 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). The name cannot start with a digit or a hyphen (-).
ess_custom_terminate_policy_function Request Type Valid values: - Event Handler: The function processes event requests.
- HTTP Handler: The function processes HTTP requests or WebSocket requests.
Event Handler Code Runtime Select a runtime that is supported by Function Compute. Python 3.9 Code Upload Method Select a method to upload the function code to Function Compute. Valid values: - Use Sample Code
- Upload ZIP
- Upload Folder
- Upload OSS
Below the Use Sample Code tab, select Hello, world!.
Hello, world! - On the Code tab, copy the following sample code to the index.py file and click Deploy. Note On the details page of the function, click OK in the Web IDE Usage Notes dialog box.In the function code, you can define specific conditions to remove instances based on your business requirements. For example, you can specify instance vCPU utilization or business scheduling system as the instance removal condition. In this example, the specified order of instance IDs is used as the instance removal condition.
# -*- coding: utf-8 -*- import logging import json def handler(event, context): evt = json.loads(event) logger = logging.getLogger() logger.info(evt) removeCount = 0 if "CapacityToRemove" in evt: for cp in evt["CapacityToRemove"]: logger.info(cp["Capacity"]) removeCount += int(cp["Capacity"]) instances_to_terminate = [] if "Instances" in evt: for i in evt["Instances"]: instances_to_terminate.append(i["InstanceId"]) response = { 'InstanceIds': instances_to_terminate[:removeCount] } logger.info(response) return response
- On the Code tab, click the icon to the right of Test Function. In the drop-down list, select Configure Test Parameters, enter the same code in Introduction to custom scale-in policy, and then click OK.
- On the Code tab, click Test Function. After the function is executed, you can click the Response tab to view the IDs of the instances that can be removed based on the custom scale-in policy. Sample code:
{ "InstanceIds": [ "i-bp11az18341u****t47v" ] }
The InstanceIds parameter specifies the ID of the instances that can be removed from the scaling group based on the custom scale-in policy. Auto Scaling removes instances from the scaling group based on the value of the InstanceIds parameter and the scale-in rule. Examples:- If you set the CapacityToRemove parameter to 1, Auto Scaling removes one instance that is specified by the InstanceIds parameter from the scaling group.
- If you leave the InstanceIds parameter empty, Auto Scaling removes no instance from the scaling group.
- In the upper part of the page that appears, click Publish Version, and then click OK.
Step 2: Configure a scaling group for the custom scale-in policy
- Log on to the Auto Scaling console.
- Create a scaling group. For more information, see Create scaling groups. When you create the scaling group, set the Scale-in Policy parameter to Custom Policy, as shown in the following figure.The following table describes the parameters of the scaling group.
Parameter Description Example Type The type of instances that provide computing power in the scaling group. This parameter also specifies the type of instances that Auto Scaling adds to or removes from the scaling group during a scaling activity. Valid values: - ECS: Elastic Compute Service (ECS) instance
- ECI: elastic container instance
ECS Instance Configuration Source Auto Scaling creates instances based on the value of the Instance Configuration Source parameter. Valid values: - Launch Templates
- Select Existing Instance
- Create from Scratch
Create from Scratch Scale-In Policy If you want to remove instances from the scaling group and you have multiple options, configure the Scale-In Policy parameter. The Scale-In Policy parameter is supported only if you set the Type parameter to ECS. Note If multiple instances still meet the scale-in condition after Auto Scaling removes a specific number of instances from the scaling group based on the scale-in policy, Auto Scaling continues to remove an instance in a random manner.Valid values:- Earliest Instance Created Using Scaling Configuration
- Earliest Created Instance
- Most Recent Created Instance
- Custom Policy
Custom Policy - Set the Service parameter to ess_custom_terminate_policy.
- Set the Version parameter to LATEST.
- Set the Function parameter to ess_custom_terminate_policy_function.
Maximum Number of Instances If the number of instances in the scaling group is greater than the value of this parameter, Auto Scaling removes instances until the number of instances in the scaling group does not exceed the maximum number. 3 Note In this example, this parameter is set to 3 to facilitate the subsequent test.VPC Select an existing VPC. vpc-bp1l5b6zsvw30qb6t**** vSwitch Each vSwitch resides in a zone. To deploy instances across multiple zones, specify multiple vSwitches in different zones. vsw-bp1qo7s91cbch5i4l**** - Create a scaling configuration. For more information, see Create scaling configurations for scaling groups that contain ECS instances.
- Enable the scaling group. For more information, see Enable or disable scaling groups.
- Create a scaling rule. In this example, two simple scaling rules are created. For more information, see Create a scaling rule.
- Create a scale-out rule. In the Create Scaling Rule dialog box, set the Operation parameter to Change To 3 Instances.
- Create a scale-in rule. In the Create Scaling Rule dialog box, set the Operation parameter to Remove 1 Instances.
- Execute the scaling rules. In this step, execute the preceding scaling rules. For more information, see Execute a scaling rule.On the Scaling Activities tab, you can view the execution status of the scaling rules.
Check whether the custom scale-in policy takes effect
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region.
- On the Services tab, find the
ess_custom_terminate_policy
service that is created in Step 1: Create a function in Function Compute and click Functions in the Actions column. - Click Functions in the left-side navigation pane, find the
ess_custom_terminate_policy_function
function that is created in Step 1: Create a function in Function Compute, and then click the name of the function. - On the details page of the function, click the Logs tab.
- Click the Function Logs tab to view the records of function invocations that are triggered by scale-ins.
- Query the request parameters and response parameters that are logged. Sample code:
1-640ad88d-b59ed1af57161e4b363cb7c9 [INFO] {'ScalingGroupARN': 'acs:ess:cn-hangzhou:160998252992****:scalinggroup/asg-bp12zc8b5m4mxhra****', 'ScalingGroupId': 'asg-bp12zc8b5m4mxhra****', 'CapacityToRemove': [{'ZoneId': 'cn-hangzhou-i', 'Capacity': 1}], 'Instances': [{'InstanceId': 'i-bp11az18341u****t47v', 'ZoneId': 'cn-hangzhou-b', 'InstanceType': 'ecs.g7.xlarge', 'ChargeType': 'PostPaid'}, {'InstanceId': 'i-bp11mcx562ak****lcn6', 'ZoneId': 'cn-hangzhou-i', 'InstanceType': 'ecs.g7.xlarge', 'ChargeType': 'PostPaid'}, {'InstanceId': 'i-bp11mcx562aky****lcn7', 'ZoneId': 'cn-hangzhou-i', 'InstanceType': 'ecs.g7.xlarge', 'ChargeType': 'PostPaid'}], 'AdjustmentType': 'SystemScaleIn'} 1-640ad88d-b59ed1af57161e4b363cb7c9 [INFO] 1 1-640ad88d-b59ed1af57161e4b363cb7c9 [INFO] {'InstanceIds': ['i-bp11az18341u****t47v']}
- View the details of the removed instances. For more information, see View the details of a scaling activity.In the response parameters of the function log, the instance
{'InstanceIds': ['i-bp11az18341u****t47v']}
is removed. This matches theInstances "i-bp11az18341u****t47v" are deleted
message that appeared on the scaling activity details page. In this case, the custom scale-in policy works as expected.