본문 바로가기

CKA &. CKAD/Application Lifecycle Management

InitContainer and Practice Test

Multi-container Pod에서 각 Container는 POD의 Lifecycle동안 활성 상태로 유지되는 프로세스를 실행해야 한다.

예를 들어 이전에 Web Application과 logging Agent가 있는 Mutli Container Pod에서 두 컨테이너 모두 항상 활성 상태를 유지해야 한다.

Log Agent Container에서 실행되는 프로세스는 Web Application이 실행되는 동안 활성 상태로 유지 된다. 이들 중 하나라도 실패하면 POD가 다시 시작된다.

 

그러나 때대로 Container에서 완료될 때까지 실행되는 프로세스를 실행할 수 있다. 예를 들어, 기본 웹 애플리케이션에서 사용할 저장소에서 코드 또는 바이너리를 가져 오는 프로세스이다. 이는 Pod가 처음 생성될 때 한번만 실행되는 작업이다. 

또는 실제 애플리케이션이 시작되기 전에 외부 서비스나 데이터베이스가 작동 할 때까지 기다리는 프로세스이다. 그것이 initContainers가 들어오는 곳이다.

 

initContainer는 다음과 같이 initContainers 섹션 내에 지정된다는 점을 제외하고는 다른 모든 컨테이너와 마찬가지로 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]
  initContainers:
  - name: init-myservice
    image: busybox
    command: ['sh', '-c', 'git clone <some-repository-that-..]

Pod가 처음 생성될 때 initContainer가 실행되고 initContainer의 프로세스는 애플리케이션을 호스팅하는 실제 컨테이너가 시작되기 전에 완료 될 때까지 실행되어야 한다.

 

다중 Pod Container에 대해 수행 한 것처럼 여러 initContainer를 구성할 수 도 있다. 이 경우 각 initContainer는 순차적으로 한번에 하나씩 실행된다.

 

initContainer 중 하나라도 완료되지 않으면 Kubernetes는 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 runnig && sleep']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'until nslookup myservice; do echo']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'unitl nslookup mydb; do echo wiatting']

 

상세한 내용은 아래 link에서 확인 한다.

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

 

Init Containers

This page provides an overview of init containers: specialized containers that run before app containers in a Pod. Init containers can contain utilities or setup scripts not present in an app image. You can specify init containers in the Pod specification

kubernetes.io

[Practice Test]

ㅁ Identify the pod that has an initContainer configured

kubectl get pod

kubectl describe pod [pod name]

ㅁ Why is the initContainer terminated? what is the reason?

- The process crashed

- The process connot start

- The process completed successfully

 

ㅁ We just created a. ew app named purple. How many initContainers does it have?

kubectl get pod purple

NAME     READY   STATUS     RESTARTS   AGE
purple   0/1     Init:0/2   0          72s
kubectl describe pod purple

ㅁ What is the state of the POD?

kubectl describe pod purple

Name:         purple
Namespace:    default
Priority:     0
Node:         node01/172.17.0.29
Start Time:   Sat, 27 Mar 2021 14:40:29 +0000
Labels:       <none>
Annotations:  <none>
Status:       Pending
IP:           10.244.1.8

       ~~
Containers:
  purple-container:
    Container ID:  
    Image:         busybox:1.28
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      echo The app is running! && sleep 3600
    State:          Waiting
      Reason:       PodInitializing
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-brfzb (ro)

       ~~
Conditions:
  Type              Status
  Initialized       False 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 

- Running

- Waiting

- Terminated

 

ㅁ How long after the creation of the POD will the application come up and be available to users?

Init Containers:
  warm-up-1:
    Container ID:  docker://0b6f7db3f3b3c2294605783385e1e9160c226f70947d1707be66c40c4dc07504
    Image:         busybox:1.28
    Image ID:      docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      sleep 600
    State:          Running
      Started:      Sat, 27 Mar 2021 14:40:31 +0000
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-brfzb (ro)
  warm-up-2:
    Container ID:  
    Image:         busybox:1.28
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      sleep 1200
    State:          Waiting
      Reason:       PodInitializing
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-brfzb (ro)

- 20 Minutes

- 10 Minutes

- 600 Seconds

- 30 Minutes

 

ㅁ Update the pod red to use an initContainer that uses the busybox image and sleep for 20 seconds

- Pod: red

- initcontainer Configure Correctly

 

kubectl get pod red -o yaml > red.yaml

red.yaml에 아래 내용 추가

spec:
  initContainers:
  - command:["sleep", "20"]
    image: busybox
    name: myinit
controlplane $ kubectl create -f red.yaml 
pod/red created

controlplane $ kubectl get pod red
NAME   READY   STATUS     RESTARTS   AGE
red    0/1     Init:0/1   0          6s

controlplane $ kubectl get pod red
NAME   READY   STATUS     RESTARTS   AGE
red    0/1     Init:0/1   0          12s

controlplane $ kubectl get pod red
NAME   READY   STATUS     RESTARTS   AGE
red    0/1     Init:0/1   0          15s

controlplane $ kubectl get pod red
NAME   READY   STATUS     RESTARTS   AGE
red    0/1     Init:0/1   0          20s

controlplane $ kubectl get pod red
NAME   READY   STATUS     RESTARTS   AGE
red    0/1     Init:0/1   0          22s

controlplane $ kubectl get pod red
NAME   READY   STATUS            RESTARTS   AGE
red    0/1     PodInitializing   0          23s

controlplane $ kubectl get pod red
NAME   READY   STATUS    RESTARTS   AGE
red    1/1     Running   0          26s

 

ㅁ A new application orange is deployed. There is something wrong with it. Identify and fix the issue.

kubectl get pod -o yaml > oralge.yaml
vi orange.yaml

orange Pod의 initContainer 부분 수정

'CKA &. CKAD > Application Lifecycle Management' 카테고리의 다른 글

Practice Test - Multi-Container Pod  (0) 2021.03.27
Secret Practice Test  (0) 2021.03.27
Secret  (0) 2021.03.27
ConfigMap Practice Test  (0) 2021.03.27
Configure Environment Variable in Applications  (0) 2021.03.27