If you are familiar with Kubernetes and want to perform specific operations before application containers are started or stopped, you can configure application lifecycle management. For example, you want to deploy resources before the containers run, or gracefully shut down an application before the containers are stopped. This topic describes how to configure application lifecycle management in Serverless App Engine (SAE).
Background information
You can perform the following configurations for an application in SAE:
PostStart Settings: Create tasks to deploy resources or prepare environments after application containers are created but are not running.
PreStop Settings: Create tasks to gracefully shut down the application and notify other systems before application containers are stopped.
TerminationGracePeriodSeconds Settings: Specify a timeout period for application instances. After the specified timeout period elapses, SAE forcibly stops the instances.
Procedure
Configure application lifecycle management when you create an application
Log on to the SAE console.
In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click Create Application.
In the Basic Information step, configure the parameters and click Next: Application Deployment Configurations.
In the Deployment Configurations step, configure the Technology Stack Programming Language and Application Deployment Method parameters and the corresponding settings.
In the Application Lifecycle Management section, configure the parameters as needed.
NoteScript: The first line is a command and the second line is a parameter. Separate multiple parameters with line feeds. Do not enter blank lines.
Configure PostStart Settings.
In the script editor, enter a custom PostStart command.
Configure PreStop Settings.
In the script editor, enter a custom PostStart command.
Configure TerminationGracePeriodSeconds Settings.
In the Timeout Period of Graceful Shutdown field, specify a timeout period. Maximum value: 600. Unit: seconds.
NotePostStart is a container hook. The hook is immediately triggered after a container is created. The hook notifies Kubernetes that the container is created. The hook does not pass parameters to the corresponding hook handler. If the corresponding hook handler fails to be executed, the container is stopped, and the restart policy of the container is used to determine whether to restart the container. For more information, see Container Lifecycle Hooks.
PreStop is a container hook. The hook is triggered before a container is deleted. The corresponding hook handler must be completely executed before the request to delete the container is sent to the Docker daemon. The Docker daemon sends an SGTERN semaphore to itself to delete the container, regardless of the execution result of the corresponding hook handler. For more information, see Container Lifecycle Hooks.
Click Next: Confirm Specifications.
In the Specification Confirmation step, view the details of the application and the fee for the selected specifications. Then, click Confirm.
The Creation Completed step appears. You can click Application Details to go to the Basic Information page of the application.
Verify the result.
For example, write
Hello from the postStart handler
in the script before you start an application.The following script shows a sample PostStart configuration:
/bin/sh -c echo "Hello from the postStart handler" > /usr/share/message
After you deploy the application, use a webshell to check whether the
Hello from the postStart handler
script exists. If the script exists, the configuration is successful.
Configure application lifecycle management when you deploy an application
After you redeploy an application, the application is restarted. To prevent unpredictable errors such as business interruptions, we recommend that you deploy applications during off-peak hours.
The procedure that can be performed to update an application varies based on the number of instances in the application. This section provides an example on how to configure the required features for an application in which the number of instances is greater than or equal to 1. For information about how to update an application in which the number of instances is 0, see Update an application.
Log on to the SAE console.
In the left-side navigation pane, click Applications. In the top navigation bar, select a region. Then, click the name of an application.
In the upper-right corner of the Basic Information page, click Deploy Application.
In the Application Lifecycle Management section, configure the parameters as needed.
NoteScript: The first line is a command and the second line is a parameter. Separate multiple parameters with line feeds. Do not enter blank lines.
Configure PostStart Settings.
In the script editor, enter a custom PostStart command.
Configure PreStop Settings.
In the script editor, enter a custom PostStart command.
Configure TerminationGracePeriodSeconds Settings.
In the Timeout Period of Graceful Shutdown field, specify a timeout period. Maximum value: 600. Unit: seconds.
NotePostStart is a container hook. The hook is immediately triggered after a container is created. The hook notifies Kubernetes that the container is created. The hook does not pass parameters to the corresponding hook handler. If the corresponding hook handler fails to be executed, the container is stopped, and the restart policy of the container is used to determine whether to restart the container. For more information, see Container Lifecycle Hooks.
PreStop is a container hook. The hook is triggered before a container is deleted. The corresponding hook handler must be completely executed before the request to delete the container is sent to the Docker daemon. The Docker daemon sends an SGTERN semaphore to itself to delete the container, regardless of the execution result of the corresponding hook handler. For more information, see Container Lifecycle Hooks.
After you configure the settings, click Confirm.
Verify the result.
For example, write
Hello from the postStart handler
in the script before you start an application.The following script shows a sample PostStart configuration:
/bin/sh -c echo "Hello from the postStart handler" > /usr/share/message
After you deploy the application, use a webshell to check whether the
Hello from the postStart handler
script exists. If the script exists, the configuration is successful.