문제3: init Container

tothelight·2024년 5월 21일

CKAD 예상 문제

목록 보기
3/14

/data/ckad/fc-app.yml을 편집해서 다음의 조건에 맞는 init 컨테이너를 추가하십시오.

  • fc-app.yml을 통해 main 컨테이너 애플리케이션이 동작 가능하다.
  • init 컨테이너로 busybox:1.28 컨테이너를 추가하고 /workdir/fcdata.txt라는 empty 파일을 생성한다.
  • 만약 init 컨테이너가 fcdata.txt 파일을 생성하지 못하면 main 컨테이너는 실행할 수 없다.
  • init 컨테이너와 main 컨테이너는 볼륨 마운트를 통해 /workdir 디렉토리를 공유한다.

명령어 흐름

# 현재 fc-app.yml 파일을 확인합니다.
$ cat /data/ckad/fc-app.yml

# fc-app.yml 파일을 엽니다.
$ vi /data/ckad/fc-app.yml

# 수정한 yaml 파일을 적용합니다.
$ kubectl apply -f /data/ckad/fc-app.yml

fc-app.yml 수정 내용

apiVersion: v1
kind: Pod
metadata:
  name: fc-app
spec:
  containers:
  - name: main
    image: busybox:1.28
    command: ['sh', '-c', 'if [ -f /workdir/fcdata.txt ]; then exit 1; else sleep 300; fi']
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  initContainers:
  - name: init
    image: busybox:1.28
    command: ['sh', '-c', 'touch /workdir/fcdata.txt']
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  volumes:
  - name: workdir
    emptyDir: {}

솔루션 설명

  1. 현재 fc-app.yml 파일 확인

    $ cat /data/ckad/fc-app.yml
    • cat /data/ckad/fc-app.yml 명령어로 현재 fc-app.yml 파일의 내용을 확인합니다.
  2. fc-app.yml 파일 수정

    $ vi /data/ckad/fc-app.yml
    • vi /data/ckad/fc-app.yml 명령어로 fc-app.yml 파일을 엽니다.
    • 다음과 같이 파일 내용을 수정합니다.
    apiVersion: v1
    kind: Pod
    metadata:
      name: fc-app
    spec:
      containers:
      - name: main
        image: busybox:1.28
        command: ['sh', '-c', 'if [ -f /workdir/fcdata.txt ]; then exit 1; else sleep 300; fi']
        volumeMounts:
        - name: workdir
          mountPath: "/workdir"
      initContainers:
      - name: init
        image: busybox:1.28
        command: ['sh', '-c', 'touch /workdir/fcdata.txt']
        volumeMounts:
        - name: workdir
          mountPath: "/workdir"
      volumes:
      - name: workdir
        emptyDir: {}
    • initContainers 섹션을 추가하여 init 컨테이너를 정의합니다.
    • init 컨테이너는 busybox:1.28 이미지를 사용하고 /workdir/fcdata.txt 파일을 생성하는 명령어를 실행합니다.
    • volumeMounts를 통해 /workdir 디렉토리를 공유합니다.
    • main 컨테이너는 init 컨테이너가 생성한 fcdata.txt 파일이 있는 경우 종료하고, 그렇지 않으면 300초 동안 대기하도록 설정합니다.
  3. 수정한 yaml 파일 적용

    $ kubectl apply -f /data/ckad/fc-app.yml
    • kubectl apply -f /data/ckad/fc-app.yml 명령어로 수정한 yaml 파일의 내용을 적용합니다.

0개의 댓글