• Cloud Container Engine

cce
  1. Help Center
  2. Cloud Container Engine
  3. User Guide 2.0
  4. Application Management
  5. Setting Container Lifecycle

Setting Container Lifecycle

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:

  • Startup Command: executed to start a container. For details, see Setting Container Startup Command.
  • Post-Start Processing: executed immediately after an application is started.
  • Pre-Stop Processing: executed before an application is stopped. The pre-stop processing function helps you ensure that the services running in the instances can be completed in advance in the case of application upgrade or instance deletion.

Commands and Parameters Used to Run a Container

The Docker image has metadata that stores image information. If lifecycle commands and parameters are not set, CCE runs the default commands and parameters, that is, Docker native commands Entrypoint and CMD, provided during image creation. For details, see the description of Entrypoint and CMD.

If the commands and parameters used to run a container are set during application creation, the default commands Entrypoint and CMD are overwritten during image building. The rules are as follows:

Table 1 Commands and parameters used to run a container

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]

Post-start Processing

  1. Log in to the CCE console. Expand Lifecycle when creating an application.
  2. Set the parameters for processing after startup, as listed in Table 2.

    Table 2 Container lifecycle parameters

    Parameter

    Description

    CLI Mode

    Set the command to be executed in the container to the command to be executed. 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, find an executable program in the default path. If multiple commands need to be executed, you are advised to write the commands in the script for execution.

    For example, the following command needs to be executed:

    exec: 
      command: 
      - /install.sh 
      - install_agent

    Enter /install install_agent in the script.

    This command indicates that install_agent needs to be executed after the container is created successfully.

    HTTP Request Mode

    HTTP invocation request. The related parameters are described as follows:

    • Path: (optional) request URL.
    • Port: (mandatory) request port.
    • Host address: (optional) IP address of the request. The default value is the IP address of the node where the container resides.

Pre-stop Processing

  1. Log in to the CCE console. Click the Pre-Stop Processing tab during the lifecycle configuration when an application is created.
  2. Set pre-stop parameters, as shown in Table 2.

Example YAML for Setting the Container Lifecycle

This section uses an Nginx application as an example to describe how to set the container lifecycle.

Prerequisites

You have configured the kubectl command and connected an ECS to the cluster. For details, see Connecting to a Kubernetes Cluster Using kubectl.

Procedure

  1. Log in to the ECS on which the kubectl commands have been configured.
  2. Create and edit the nginx-deployment.yaml file. nginx-deployment.yaml is an example file name, and you can change it as required.

    vi nginx-deployment.yaml

    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: extensions/v1beta1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: nginx
      strategy:
        type: RollingUpdate
      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