このトピックでは、環境変数を使用して Elastic Container Instance (ECI) Pod 内のコンテナの起動優先度と終了優先度を設定し、指定した順序で起動および終了させる方法について説明します。
仕組み
デフォルトでは、ECI Pod 内のコンテナは特定の順序はなく、並列で起動および終了します。シナリオによってはコンテナ間に依存関係があり、あるコンテナが起動して稼働してから別のコンテナを起動する必要がある、またはあるコンテナが停止してから別のコンテナを終了する必要がある場合があります。例を次に示します。
-
Istio のサービスガバナンスのシナリオでは、アプリケーションコンテナがトラフィックを生成する前に istio-proxy コンテナが Ready になっている必要があり、またアプリケーションコンテナの終了後にのみ istio-proxy コンテナが終了する必要があります。
-
ログ収集では、アプリケーションコンテナがログを生成する前にログエージェント コンテナが Ready になっている必要があり、またアプリケーションコンテナの終了後にのみログエージェント コンテナが終了する必要があります。
これらのシナリオに対応するために、ECI では環境変数を使用してコンテナの起動優先度と終了優先度を設定することで、コンテナを指定した順序で起動および終了させることができます。
設定
次の環境変数で、コンテナの起動順序と終了順序を制御します。
|
パラメータ |
環境変数 |
説明 |
|
コンテナの起動優先度 |
ECI_CONTAINER_LAUNCH_PRIORITY |
|
|
コンテナの終了優先度 |
ECI_CONTAINER_EXIT_PRIORITY |
|
コンテナの終了順序を設定すると、Pod の仕様にある TerminationGracePeriodSeconds の値を超え、Pod 全体の終了時間が長くなる可能性があります。
例
コンテナの起動順序の設定
-
次のコマンドを使用して、YAML 設定ファイルから Deployment を作成します。
kubectl apply -f test-launch.yaml次の
test-launch.yamlは、レプリカが 1 つの Deployment を定義するものです。この Pod には c1 と c2 の 2 つのコンテナが含まれており、c1 には c2 より高い起動優先度が設定されています。c1 には readiness probe が設定されており、c1 が準備完了した後にのみ c2 が起動することが保証されます。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 -
Pod のステータスでコンテナの起動時刻を確認します。
kubectl describe pod <pod name>出力の
Containersセクションに、c2 コンテナが c1 コンテナの後に起動していることが表示されます。以下に例を示します。Containers: c1: Container ID: containerd://779ec5b6d9ad1164c929ab0b8bbc4a3a3b24fe1f9654867e842f2dcfe7b24beb Image: registry-vpc.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:2ec3c026183996087f26c6b3 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
コンテナの終了順序の設定
-
次のコマンドを使用して、YAML 設定ファイルから Deployment を作成します。
kubectl apply -f test-exit.yaml次の
test-exit.yamlは、レプリカが 1 つの Deployment を定義するものです。この Pod には 3 つのコンテナが含まれており、終了優先度は c1、c2、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 -
Pod を削除してから、コンテナの
Killingイベントの順序を確認します。説明これらのイベントは一時的なものであり、Pod の削除後は確認できない場合があります。
kubectl describe pod <pod name>出力の
Eventsセクションには、コンテナが終了優先度に従い、c1、c2、c3 の順で停止することが表示されます。以下に例を示します。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 40s kubelet Stopping container c1 Normal Killing 20s kubelet Stopping container c2 Normal Killing 7s kubelet Stopping container c3