CKA (Certified Kubernetes Administrator)/Kode Kloud

04.Application Lifecycle Management - Init Containers

seulseul 2022. 1. 21. 14:37
Application Lifecycle Management

1)  Rolling Updates and Rollbacks
2) Commands and Arguments
3) Env Variables
4) Secrets
5) Multi Container PODs
6) Init Containers

01. Identify the pod that has an initContainer configured.

 

ask : blue

 

Run the command kubectl describe pod blue

It has an InitContainer called init-myservice
controlplane ~ ✖ kubectl describe pod blue
Name:         blue
Namespace:    default
Priority:     0
Node:         controlplane/172.25.0.59
Start Time:   Fri, 21 Jan 2022 04:48:10 +0000
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.42.0.11
IPs:
  IP:  10.42.0.11
Init Containers:
  init-myservice:
    Container ID:  containerd://1c136c413154fa6648ad7f1a8f945f39abb4fbdb3159e16d720e6e6cad5c7ac3
    Image:         busybox
    Image ID:      docker.io/library/busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      sleep 5
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Fri, 21 Jan 2022 04:48:13 +0000
      Finished:     Fri, 21 Jan 2022 04:48:18 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-dbz9f (ro)
Containers:
  green-container-1:
    Container ID:  containerd://1b38bffe0e50d43ca5faa911d38ef621cd9cb3583693509d043bd474d3a063eb
    Image:         busybox:1.28
    Image ID:      docker.io/library/busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      echo The app is running! && sleep 3600
    State:          Running
      Started:      Fri, 21 Jan 2022 04:48:20 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-dbz9f (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-dbz9f:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  17m   default-scheduler  Successfully assigned default/blue to controlplane
  Normal  Pulling    17m   kubelet            Pulling image "busybox"
  Normal  Pulled     17m   kubelet            Successfully pulled image "busybox" in 1.239566066s
  Normal  Created    17m   kubelet            Created container init-myservice
  Normal  Started    17m   kubelet            Started container init-myservice
  Normal  Pulled     17m   kubelet            Container image "busybox:1.28" already present on machine
  Normal  Created    17m   kubelet            Created container green-container-1
  Normal  Started    17m   kubelet            Started container green-container-1

02. What is the image used by the initContainer on the blue pod?

 

ask : busybox

 

03. What is the state of the initContainer on pod blue

ask : Terminated

 

04. Why is the initContainer terminated? What is the reason?

 

ask : The process completed successfully

 

05. We just created a new app named purple. How many initContainers does it have?

 

ask : 2

 

controlplane ~ ➜  kubectl describe pod purple
Name:         purple
Namespace:    default
Priority:     0
Node:         controlplane/172.25.0.59
Start Time:   Fri, 21 Jan 2022 05:09:17 +0000
Labels:       <none>
Annotations:  <none>
Status:       Pending
IP:           10.42.0.12
IPs:
  IP:  10.42.0.12
Init Containers:
  warm-up-1:
    Container ID:  containerd://ce4f9327f3d3f735bd938e2698199d36ce1e1e0b85d309ab5b3f4524fc7a4428
    Image:         busybox:1.28
    Image ID:      docker.io/library/busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
    Port:          <none>
    Host Port:     <none>
    Command:
      sh
      -c
      sleep 600
    State:          Running
      Started:      Fri, 21 Jan 2022 05:09:18 +0000
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-l6wlw (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 kube-api-access-l6wlw (ro)
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 kube-api-access-l6wlw (ro)
Conditions:
  Type              Status
  Initialized       False 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-l6wlw:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  49s   default-scheduler  Successfully assigned default/purple to controlplane
  Normal  Pulled     49s   kubelet            Container image "busybox:1.28" already present on machine
  Normal  Created    49s   kubelet            Created container warm-up-1
  Normal  Started    49s   kubelet            Started container warm-up-1

 

06. What is the state of the POD?

 

ask : Pending

 

 

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

 

ask : 30 minutes

 

# Hint
Check the commands used in the initContainers. 
The first one sleeps for 600 seconds (10 minutes)
and the second one sleeps for 1200 seconds (20 minutes)

# solution
Adding the sleep times for both initContainers, 
the application will start after 1800 seconds or 30 minutes.

 

08. Update the pod red to use an initContainer that uses the busybox image and sleeps for 20 seconds

Delete and re-create the pod if necessary. But make sure no other configurations change.

  • Pod: red
  • initContainer Configured Correctly
--
apiVersion: v1
kind: Pod
metadata:
  name: red
  namespace: default
spec:
  containers:
  - command:
    - sh
    - -c
    - echo The app is running! && sleep 3600
    image: busybox:1.28
    name: red-container
  initContainers:
  - image: busybox
    name: red-initcontainer
    command: 
      - "sleep"
      - "20"

 

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

Once fixed, wait for the application to run before checking solution.

  • Issue fixed
# hint
Check the command used by the initContainer and correct it.
There is a typo in the command used by the initContainer. 
To fix this, first get the pod definition file by running 

kubectl get pod orange -o yaml > /root/orange.yaml

Next, edit the command and fix the typo.
Then, delete the old pod by running kubectl delete pod orange
Finally, create the pod again by running 

kubectl create -f /root/orange.yaml

 

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2022-01-21T05:26:49Z"
  name: orange
  namespace: default
  resourceVersion: "1377"
  uid: 47942a76-0c3f-4d88-a83c-4b8ca535f2c1
spec:
  containers:
  - command:
    - sh
    - -c
    - echo The app is running! && sleep 3600
    image: busybox:1.28
    imagePullPolicy: IfNotPresent
    name: orange-container
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-crcfv
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  initContainers:
  - command:
    - sh
    - -c
    - sleeeep 2;
    image: busybox
    imagePullPolicy: Always
    name: init-myservice
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-crcfv
      readOnly: true
  nodeName: controlplane
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: kube-api-access-crcfv
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
                           fieldPath: metadata.namespace
            path: namespace
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2022-01-21T05:26:49Z"
    message: 'containers with incomplete status: [init-myservice]'
    reason: ContainersNotInitialized
    status: "False"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2022-01-21T05:26:49Z"
    message: 'containers with unready status: [orange-container]'
    reason: ContainersNotReady
    status: "False"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2022-01-21T05:26:49Z"
    message: 'containers with unready status: [orange-container]'
    reason: ContainersNotReady
    status: "False"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2022-01-21T05:26:49Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - image: busybox:1.28
    imageID: ""
    lastState: {}
    name: orange-container
    ready: false
    restartCount: 0
    started: false
    state:
      waiting:
        reason: PodInitializing
  hostIP: 172.25.0.59
  initContainerStatuses:
  - containerID: containerd://e672f82d4c69bbce884a493b782745775fa542b72e3e29e2c809d5e35922ce8b
    image: docker.io/library/busybox:latest
    imageID: docker.io/library/busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    lastState:
      terminated:
        containerID: containerd://e672f82d4c69bbce884a493b782745775fa542b72e3e29e2c809d5e35922ce8b
        exitCode: 127
        finishedAt: "2022-01-21T05:28:19Z"
        reason: Error
        startedAt: "2022-01-21T05:28:19Z"
    name: init-myservice
    ready: false
    restartCount: 4
    state:
      waiting:
        message: back-off 1m20s restarting failed container=init-myservice pod=orange_default(47942a76-0c3f-4d88-a83c-4b8ca535f2c1)
        reason: CrashLoopBackOff
  phase: Pending
  podIP: 10.42.0.14
  podIPs:
  - ip: 10.42.0.14
  qosClass: BestEffort
  startTime: "2022-01-21T05:26:49Z"
~

Bookmark

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

 

초기화 컨테이너

이 페이지는 초기화 컨테이너에 대한 개요를 제공한다. 초기화 컨테이너는 파드의 앱 컨테이너들이 실행되기 전에 실행되는 특수한 컨테이너이며, 앱 이미지에는 없는 유틸리티 또는 설정 스크

kubernetes.io