After you associate a Server Load Balancer (SLB) instance with a scaling group, the Elastic Compute Service (ECS) instances or elastic container instances in the scaling group are automatically attached to the SLB instance as backend servers to process client requests. Alibaba Cloud SLB provides the following types of load balancers: Classic Load Balancer (CLB), Application Load Balancer (ALB), Network Load Balancer (NLB). You can associate the preceding load balancers with your scaling group based on your business requirements. This topic describes how to use the lifecycle hook feature of Auto Scaling to specify a period of time to perform the required operations on instances in a scaling group. This helps ensure service availability.
Basic concepts
The following table describes the concepts that are used in this topic.
Concept | Description | References |
SLB | SLB is a service that forwards network traffic to backend servers to increase the throughput of your application. You can use SLB to prevent service interruptions that are caused by single points of failure (SPOFs) and improve service availability. Alibaba Cloud SLB provides the following types of load balancers: CLB, ALB, and NLB. | |
Lifecycle hook | A lifecycle hook is a tool that is used to manage the lifecycles of ECS instances or elastic container instances in a scaling group. |
Procedure
In this topic, a CLB instance is used as an example to describe the effects of the lifecycle hook feature on service availability of applications in different scenarios and how to ensure the service availability.
Before you perform the following steps, make sure that a CLB instance is associated with your scaling group. You can also associate an ALB server group or an NLB server group with your scaling group based on your business requirements. For more information, see Associate SLB instances with or disassociate SLB instances from a scaling group.
Scenario 1: Scale-out
Effect comparison
The scale-out effects vary based on whether a scaling group has lifecycle hooks. The following table describes the difference between using and not using a lifecycle hook.
Condition | Description |
Not using a lifecycle hook | During a scale-out, Auto Scaling immediately adds ECS instances or elastic container instances to the scaling group. Auto Scaling also attaches the instances to the associated CLB instance as backend servers. Then, the ECS instances or elastic container instances can provide services. However, the application that you deploy on the ECS instances or elastic container instances may require a period of time to start. If the CLB instance forwards client requests to the ECS instances or elastic container instances before the application starts, the instances cannot respond to the client requests and provide services. |
Using a lifecycle hook | During a scale-out, Auto Scaling puts ECS instances or elastic container instances into the Pending Add state before Auto Scaling attaches the ECS instances or elastic container instances to the associated CLB instance as backend servers. When the application that you deploy on the ECS instances or elastic container instances starts and the lifecycle hook expires, the ECS instances or elastic container instances are automatically attached to the associated CLB instance as backend servers to provide services. |
Usage notes
If you want to use the lifecycle hook feature, you must create a lifecycle hook. For more information, see Manage lifecycle hooks.
When you create a lifecycle hook for scale-outs, you must perform the following operations:
Set the Scaling Activity parameter to Scale-out Event.
Specify a period of time during which your application can start as expected as the value of the Timeout Period parameter. The value of the Timeout Period parameter must be an integer that ranges from 30 to 21600. Unit: seconds.
NoteIf you want the timeout period of a lifecycle hook to end ahead of schedule, you can call the CompleteLifecycleAction API operation. For more information, see CompleteLifecycleAction.
Set the Default Execution Policy parameter to Continue.
After the lifecycle hook that you created takes effect, the ECS instances or elastic container instances that are being scaled out enter the Pending Add state. The ECS instances or elastic container instances exit the Pending Add state when the timeout period ends. During the timeout period, the application that is deployed on the ECS instances or elastic container instances must complete startup. After the ECS instances or elastic container instances exit the Pending Add state, the instances are completely added to the scaling group and also attached to the associated CLB instance as backend servers. When the ECS instances or elastic container instances enter the In Service state, the ECS instances or elastic container instances can provide services. This ensures service availability of applications.
Scenario 2: Scale-in
Effect comparison
The scale-in effects also vary based on whether a scaling group has lifecycle hooks. The following table describes the difference between using and not using a lifecycle hook.
Condition | Description |
Not using a lifecycle hook | During a scale-in, Auto Scaling immediately removes ECS instances or elastic container instances from the scaling group. Auto Scaling also detaches the ECS instances or elastic container instances from the associated CLB instance. Then, the ECS instances or elastic container instances stop providing services. However, the ECS instances or elastic container instances that are being removed from the scaling group may have unprocessed client requests. This may cause client access exceptions. |
Using a lifecycle hook | During a scale-in, Auto Scaling puts ECS instances or elastic container instances into the Pending Remove state after Auto Scaling detaches the ECS instances or elastic container instances from the associated CLB instance. After the ECS instances or elastic container instances process all the existing client requests and the timeout period of the lifecycle hook ends, Auto Scaling removes the ECS instances or elastic container instances from the scaling group. This ensures successful access from clients to applications. |
Usage notes
We recommend that you create lifecycle hooks for scaling groups that have ongoing scale-ins. For more information, see Manage lifecycle hooks.
When you create a lifecycle hook for scale-ins, you must perform the following operations:
Set the Scaling Activity parameter to Scale-in Event.
Specify a period of time during which your application can complete all the existing client requests as the value of the Timeout Period parameter. The value of the Timeout Period parameter must be an integer that ranges from 30 to 21600. Unit: seconds.
NoteIf you want to end the timeout period of a lifecycle hook ahead of schedule, you can call the
CompleteLifecycleAction
API operation. For more information, see CompleteLifecycleAction.
After the lifecycle hook that you created takes effect, the ECS instances or elastic container instances that are being scaled in enter the Pending Remove state. The ECS instances or elastic container instances exit the Pending Remove state when the timeout period ends. During the timeout period of the lifecycle hook, the ECS instances or elastic container instances no longer receive new client requests. The ECS instances or elastic container instances process only the existing client requests that are received before the lifecycle hook takes effect. When the timeout period ends, the ECS instances or elastic container instances are completely removed from the scaling group. This ensures service availability of applications.