Custom ECS scale-in policy with Function Compute

更新时间:
复制 MD 格式

To control which instances are removed during a scale-in event—for example, removing instances with the lowest CPU load first—you can use Function Compute to create a custom scale-in policy. When Auto Scaling performs a scale-in activity, it automatically selects specific instances for removal based on this policy.

Background information

Auto Scaling supports multiple built-in policies for removing instances during a scale-in activity. These policies use criteria such as the instance creation time or the version of the scaling configuration. For more information, see Configure an instance termination policy for a scaling group.

If you have more specific requirements, you can use Function Compute to create a custom scale-in policy tailored to your business needs. Common use cases include:

  • Scenario 1: Remove instances with the lowest CPU load to minimize impact on your application.

  • Scenario 2: Integrate with your own scheduling system to remove idle instances first, reducing costs while maintaining service availability.

Prerequisites

Procedure

Step 1: Create a function

The Alibaba Cloud Function Compute console is available in two versions: Function Compute 2.0 and Function Compute 3.0. This guide uses Function Compute 2.0.

  1. Log on to the Function Compute console.

    If you are in the Function Compute 3.0 console, click Back to Function Compute 2.0.

  2. In the left-side navigation pane, click Services & Functions.

  3. In the top navigation bar, select a region.

  4. Create a service.

    1. On the Services page, click Create Service.

    2. In the panel that appears, configure the service settings and click OK.

      This topic uses the following settings as an example. Use the default values for any other parameters. For more information, see Create a service.

      Parameter

      Example

      Description

      Service

      test-service

      The name of the Function Compute service. Follow the naming conventions shown in the console.

      Description

      test

      A description for the Function Compute service.

      Logging

      Enable

      Enable logging to automatically send function execution logs to Simple Log Service. This lets you debug code, diagnose failures, and perform data analysis.

  5. Create a function.

    1. Click the service name, and then click Function Management in the left-side navigation pane.

    2. Click Create Function.

    3. On the Create Function page, configure the function settings and click Create.

      This topic uses the Use Built-in Runtime method to create the function. The following settings are used as an example. Use the default values for any other parameters. For more information, see Create a function.

      Parameter

      Example

      Description

      Basic Settings

      Function Name

      test-fc

      Enter a name for the function. Follow the naming conventions shown in the console.

      Handler Type

      Event Handler

      The following handler types are supported:

      • Event Handler: The function processes event-driven requests.

      • HTTP Handler: The function processes HTTP or WebSocket requests.

      Code

      Runtime Environment

      Python 3.9

      The runtime environment for your code.

      Code Upload Method

      Use Sample Code

      Hello, world! sample

      The method for uploading the function code to Function Compute.

  6. Write and deploy the function code.

    1. Click the function name to go to the function details page.

    2. On the Code tab, edit the index.py file in the code editor and click Deploy.

      You can implement specific filtering logic based on your business requirements, such as retrieving the CPU load of each instance or querying your own service scheduling system.

      The following sample code selects instances for removal based on the number of instances to be removed (Capacity) and the list of instance IDs (InstanceId) passed in the event.

      # -*- 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
      
  7. (Optional) Test the function.

    1. On the Code tab, click the 图标 icon next to Test Function and select Configure Test Parameters from the drop-down list.

    2. Enter the test event object and click OK.

      The following code is used as a test example. ScalingGroupId specifies the scaling group to manage. CapacityToRemove specifies to remove one ECS instance from the cn-beijing-g zone. Instances specifies the list of instances available for removal.

      {
      	"ScalingGroupARN": "acs:ess:cn-beijing:160998252992****.scalinggroup/asg-2zei8mzn72rb115k****",
      	"ScalingGroupId": "asg-2zei8mzn72rb115k****",
      	"CapacityToRemove": [{
      		"ZoneId": "cn-beijing-g",
      		"Capacity": 1
      	}],
      	"Instances": [{
      			"InstanceId": "i-2zeinb37ovdsx6l0****",
      			"ZoneId": "cn-beijing-g",
      			"InstanceType": "ecs.g7.xlarge",
      			"ChargeType": "PostPaid"
      		},
      		{
      			"InstanceId": "i-2zeinb37ovdsx6l0****",
      			"ZoneId": "cn-beijing-g",
      			"InstanceType": "ecs.g7.xlarge",
      			"ChargeType": "PostPaid"
      		},
      		{
      			"InstanceId": "i-2zeinb37ovdsx6l0****",
      			"ZoneId": "cn-beijing-g",
      			"InstanceType": "ecs.g7.xlarge",
      			"ChargeType": "PostPaid"
      		}
      	],
      	"AdjustmentType": "SystemScaleIn"
      }
    3. Click Test Function.

    4. On the Response tab, view the test result.

      The expected response from the function is shown below. This response instructs Auto Scaling to remove the first instance in the list during a scale-in event.

      {
          "InstanceIds": [
              "i-2zeinb37ovdsx6l0****"
          ]
      }
  8. At the top of the page, click Publish Version and then click OK.

Step 2: Configure the scale-in policy

This section shows how to apply the custom scale-in policy when you create a new scaling group. If you have an existing scaling group, you can modify its scale-in policy instead.

  1. Log on to the Auto Scaling console.

  2. Create a scaling group.

    1. On the Scaling Groups page, click Create Scaling Group.

    2. Configure the scaling group parameters and click Create.

      This topic uses the following settings as an example. Use the default values for any unmentioned parameters. For more information, see Configure a scaling group.

      Parameter

      Example

      Description

      Scaling Group Name

      test

      Enter a name for the scaling group. Follow the naming conventions shown in the console.

      Type

      ECS

      The type of instances in the scaling group.

      This example uses ECS, which means the scaling group manages ECS instances.

      Instance Configuration Source

      Create from Scratch

      The template for creating instances.

      This example uses Create from Scratch, which means no template is specified at this stage. You must then create a scaling configuration after the scaling group is created.

      Instance Removing Policy

      Custom Policy

      • Service: test-service

      • Version: LATEST

      • Function: test-fc

      The policy for removing instances from the scaling group during a scale-in activity.

      Select Custom Policy and then select the function you created in Step 1.

      Minimum Number of Instances

      0

      The minimum number of instances in the scaling group. If the current number of instances falls below this value, Auto Scaling adds instances to meet the minimum requirement.

      Maximum Number of Instances

      5

      The maximum number of instances in the scaling group. If the current number of instances exceeds this value, Auto Scaling removes instances to meet the maximum requirement.

      Enable Expected Number of Instances and Expected Number of Instances

      Enabled, 3

      Enable this feature and set the expected number of instances. Auto Scaling automatically maintains this number of instances in the group.

  3. Create a scaling configuration. For more information, see Create a scaling configuration for ECS instances.

  4. Enable the scaling group. For more information, see Enable or disable a scaling group.

    Because the expected number of instances for this scaling group is set to 3, Auto Scaling automatically creates three ECS instances after the group is enabled.

Step 3: Verify the scale-in

  1. Trigger a scale-in activity and verify the instance removal.

    1. Trigger a scale-in activity by modifying the expected number of instances for the scaling group. For more information, see View or modify a scaling group.

      Important

      You can also trigger scale-in activities by using a scheduled task or an event-triggered task, or by manually executing a scaling rule. However, a custom scale-in policy created by using Function Compute cannot be applied to scale-in activities triggered by modifying the minimum or maximum number of instances for a scaling group.

      In this example, the expected number of instances is changed to 2. Because the scaling group previously had three ECS instances, this change automatically triggers a scale-in activity to remove one ECS instance. This reduces the instance count to the new expected number.

    2. View the scaling activity details to confirm which instance was removed. For more information, see View scaling activity details.

      In this example, the ID of the removed instance is i-2ze2qdthrkpf****tldq.

      In the scaling activity details, the Cause field shows that the user changed the expected capacity from 3 to 2. The Details field shows that the corresponding instance was deleted. The Status Information field indicates that one ECS instance was removed, and the activity status is Successful.

  2. View the function invocation logs to confirm that the scale-in activity triggered the function.

    1. In the Function Compute console, go to the Services page and click the name of your service.

    2. On the Function Management page, click the name of your function.

    3. On the function details page, click the Logs tab.

    4. On the Requests tab, in the Requests List, find the request that corresponds to the time of the scale-in activity and click the request ID.

    5. In the request details panel that appears, click Log Details to view the logs for the function invocation.

      The log output below shows that when the scale-in activity was triggered, Auto Scaling passed the number of instances to remove and the list of current instance IDs in the scaling group to Function Compute. After its logic was applied, the function returned the instance ID i-2ze2qdthrkpf****tldq for removal. This ID matches the ID of the removed instance.

      FC Invoke Start RequestId: 000BFA95-CAA4-5D83-B643-BC51C42402CD
      2024-06-13 11:28:22 000BFA95-CAA4-5D83-B643-BC51C42402CD [INFO] {'ScalingGroupARN': 'acs:ess:cn-beijing:16099825xxx:scalinggroup/asg-2e6ny5x9dtxxx', 'ScalingGroupId': 'asg-2e6ny5x9dtiaxxxg', 'CapacityToRemove': [{'ZoneId': 'cn-beijing-g', 'Capacity': 1}], 'Instances': [{'InstanceId': 'i-2ze2qdthrxxxxtldq', 'ZoneId': 'cn-beijing-g', 'InstanceType': 'ecs.c6e.large', 'ChargeType': 'SpotAsPriceGo'}, {'InstanceId': 'i-2ze2qdtxxxxtldr', 'ZoneId': 'cn-beijing-g', 'InstanceType': 'ecs.c6e.large', 'ChargeType': 'SpotAsPriceGo'}, {'InstanceId': 'i-2ze2qdthxxxxtlds', 'ZoneId': 'cn-beijing-g', 'InstanceType': 'ecs.c6e.large', 'ChargeType': 'SpotAsPriceGo'}], 'AdjustmentType': 'SystemScaleIn'}
      2024-06-13 11:28:22 000BFA95-CAA4-5D83-B643-BC51C42402CD [INFO] 1
      2024-06-13 11:28:22 000BFA95-CAA4-5D83-B643-BC51C42402CD [INFO] {'InstanceIds': ['i-2ze2qdtxxxxtldq']}
      FC Invoke End RequestId: 000BFA95-CAA4-5D83-B643-BC51C42402CD