CCE provides callback functions for the lifecycle management of containerized applications. For example, if you want a container to perform a certain operation before stopping, you can register a hook function.
CCE provides the following lifecycle callback functions:
By default, the default command during image start. To run a specific command or rewrite the default image value, you must perform specific settings:
A Docker image has metadata that stores image information. If lifecycle commands and arguments are not set, CCE runs the default commands and arguments, that is, Docker instructions ENTRYPOINT and CMD, provided during image creation.
If the commands and arguments used to run a container are set during application 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 |
Parameters 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] |
Configuration Item |
Procedure |
---|---|
Command |
Enter an executable command, for example, /run/server. If there are multiple executable commands, write them in 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 |
Enter the argument that controls the container running command, for example, --port=8080. If there are multiple arguments, separate them in different lines. |
Parameter |
Description |
---|---|
CLI |
Set commands to be executed in the container for post-start processing. The command format is Command Args[1] Args[2].... Command is a system command or a user-defined executable program. If no path is specified, an executable program in the default path will be selected. If multiple commands need to be executed, write the commands into a script for execution. Commands that are executed in the background or asynchronously are not supported. Example command: exec: command: - /install.sh - install_agent Enter /install install_agent in the script. This command indicates that install.sh will be executed after the container is created successfully. |
HTTP request |
Send an HTTP request for post-start processing. The related parameters are described as follows:
|
Parameter |
Description |
---|---|
CLI |
Set commands to be executed in the container for pre-stop processing. The command format is Command Args[1] Args[2].... Command is a system command or a user-defined executable program. If no path is specified, an executable program in the default path will be selected. If multiple commands need to be executed, write the commands into a script for execution. Example command: exec: command: - /uninstall.sh - uninstall_agent Enter /uninstall uninstall_agent in the script. This command indicates that the uninstall.sh script will be executed before the container completes its execution and stops running. |
HTTP request |
Send an HTTP request for pre-stop processing. The related parameters are described as follows:
|
This section uses Nginx as an example to describe how to set the container lifecycle.
In the following configuration file, the postStart command is defined to run the install.sh command in the /bin/bash directory. preStop is defined to run the uninstall.sh command.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - image: nginx command: - sleep 3600 # Startup command imagePullPolicy: Always lifecycle: postStart: exec: command: - /bin/bash - install.sh # Post-start command preStop: exec: command: - /bin/bash - uninstall.sh # Pre-stop command name: nginx imagePullSecrets: - name: default-secret