Container lifecycle hooks are core mechanisms provided by Kubernetes. They enable you to insert custom logic at key phases throughout the container lifecycle. These hooks provide refined process controls over containerized applications, enabling applications to better adapt to the dynamic characteristics of the cloud native environment. CCE provides the following container lifecycle hooks. For more information, see Container Lifecycle Hooks.
A startup command is executed when a container starts. It is used to define the main process of a container. The main process status determines the container lifecycle. If the command fails to be executed and no restart policy is configured, the container will be terminated.
By default, the default command is executed during image start. To run a specific command or rewrite the default image setting, you must perform specific operations. By default, the container executes the startup command preset in the image. Docker images contain a set of metadata fields for defining the startup behavior, including ENTRYPOINT and CMD. If the commands and arguments (specified by Command and Args) are not configured in the container specifications, the default values during image build are used.
If the commands and arguments used to run a container are configured during workload creation, the default commands ENTRYPOINT and CMD are overwritten during image build. The rules are as follows:
Image ENTRYPOINT | Image CMD | Command to Run a Container | Argument to Run a Container | Command Executed |
|---|---|---|---|---|
[touch] | [/root/test] | Not set | Not set | [touch /root/test] |
[touch] | [/root/test] | [mkdir] | Not set | [mkdir] |
[touch] | [/root/test] | Not set | [/opt/test] | [touch /opt/test] |
[touch] | [/root/test] | [mkdir] | [/opt/test] | [mkdir /opt/test] |
Parameter | Example Value | Description | Example YAML |
|---|---|---|---|
Command |
| Enter one or more executable commands. For multiple commands, place each on a separate line. NOTE: When running multiple commands, use /bin/sh or another shell as the executable, and pass the commands as arguments. |
|
Args |
| Enter one or more arguments. If you need multiple arguments, separate them on different lines. |
A PostStart hook, a container lifecycle hook provided by Kubernetes, is used to execute initialization tasks, such as service registration and dynamic configuration generation, immediately after the main process of a container starts. This hook is asynchronously triggered by kubelet and runs in parallel with the main process. Although the PostStart hook is executed asynchronously with the main process of the container, if the PostStart hook takes too long or is suspended, the container may not change to the Running state. If the PostStart hook fails to be executed, the container may fail to start and be terminated.
Processing Method | Example Value | Description | Example YAML |
|---|---|---|---|
CLI | Run the following commands to write the message file to the container's /usr/share directory:
| Used for running the commands in the container. You need to configure Command. The command format is Command Arg[1] Arg[2]..., where Command indicates a system command or a user-defined executable program. If no path is specified, the system searches for the command in the default path. If multiple commands needed to be executed, write them into a script in the container image in advance and invoke the script using this command. The executable command must be executed synchronously or in the frontend. If it is executed asynchronously or in the backend, the lifecycle hook may fail to be executed. CAUTION: Ensure that the postStart script runs to completion and exits properly. Otherwise, subsequent pod status updates and pod deletion will be affected. |
|
HTTP request |
| Used for initiating an HTTP request. The related parameters are described as follows:
|
|
If a container crashes or exits abnormally, the PreStop hook will not be triggered. The pod's termination grace period starts to count down before the PreStop hook is executed. Regardless of the outcome of the hook handler, the container will be terminated within the pod's termination grace period unless the finalizer delays the termination process. Other management operations on the container are blocked until the PreStop hook is complete or the termination grace period is reached.
Parameter | Example Value | Description | Example YAML |
|---|---|---|---|
CLI | Enter the following commands to gracefully stop the Nginx service:
| Used for running the commands in the container. You need to configure Command. The command format is Command Arg[1] Arg[2]..., where Command indicates a system command or a user-defined executable program. If no path is specified, the system searches for the command in the default path. If multiple commands needed to be executed, write them into a script in the container image in advance and invoke the script using this command. |
|
HTTP request |
| Used for initiating an HTTP request. The related parameters are described as follows:
|
|
vim lifecycle.yaml
Example file content:
kubectl create -f lifecycle.yaml
If information similar to the following is displayed, the workload is being created:
deployment.apps/nginx created
kubectl get deployment
If all the pods of the workload are available, the workload has been created.
NAME READY UP-TO-DATE AVAILABLE AGEnginx 1/1 1 1 4m59s