Net Eng

[CKA] - Init Container를 포함한 Pod 운영 본문

Cloud/Kubernetes

[CKA] - Init Container를 포함한 Pod 운영

欲心 2024. 1. 22. 10:11

[선수 지식]

Init Container

 

초기화 컨테이너

이 페이지는 초기화 컨테이너에 대한 개요를 제공한다. 초기화 컨테이너는 파드의 앱 컨테이너들이 실행되기 전에 실행되는 특수한 컨테이너이다. 초기화 컨테이너는 앱 이미지에는 없는 유틸

kubernetes.io

 


 

[선수 작업]

kubectl config set-context conk8s --user=kubernetes-admin --cluster=kubernetes
mkdir -p /data/cka /workdir
cat << EOF > /data/cka/webpod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: web-pod
spec:
  containers:
  - image: busybox:1.28
    name: main
    command: ['sh', '-c', 'if [ ! -f /workdir/data.txt ]; then  exit 1; else sleep 30; fi']
    volumeMounts:
    - name: workdir
      mountPath: "/workdir"
  volumes:
  - name: workdir
    emptyDir: {}
EOF

 


 

[문제]

Perform the following

Tasks:

Add an init container to web-pod(which has been defined in spec file /data/cka/webpod.yaml).

The init container should create an empyt file named /workdir/data.txt.

If /workdir/data.txt is not detected, the Pod should exit.

Once the spec file has been updated with the init container definition, the Pod should be created.

 

작업 클러스터: confk8s

 


 

[풀이]

kubectl config use-context confk8s

* confk8s 클러스터 사용

 

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

* yaml 파일에서 initContainer 항목 추가

 

kubectl apply -f /data/cka/webpod.yaml

* yaml 파일 실행

 

kubectl get pods

* 실행 확인

 

kubectl exec web-pod -c main -- ls -l /workdir/data.txt

* 컨테이너에 접근하여 확인

 

Pod 확인 및 data.txt 생성 확인

'Cloud > Kubernetes' 카테고리의 다른 글

[CKA] - ConfigMap 운영  (1) 2024.01.22
[CKA] - NodePort 서비스 생성  (1) 2024.01.22
[CKA] - CPU 사용량이 높은 파드 검색  (1) 2024.01.21
[CKA] - Pod Log 추출  (0) 2024.01.21
[CKA] - Deployment & Expose the Service  (0) 2024.01.21