Configure application lifecycle management

更新时间:
复制 MD 格式

If you are familiar with Kubernetes and need to perform specific actions before your application containers start or stop, such as deploying resources or performing a graceful shutdown, you can configure application lifecycle management. This topic describes how to configure PostStart, PreStop, and TerminationGracePeriodSeconds for your Serverless App Engine (SAE) applications.

Overview

  • A pod hook (or container hook), managed by the Kubernetes component kubelet, runs custom tasks at key stages of a container's lifecycle. These stages include before the container process starts (PostStart) and before it terminates (PreStop). Hooks allow Kubernetes to manage the container lifecycle with more flexibility and precision. For more information, see Container Lifecycle Hooks.

    • PostStart: A lifecycle hook that runs immediately after a container is created to notify it of this event. No parameters are passed to the hook handler. If the handler fails, the container is terminated and restarted according to its restart policy.

    • PreStop: A lifecycle hook triggered before a container is terminated, typically used to run cleanup tasks or perform a graceful shutdown. The hook handler must complete its execution before Kubernetes sends a termination signal (like SIGTERM) to the container. Regardless of the handler's outcome, once it completes, Kubernetes sends the SIGTERM signal and waits for a specified period before forcibly terminating the container.

  • TerminationGracePeriodSeconds: This setting specifies the grace period for a container's termination, allowing it to complete cleanup and shutdown operations before being forcibly stopped. This improves stability by preventing issues like data loss, request failures, and service unavailability caused by abrupt termination. When combined with a PreStop hook, it enables a complete graceful shutdown process.

Entry points

The entry point varies by scenario:

Create application

  1. Log on to the SAE console. In the left-side navigation pane, choose . Select a region and namespace, and then click Create Application.

  2. On the Basic Information wizard page, configure the required parameters and click Next: Advanced Settings.

  3. On the Advanced Settings page, find and expand the Application Lifecycle Management section.

Modify a running application

Warning

Redeploying an application restarts it. To avoid service interruptions, perform deployments during off-peak hours.

  1. Log on to the SAE console. In the left-side navigation pane, choose . Select the region and namespace, and then click the name of your application.

  2. On the application's Basic Information page, click Deploy Application.

  3. On the Deploy Application page, find and expand the Application Lifecycle Management section.

Modify a stopped application

  1. Log on to the SAE console. In the left-side navigation pane, choose . Select the region and namespace, and then click the name of your application.

  2. On the application's Basic Information page, click Modify Application Configurations.

  3. On the Modify Application Configurations page, find and expand the Application Lifecycle Management section.

Procedure

In the Application Lifecycle Management section, configure PostStart, PreStop, and TerminationGracePeriodSeconds as needed.

We recommend that you configure all three settings to establish a complete lifecycle management loop for your containers, ensuring smooth service startup and shutdown, orderly resource release, and zero-downtime operations.

PostStart

In the Application Lifecycle Management section, configure the PostStart hook on the PostStart tab.

Note

SAE provides two shell interpreters:

  • >_ /bin/sh

  • >_ /bin/bash

For example, before the application starts, write the content Hello from the postStart handler to the /usr/share/message file.

Select >_/bin/sh and enter the following command in the code editor: echo "Hello from the postStart handler" > /usr/share/message.

In a production environment, configure the settings based on your business requirements. The example provided in this topic is for minimal verification purposes only and must not be used in a production environment.

Verify the result:

After the application is created or redeployed, use Webshell to open the instance's terminal and verify that the /usr/share/message file exists and contains the expected content.

[~]$cat /usr/share/message
Hello from the postStart handler
[~]$

PreStop

In the Application Lifecycle Management section, configure the PreStop hook on the PreStop tab.

Note

SAE provides two shell interpreters:

  • >_ /bin/sh

  • >_ /bin/bash

For example, before the container terminates, run a script to write Perform cleanup to the /cleanup.log file and then wait 100 seconds for verification.

In a production environment, configure the settings based on your business requirements. The example provided in this topic is for minimal verification purposes only and must not be used in a production environment.

Select >_/bin/sh and enter the following command in the code editor: echo "Perform cleanup" > /cleanup.log && sleep 100.

Verify the result:

On the Basic Information page of the application, click Stop Application. Then, use Webshell to open the instance's terminal and verify that the /cleanup.log file exists and contains the expected content.

[/]$cat /cleanup.log
Perform cleanup
[/]$

TerminationGracePeriodSeconds

In the Application Lifecycle Management section, on the TerminationGracePeriodSeconds tab, set the graceful shutdown timeout to 60 seconds.

If the graceful shutdown timeout is shorter than the time required for the container to terminate gracefully, the container is forcibly terminated (Signal: SIGKILL, exit code: 137). This required time includes both the PreStop hook execution and the container's handling of the SIGTERM signal. For example, if the container requires 100 seconds to terminate gracefully but the timeout is set to 60 seconds, the container is killed before its graceful termination can complete.

Verify the result:

In this example, the graceful shutdown timeout is intentionally set to be shorter than the container's graceful shutdown time for verification purposes.

On the Basic Information page of the application, click Stop Application. Then, use Webshell to open the instance's terminal and verify that the container reports exit code 137.

During graceful shutdown, the container remains in the Terminating state until the configured timeout expires.
[/]${"metadata":{},"status":"Failure","message":"command terminated with non-zero exit code: error executing command [env COLUMNS=137 ROWS=46 LINES=46 /bin/sh -i -c /bin/bash || clear;/bin/sh], exit code 137","reason":"NonZeroExitCode","details":{"causes":[{"reason":"ExitCode","message":"137"}]}}