automatically scale ECI instances based on real-time workloads

Updated at:

Elastic Container Instance (ECI) works with Auto Scaling (ESS) and Server Load Balancer (SLB) to dynamically add or remove ECI instances based on real-time workloads, improving resource utilization, increasing system availability, and reducing costs. This topic walks through the setup using a Spring Boot containerized application as an example.

Background information

This example uses a Spring Boot containerized application to demonstrate the following scenarios:

  • Use Alibaba Cloud Auto Scaling and Server Load Balancer to automatically scale ECI instances based on real-time workloads.

  • Use the image cache, log collection, and monitoring features of ECI to test container elasticity in a serverless scenario.

  • Use the native scaling capabilities of Auto Scaling to perform a phased release of an application.

The following figure shows the relationships between the components in this example:Auto Scaling

Configurations

Configure an application image

This example uses Spring Boot to build a simple HTTP server. The server listens for external requests and consumes about 5 seconds of CPU time for each request. For the source code, see SpringBootDemo.

Note

In this example, the application listens on TCP port 8761. You can also use a custom port.

  1. Compile the application to obtain a .jar file. Then, package the file into an image using Docker.

    FROM java:8
    ADD target/*.jar /app.jar
    ENTRYPOINT ["java", "-jar", "/app.jar"]
  2. Push the packaged image to your image repository.

Configure load balancing

Distributed applications need load balancing to distribute traffic across replicas. This example uses the Alibaba Cloud Server Load Balancer (SLB) service. SLB includes Application Load Balancer (ALB) and Classic Load Balancer (CLB). This example uses CLB. To configure load balancing, perform the following steps:

  1. Log on to the Classic Load Balancer (CLB) console.

  2. Create a CLB instance.

    A CLB instance is a service entity that receives and distributes traffic. For more information, see Create a CLB instance.

  3. Configure a listener.

    A listener checks client requests and sends them to backend servers. Because the example application uses TCP port 8761, the CLB instance must listen on backend port 8761. For more information, see Add a TCP listener.

Configure a scaling group

  1. Log on to the Auto Scaling console.

  2. Create a scaling group.

    1. In the left navigation pane, click Scaling Groups.

    2. In the top menu bar, select a region.

    3. Click Create Scaling Group.

    4. In the dialog box that appears, configure the scaling group.

      Note the following parameters when you create the scaling group. For more information, see Create a scaling group.

      Parameter

      Description

      Example

      Scaling Group Type

      The type of instances in the scaling group. You must select ECI.

      ECI

      Instance Configuration Source

      The source of instance configuration used during scale-outs.

      • Select Existing Instance: Uses the information of an existing instance to create a scaling configuration.

      • Create From Scratch: Creates a scaling configuration after the scaling group is created.

      Create From Scratch

      Min. Instances

      The minimum number of instances in the scaling group. Auto Scaling automatically adds instances if the count falls below this value.

      1

      Max. Instances

      The maximum number of instances in the scaling group. Auto Scaling automatically removes instances if the count exceeds this value.

      50

      VPC

      Select the virtual private cloud (VPC) for the ECI instances.

      vpc-2zeghwzptn5zii0******

      Select vSwitch

      Select the vSwitches for the ECI instances. Selecting multiple vSwitches enables multi-zone deployment.

      • vsw-2zevanrscmoiaxr******

      • vsw-2ze23nqzig8inpr******

      Associate Classic Load Balancer (CLB)

      After you associate a CLB instance, instances added to the scaling group are automatically added as backend servers of the CLB instance. Select the CLB instance that you created.

      lb-2zerkwijjdblq4n******

  3. Create and enable a scaling configuration.

    1. On the Scaling Groups page, find the target scaling group, and in the Instance Configuration Source column, click Create Scaling Configuration.

    2. On the page that appears, complete the scaling configuration.

      Note the following parameters when you create the configuration. For more information, see Create a scaling configuration (ECI instances).

      Configuration module

      Parameter

      Description

      Container Group Configuration

      CPU, Memory

      Select vCPU and memory specifications as needed. This example uses 4 vCPU and 8 GiB of memory.

      Storage

      If your application needs to use configuration items or external storage such as NAS, add a volume. For more information, see Volumes.

      Temporary Storage Size

      By default, an ECI instance provides 30 GiB of free storage space. If this does not meet your needs, you can add more temporary storage space. For more information, see Increase the size of the temporary storage space.

      Image Cache

      An image cache speeds up image pulls and eliminates redundant downloads during scale-outs. We recommend enabling this feature. For more information, see Image cache overview.

      Container Configuration

      Image

      Select the application container image and version that you created.

      Startup Command

      If a startup command is not set in the image, you can configure one here. Otherwise, the container might fail to start or exit due to a startup exception. For more information, see Set startup commands and arguments for a container.

      Environment Variable

      In addition to the environment variables included in the image, you can add custom environment variables. We recommend that you use environment variables to customize log collection configurations. For more information, see Customize ECI log collection.

      For example, configure the following environment variables for log collection:

      • aliyun_logs_store-ess: Set the value to stdout to collect standard output logs.

      • aliyun_logs_store-ess_project: Set the value to the log project name, such as ess-eci-asg-bp194y7437g179lfny1h.

      • aliyun_logs_store-ess_machinegroup: Set the value to the machine group name, such as ess-eci-asg-bp194y7437g179lfny1h.

      After you enable the scaling configuration, the system automatically creates ECI instances to meet the minimum number of instances required by the scaling group. All ECI instances in the group are added to the backend server group of the CLB instance.

  4. Create a scaling rule.

    1. On the Scaling Groups page, find the target scaling group and click its ID.

    2. Click the Scaling Rules and Event-triggered Tasks tab.

    3. On the Scaling Rules tab, click Create Scaling Rule.

    4. In the dialog box that appears, complete the scaling rule configuration.

      A scaling rule triggers a scaling activity. This example uses a target tracking rule with CPU utilization as the metric and a target value of 60%. Note the following parameters when you create the rule.

      Parameter

      Description

      Example

      Rule Type

      The options are Simple Rule and Target Tracking Rule:

      • Simple Rule: Specifies the number of instances to add or remove, or adjusts the number of instances to a specified value.

      • Target Tracking Rule: Lets you select a metric and set a target value. The system automatically adds or removes instances to keep the metric value near the target value.

      Target Tracking Rule

      Metric Type

      When you set a target tracking rule, you must select a metric to monitor.

      CPU Utilization

      Target Value

      When you set a target tracking rule, you must set a target value for the metric.

      60

      Ramp-up Period

      Instances in a ramp-up period are added to the scaling group but do not report monitoring data to CloudMonitor. When the number of instances to scale is dynamically calculated, instances in a ramp-up period are not counted as existing instances. This prevents metric fluctuations.

      30

      Number of times for scale-out

      The number of times the rule conditions must be met to trigger a scale-out.

      1

      Number of times for scale-in

      The number of times the rule conditions must be met to trigger a scale-in.

      10

Test the elastic scaling effect

By default, a scaling group holds the expected number of instances and does not scale. After you configure a scaling rule, Auto Scaling adjusts capacity automatically when the metric deviates from the target.

In this example, the scaling rule targets 60% CPU utilization. A scale-out is triggered if CPU utilization exceeds 60% once. A scale-in is triggered if CPU utilization stays below 60% for 10 consecutive evaluation periods.

The test procedure is as follows:

  1. Simulate stress testing by sending requests to the application at a frequency of 500 times per minute.

    For the sample code for the simulated stress test, see HttpClientHelper.

    1. In the Simple Log Service console, view the logs. You can see that the application container writes an output log each time it processes a request.

      Log collection was configured using an environment variable when the scaling configuration was created. Therefore, the standard output logs of the container are collected in the configured store-ess.

      In Logstore store-ess, the query returns 929 records. The _source_ field is stdout and the content field value is submit job success, indicating that the application container has successfully processed the requests.

    2. In the Elastic Container Instance console, view the monitoring metrics of the ECI instance. You can see that the CPU utilization has reached 100%.ess-eci-3

    3. Check whether the scale-out was successful.

      • A scale-out is triggered.

        In the Instance List of the scaling group, on the Auto Created tab, two ECI instances are displayed: one with the status In Service and health check Healthy, and the other with the status Pending and health check Unhealthy (still being scaled out).

      • The scale-out is successful.

        After the scale-out is complete, the Auto Created instance list of the scaling group displays two ECI instances. Both instances are in the In Service status with Healthy health check results and a default CLB weight of 50.

        The application image used in this example is about 250 MB. Because the automatic image cache feature was enabled when the scaling configuration was created, it takes only a few seconds to scale out a single ECI instance.

        After the scale-out is triggered, the Events tab of the new ECI instance shows 4 Normal events in sequence: image cache hit (imc-bp1eofik7lyc7b2p82eb), container image already exists on node, created container container-1, and started container container-1. This indicates that the instance started quickly by using the image cache.

      • Monitoring metrics after the scale-out

        After the scale-out, the CPU utilization stabilizes between 50% and 60%.

        ess-eci-7

  2. Stop sending requests.

    The application payload drops to 0, which triggers a scale-in. After the scale-in is successful, only the minimum number of instances are retained in the scaling group.

Configure an application upgrade

Auto Scaling does not currently support rolling upgrades for ECI instances. Instead, you can perform a phased release. The following example upgrades an application from v4 to v5:

  1. Clone scaling group v4 to create a new scaling group named v5.ess-eci-8

    Before the phased release, scaling group eci-test-boot-v4 has 10 total instances (minimum 0, maximum 50, expected 10), and scaling group eci-test-boot-v5 has 1 total instance (minimum 1, maximum 50, expected 1). Both scaling groups are of the ECI type and are enabled.

  2. Modify the settings of scaling groups v4 and v5 to scale out v5 and scale in v4.

    1. Modify the scaling rule of scaling group v5. Change the CPU utilization threshold to 30%. This means a scale-out is triggered if the CPU utilization is higher than 30%.

    2. Change the minimum number of instances for scaling group v4 to 0.

    Because the scaling rules of the two scaling groups conflict, v5 continuously scales out. This causes the CPU utilization to decrease, which eventually triggers a scale-in for v4. The process is as follows:ess-eci-9

    After a period of time, the number of instances in v4 scales in to 0. The number of instances in v5 scales out until the CPU utilization is about 30%. After the modification, the total instance count and expected instance count of scaling group eci-test-boot-v4 are both 0, and the total instance count and expected instance count of scaling group eci-test-boot-v5 are both 20, indicating that all traffic has been switched to v5.

  3. Modify the scaling rule of scaling group v5 by changing the CPU utilization threshold back to 60%.

    Because the overall load is low (about 30% CPU utilization), you need to change the CPU utilization threshold in the v5 scaling rule back to 60%. This allows the v5 instances to scale in to a normal level.

    After the modification, scaling group eci-test-boot-v5 has 10 total instances, 10 expected instances, 1 minimum instance, and 50 maximum instances. Scaling group eci-test-boot-v4 has 0 total instances, 0 expected instances, 0 minimum instances, and 50 maximum instances. Both scaling groups are of the ECI type and are enabled.

After the configuration is complete, you can view the logs to see the phased release process that is based on the automatic scaling capability of ESS.

  • v4

    In Logstore store-ess, the query * not v5 filters out v5 logs (showing only v4 logs). The query returns 8,023 logs, mainly concentrated between 16:04 and 16:32, then gradually decreasing to zero, indicating that v4 instances have been completely scaled in.

  • v5

    In the Simple Log Service (SLS) console, in Logstore store-ess, the query * and v5 for the time range 2021-12-21 16:04:54 to 2021-12-21 17:34:49 returns 26,431 logs. The histogram shows the log volume increasing from around 16:20 and stabilizing at approximately 1,200 to 1,400 after 16:32.

This process has the following features:

  • During the phased release, the application load does not increase suddenly. The transition is smooth.

  • The phased release is fully controllable. The instance management capability of ESS lets you precisely control the replica ratio of old and new versions, which controls the traffic ratio. You can roll back at any time by reverting to a previous container image version.

  • The postStart and preStop lifecycle hooks of ECI containers are used to gracefully bring the application online and offline.

References

For more information about the features of Auto Scaling, see Common Auto Scaling operations.