You can use environment variables to set startup and exit priorities for containers within an ECI instance. These priorities control the order in which containers start and exit.
How it works
By default, containers in an ECI pod start and exit in parallel, without a specific order. Some scenarios involve container dependencies, requiring one container to start only after another is running, or to exit only after another has stopped. For example:
-
In an Istio service governance scenario, the istio-proxy container must be Ready before the application container generates traffic, and it must exit only after the application container exits.
-
For log collection, the log agent container must be Ready before the application container produces logs, and it must exit only after the application container exits.
To address these scenarios, ECI allows you to set startup and exit priorities for containers by using environment variables, ensuring they start and exit in a specified order.
Configuration
You can control the container startup and exit order with the following environment variables:
|
Parameter |
Environment variable |
Description |
|
Container startup priority |
ECI_CONTAINER_LAUNCH_PRIORITY |
|
|
Container exit priority |
ECI_CONTAINER_EXIT_PRIORITY |
|
When you create an ECI instance using OpenAPI, you can set the TerminationGracePeriodSeconds parameter to ensure a graceful exit. If you also configure a container exit order, the instance's total termination time may exceed the specified TerminationGracePeriodSeconds.
Examples
Configure container startup order
API
-
Create an ECI instance with a defined container startup order.
The following example shows the request parameters used to call the CreateContainerGroup operation. The instance contains two containers, c1 and c2. Container c1 has a higher startup priority than c2, and a readiness probe is configured for c1. This ensures that c2 starts only after c1 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" ] } ] } -
Check the instance details to confirm the startup time of each container.
Call the DescribeContainerGroups operation to query the instance details. In the response, check the
CurrentState.StartTimefield for each container in theContainersobject. The start time of c2 is later than that of c1.
Console
When you create an ECI instance in the Elastic Container Instance console, you can set environment variables in the Advanced Settings of each container, set a startup priority for the container, and set a Readiness Probe (i.e., an Readiness Probe) in the Health Check section.
Set the environment variable name to ECI_CONTAINER_LAUNCH_PRIORITY and the value to 1000. In the Health Check section, configure a readiness probe. Select HTTP Request as the check method. Set the path to /, the port to 80, and the protocol to HTTP.
After the instance is created, you can view the instance events to confirm the startup order of the containers.
Configure container exit order
API
-
Create an ECI instance with a defined container exit order.
The following example shows the request parameters used to call the CreateContainerGroup operation.
TerminationGracePeriodSecondsis set to 30 seconds. The instance contains three containers, and the exit priority is c1 > c2 > c3.{ "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" ] } ] } -
Delete the instance and then observe the order of the Killing events for the containers.
NoteTo confirm the container exit order, you must query the events promptly. Events might not be available after the ECI instance is deleted.
Call the DescribeContainerGroupEvents operation to query instance events. In the
Eventsobject of the response, check the timestamp of the Killing event for each container. The events confirm that the containers exit in the order of c1, c2, and then c3.
Console
When you create an ECI instance in the Elastic Container Instance console, you can set an environment variable in the Advanced Settings section of each container to configure its exit priority.
Set the environment variable name to ECI_CONTAINER_EXIT_PRIORITY and the value to 1000.