All Products
Search
Document Center

Elastic Container Instance:Configure the startup and exit priorities of containers

Last Updated:Mar 25, 2024

This topic describes how to configure the startup and exit priorities of containers in an elastic container instance. This way, the containers start and exit in sequence.

Feature description

By default, containers in an elastic container instance concurrently start and exit. In some scenarios, containers in an instance may have a dependency relationship. A container must start after another container starts, or a container must exit after another container exits. For example:

  • In Istio service governance scenarios, the istio-proxy container must be ready before data transfers are generated on application containers, and the istio-proxy container can exit only after application containers exit.

  • In log collection scenarios, the log container must be ready before logs are generated on application containers, and the log container can exit only after application containers exit.

In the preceding scenarios, Elastic Container Instance allows you to configure the startup and exit priorities for containers in an elastic container instance by using environment variables. You can add environment variables to containers based on your business requirements to ensure that containers start or exit in sequence.

Configuration description

The following table describes the environment variables that can be used to configure the startup and exit priorities of containers:

Item

Name of the environment variable

Description

Container startup priority

ECI_CONTAINER_LAUNCH_PRIORITY

  • Valid values: -1000 to 1000. Default value: 0. A larger value indicates a higher startup priority.

  • Containers with a higher priority are guaranteed to start before containers with a lower priority start. Containers with the same priority are not guaranteed to start in sequence, and they concurrently start.

  • If a container depends on another container to provide services, you must configure a readiness probe for another container. For example, if Container B depends on Container A to provide services, you must configure a higher startup priority and readiness probe for Container A to ensure that Container B can start only after Container A is ready.

Container exit priority

ECI_CONTAINER_EXIT_PRIORITY

  • Valid values: -1000 to 1000. Default value: 0. A larger value indicates a higher exit priority.

  • Containers with a higher priority are guaranteed to exit before containers with a lower priority exit. Containers with the same priority are not guaranteed to exit in sequence, and they concurrently exit.

Important

If you call an API operation to create an elastic container instance, you can set the TerminationGracePeriodSeconds parameter to ensure that the instance exits gracefully. If you configure the sequence in which containers exit, the duration in which the instance is destroyed may be longer than the duration that is specified in TerminationGracePeriodSeconds.

Configuration examples

Configure the startup priorities of containers

API mode

  1. Create an elastic container instance for which you configure the container startup priorities.

    The following example shows the parameters that are used if you call the CreateContainerGroup API operation to create an elastic container instance. The instance contains two containers. The c1 container has a higher startup priority than the c2 container, and the c1 container is configured with a readiness probe. In this case, the c2 container starts only after the c1 container is ready.

    {
      "RegionId": "cn-beijing",
      "SecurityGroupId": "sg-2ze63v3jtm8e6syi****",
      "VSwitchId": "vsw-2ze94pjtfuj9vaymf****",
      "ContainerGroupName": "test-launch",
      "Container": [
        {
          "Name": "c1",
          "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine",
          "EnvironmentVar": [
            {
              "Key": "ECI_CONTAINER_LAUNCH_PRIORITY",
              "Value": "1000"
            }
          ],
          "ReadinessProbe.PeriodSeconds": 3,
          "ReadinessProbe.HttpGet.Path": "/",
          "ReadinessProbe.HttpGet.Port": 80
        },
        {
          "Name": "c2",
          "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine",
          "EnvironmentVar": [
            {
              "Key": "ECI_CONTAINER_LAUNCH_PRIORITY",
              "Value": "0"
            }
          ],
          "Arg": [
            "/bin/sh",
            "-c",
            "sleep 3600s"
          ]
        }
      ]
    }
  2. View the instance details and check the startup time of each container.

    Call the DescribeContainerGroups API operation to query instance details. View the details of each container in the Containers section of the response. The CurrentState.StartTime parameter shows that the startup time of the c2 container is later than the startup time of the c1 container.

Console mode

When you create an elastic container instance in the Elastic Container Instance console, you can configure an environment variable in the Advanced Settings section of each container to set the startup priority of the container. You can also set a readiness probe for the container by turning on Health Check and clicking the Readiness Probe tab.

容器启动-控制台.png

After the instance is created, you can view the events of the instance to verify the startup sequence of the containers.

Configure the exit priorities of containers

API mode

  1. Create an elastic container instance for which you configure the container exit priorities.

    The following example shows the parameters that are used if you call the CreateContainerGroup API operation to create an elastic container instance. The TerminationGracePeriodSeconds parameter is set to 30s. The instance contains the c1, c2, and c3 containers, which have descending exit priorities.

    {
      "RegionId": "cn-beijing",
      "ContainerGroupName": "test-exit",
      "SecurityGroupId": "sg-2ze63v3jtm8e6syi****",
      "VSwitchId": "vsw-2ze94pjtfuj9vaymf****",
      "TerminationGracePeriodSeconds": 30,
      "Container": [
        {
          "Name": "c1",
          "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine",
          "EnvironmentVar": [
            {
              "Key": "ECI_CONTAINER_EXIT_PRIORITY",
              "Value": "1000"
            }
          ]
        },
        {
          "Name": "c2",
          "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine",
          "EnvironmentVar": [
            {
              "Key": "ECI_CONTAINER_EXIT_PRIORITY",
              "Value": "0"
            }
          ],
          "Arg": [
            "/bin/sh",
            "-c",
            "sleep 3600s"
          ]
        },
        {
          "Name": "c3",
          "Image": "registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine",
          "EnvironmentVar": [
            {
              "Key": "ECI_CONTAINER_EXIT_PRIORITY",
              "Value": "-1000"
            }
          ],
          "Arg": [
            "/bin/sh",
            "-c",
            "sleep 3600s"
          ]
        }
      ]
    }
  2. Delete the instance and then observe the killing event sequence of the containers in the instance.

    Note

    If you want to check the sequence in which the containers exit, view the events in a timely manner. You may not be able to observe the events after the elastic container instance is deleted.

    Call the DescribeContainerGroupEvents API operation to query instance events. View the killing event time of each container in the Events section of the response. The c1, c2, and c3 containers exit in sequence.

Console mode

When you create an elastic container instance in the Elastic Container Instance console, you can configure an environment variable in the Advanced Settings section of each container to set the exit priority for the container.

容器退出-控制台.png