Configure container startup and exit order

更新时间:
复制 MD 格式

This topic describes how to use environment variables to configure startup and exit priorities for containers within an ECI pod, ensuring they start and exit in a specified order.

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

The following environment variables control the container startup and exit order:

Parameter

Environment variable

Description

Container startup priority

ECI_CONTAINER_LAUNCH_PRIORITY

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

  • A container with a higher priority always starts before a container with a lower priority. Containers with the same priority start in parallel, and their startup order is not guaranteed.

  • If a container (for example, B) depends on a service from another container (A), you must set a higher startup priority for container A and also configure a readiness probe for it. This probe ensures that container B starts only after container A is ready.

Container exit priority

ECI_CONTAINER_EXIT_PRIORITY

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

  • A container with a higher priority always exits before a container with a lower priority. Containers with the same priority exit in parallel, and their exit order is not guaranteed.

Important

Configuring the container exit order may cause the pod's total termination time to exceed the TerminationGracePeriodSeconds value in its pod specification.

Examples

Configure container startup order

  1. Use the following command to create a Deployment from a YAML configuration file.

    kubectl apply -f test-launch.yaml

    The following test-launch.yaml content defines a Deployment with one replica. The pod contains two containers, c1 and c2, where c1 has a higher startup priority than c2. A readiness probe is configured for c1 to ensure that c2 starts only after c1 is ready.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: test-launch
      labels:
        app: test
    spec:
      replicas: 1 
      selector:
        matchLabels:
          app: test
      template:
        metadata:
          labels:
            app: test
        spec:
          containers:
          - image: registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
            name: c1
            env:
            - name: ECI_CONTAINER_LAUNCH_PRIORITY
              value: "1000"
            readinessProbe:                  
              httpGet:
                path: /
                port: 80
              initialDelaySeconds: 30     
              periodSeconds: 3
          - image: registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
            name: c2
            env:
            - name: ECI_CONTAINER_LAUNCH_PRIORITY
              value: "0"
            args:
            - /bin/sh
            - -c
            - sleep 3600s
  2. Check the container startup times in the pod's status.

    kubectl describe pod <pod name>

    The Containers section of the output shows that the c2 container starts after the c1 container. An example follows:

    Containers:
    c1:
        Container ID:   containerd://779ec5b6d9ad1164c929ab0b8bbc4a3a3b24fe1f9654867e842f2dcfe7b24beb
        Image:          registry.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
        Image ID:       registry.cn-shanghai.aliyuncs.com/eci_open/nginx@sha256:2ec3c026183996087f26c6b3
        Port:           <none>
        Host Port:      <none>
        State:          Running
          Started:      Wed, 16 Aug 2023 07:11:48 +0000
        Ready:          True
        Restart Count:  0
        Readiness:      http-get http://:80/ delay=30s timeout=1s period=3s #success=1 #failure=3
        Environment:
          ECI_CONTAINER_LAUNCH_PRIORITY:  1000
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-2qkqp (ro)
    c2:
        Container ID:   containerd://fe6261cc2e659ee9afb07c2efd516250aeb854544504d66ac011b557e94a0cb6
        Image:          registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
        Image ID:       registry.cn-shanghai.aliyuncs.com/eci_open/nginx@sha256:2ec3c026183996087f26c6b38
        Port:           <none>
        Host Port:      <none>
        Args:
          /bin/sh
          -c
          sleep 3600s
        State:          Running
          Started:      Wed, 16 Aug 2023 07:12:20 +0000
        Ready:          True
        Restart Count:  0
        Environment:
          ECI_CONTAINER_LAUNCH_PRIORITY:  0

Configure container exit order

  1. Use the following command to create a Deployment from a YAML configuration file.

    kubectl apply -f test-exit.yaml

    The following test-exit.yaml content defines a Deployment with one replica. The pod contains three containers, with exit priorities set in descending order: c1, c2, and c3.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: test-exit
      labels:
        app: test
    spec:
      replicas: 1 
      selector:
        matchLabels:
          app: test
      template:
        metadata:
          labels:
            app: test
        spec:
          containers:
          - image: registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
            name: c1
            env:
            - name: ECI_CONTAINER_EXIT_PRIORITY
              value: "1000"
          - image: registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
            name: c2
            env:
            - name: ECI_CONTAINER_EXIT_PRIORITY
              value: "0"
            args:
            - /bin/sh
            - -c
            - sleep 3600s
          - image: registry-vpc.cn-shanghai.aliyuncs.com/eci_open/nginx:alpine
            name: c3
            env:
            - name: ECI_CONTAINER_EXIT_PRIORITY
              value: "-1000"
            args:
            - /bin/sh
            - -c
            - sleep 3600s
  2. Delete the pod and then observe the order of the Killing events for the containers.

    Note

    These events are transient and may be unavailable after the pod is deleted.

    kubectl describe pod <pod name>

    The Events section of the output shows the containers stopping in order of their exit priority: c1, c2, and then c3. An example follows:

    Events:
      Type     Reason                  Age    From               Message
      ----     ------                  ----   ----               -------
      Warning  MissingClusterDNS       2m10s  virtual-kubelet    pod: default/test-exit-pr
    and cannot create Pod using "ClusterFirst" policy. Falling back to "Default" policy.
      Normal   DefaultInstanceTypeMatch  2m10s  EciService       [eci.containergroup]The de
      Warning  ImageCacheMissed        2m9s   EciService         [eci.imagecache]Missed ima
      Normal   ImageCacheAutoCreated   2m9s   EciService         [eci.imagecache]Image cach
      Normal   Pulling                 115s   kubelet            Pulling image "registry.cn
      Normal   Pulled                  113s   kubelet            Successfully pulled image
    ing)
      Normal   Created                 113s   kubelet            Created container c1
      Normal   Started                 113s   kubelet            Started container c1
      Normal   Pulled                  113s   kubelet            Container image "registry.
      Normal   Created                 113s   kubelet            Created container c2
      Normal   Started                 113s   kubelet            Started container c2
      Normal   Pulled                  113s   kubelet            Container image "registry.
      Normal   Created                 112s   kubelet            Created container c3
      Normal   Started                 112s   kubelet            Started container c3
      Normal   Killing                 37s    kubelet            Stopping container c1
      Normal   Killing                 37s    kubelet            Stopping container c2
      Normal   Killing                 7s     kubelet            Stopping container c3