배경

최근 서비스 알림 Slack Bot을 유지 보수하는 과정에서 Batch Job 수행이 필요하게 되어 Kubernetes Cronjob으로 Batch Job을 생성했습니다. 처음 스케줄링 된 파드가 생성되어 지정한 Batch Job이 끝났음에도 Cronjob으로 생성한 Pod가 종료되지 않는 문제가 발생했습니다.

문제가 발생한 Pod를 분석해본 결과 메인 프로세스로 생성된 서비스 컨테이너는 종료되었지만 사이드카로 주입된istio-proxy 컨테이너가 종료되지 않아 발생한 문제였습니다.

관련 이슈 및 내용

https://github.com/istio/istio/issues/11659

https://github.com/kubernetes/enhancements/issues/3759

https://github.com/kubernetes/enhancements/issues/753

https://github.com/kubernetes/enhancements/blob/master/keps/sig-node/753-sidecar-containers/README.md

해결 방법

해결 방법을 Istio 커뮤니티에서 찾아봤을 때 proxy agent에 /quitquitquit 핸들러를 추가했다는 내용을 확인했습니다. 해당 핸들러를 통해 사이드카 컨테이너를 종료시킬 수 있다는 내용을 확인했습니다.

https://github.com/istio/istio/issues/6324

예시

apiVersion: batch/v1
kind: CronJob
metadata:
  name: slack-alert
  namespace: default
spec:
  timeZone: 'Asia/Seoul'
  schedule: "5 * * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 2
  jobTemplate:
    spec:
      completions: 1
      parallelism: 1
      backoffLimit: 3
      template:
        metadata:
            annotations:
              sidecar.istio.io/inject: "true"
              proxy.istio.io/config: '{"holdApplicationUntilProxyStarts": true}'
        spec:
          containers:
          - name: slack-alert
            image: sample/image:latest
            command:
            - /bin/sh
            - -c
            - |
              until curl -fsI <http://localhost:15021/healthz/ready>; do echo \\"Waiting for Sidecar...\\"; sleep 3; done;
              echo \\"Sidecar available. Running the command...\\";
              <YOUR_COMMAND>;😂
              x=$(echo $?); curl -fsI -X POST <http://localhost:15020/quitquitquit> && exit $x
          restartPolicy: OnFailure
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: ncloud.com/nks-nodepool
                    operator: In
                    values:
                    - devops