[CKA] Init Container

Init Container의 핵심 개념과 구성 방법, 실습 풀이를 정리합니다.

다중 컨테이너 포드에서 각 컨테이너는 POD의 수명 주기 동안 살아있는 프로세스를 실행해야 합니다. 예를 들어, 앞서 이야기했던 웹 애플리케이션과 로깅 에이전트가 있는 다중 컨테이너 포드에서 두 컨테이너는 항상 살아있는 것으로 동작해야합니다.

그 중 하나가 실패하면 POD가 다시 시작됩니다.

하지만 때때로 컨테이너에서 완료될 때까지 실행되는 프로세스를 실행하고 싶을 수 있습니다. 예를 들어, 메인 웹 애플리케이션에서 사용될 리포지토리에서 코드나 바이너리를 가져오는 프로세스입니다. 이는 포드가 처음 생성될 때 한 번만 실행되는 작업입니다. 또는 실제 애플리케이션이 시작되기 전에 외부 서비스나 데이터베이스가 작동할 때까지 기다리는 프로세스입니다. 여기서 initContainers가 등장합니다.

InitContainer

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers: # 컨테이너 명 확인
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'git clone <some-repository-that-will-be-used-by-application> ; done;']

POD가 처음 생성되면 initContainer가 실행되고, 애플리케이션을 호스팅하는 실제 컨테이너가 시작되기 전에 initContainer의 프로세스가 완료될 때까지 실행되어야 합니다.
여러 개의 initContainers를 구성할 수도 있습니다. 우리가 멀티 컨테이너 포드에 대해 한 것처럼요. 그 경우 각 init 컨테이너는 순차적으로 한 번에 하나씩 실행됩니다 .
initContainer 중 하나라도 완료되지 못하면 Kubernetes는 Init Container가 성공할 때까지 Pod를 반복적으로 다시 시작합니다.

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']