Updated on 2024-11-08 GMT+08:00

Configuring the Container Lifecycle

Scenario

CCE provides hooks for container lifecycle management. For example, you can use a hook to make a container perform a specific operation before stopping.

CCE provides the following hooks:

  • Startup Command: executed to start a container. For details, see Startup Command.
  • Post-Start: executed immediately after a container is started. For details, see Post-Start.
  • Pre-Stop: executed before a container is stopped. This hook helps you ensure that the services running on the pods can be completed in advance in case of pod upgrade or deletion. For details, see Pre-Stop.

Startup Command

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.

A Docker image has metadata that stores image information. If lifecycle commands and arguments are not set, CCE runs the default commands and arguments (Docker instructions ENTRYPOINT and CMD) provided during image creation.

If the commands and arguments used to run a container are set during workload creation, the default commands ENTRYPOINT and CMD are overwritten during image build.

Table 1 Commands and arguments used to run a container

Image ENTRYPOINT

Image CMD

Command to Run a Container

Arguments 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]

  1. Log in to the CCE console. When creating a workload, configure container information and select Lifecycle.
  2. On the Startup Command tab, enter a command and arguments.

    Table 2 Container startup command

    Parameter

    Description

    Command

    Enter an executable command, for example, /run/server.

    If there are multiple executable commands, write them on different lines.

    NOTE:

    If there are multiple commands, it is recommended that you run /bin/sh or other shell commands and use other commands as parameters.

    Args

    Enter an argument for the command, for example, --port=8080.

    If there are multiple arguments, write them on different lines.

Post-Start

  1. Log in to the CCE console. When creating a workload, configure container information and select Lifecycle.
  2. On the Post-Start tab, configure the parameters.

    Table 3 Post-Start parameters

    Parameter

    Description

    CLI

    The tool for running the commands 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 them into a script for execution. Commands that are executed in the backend 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.

Pre-Stop

  1. Log in to the CCE console. When creating a workload, configure container information and select Lifecycle.
  2. On the Pre-Stop tab, configure the parameters.

    Table 4 Pre-Stop parameters

    Parameter

    Description

    CLI

    The tool for running the commands 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 them into a script for execution.

    Example command:

    exec: 
      command: 
      - /uninstall.sh 
      - uninstall_agent

    Enter /uninstall uninstall_agent in the script. This command indicates that uninstall.sh will be executed before the container completes its execution and stops running.

YAML Example

Nginx is used as an example to describe how to set the container lifecycle.

In the following configuration file, there is a Post-Start command (install.sh) in the /bin/bash directory and a PreStop command (uninstall.sh).

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