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 | /run/server | Enter one or more executable commands. If you need multiple commands, separate them on different lines. NOTE: In the case of multiple commands, you are advised to run /bin/sh or other shell commands. Other commands are used as parameters. |
|
Args | --port=8080 | 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 | /install.sh install_agent | 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. |
|
HTTP request | Path: /nginx Port: 80 Host: 127.0.0.1 | 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 | /uninstall.sh uninstall_agent | 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 | Path: /nginx Port: 80 Host: 127.0.0.1 | Used for initiating an HTTP request. The related parameters are described as follows:
|
|
vim lifecycle.yaml
The file content is as follows:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxspec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- image: nginxcommand:- sleep 3600 #Startup command: suspends the current process for 3,600s.imagePullPolicy: Alwayslifecycle:postStart:exec:command:- /bin/bash- install.sh #Post-start command: Run the install.sh command in /bin/bash.preStop:exec:command:- /bin/bash- uninstall.sh #Pre-stop command: Run the uninstall.sh command in /bin/bash.name: nginximagePullSecrets:- name: default-secret
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