본문 바로가기
Compute/kubernetis

[따배씨] 5. Side-car Container Pod 생성 / CKA 시험 문제 학습

by 조청유곽 2025. 1. 30.
반응형

이 포스팅은 아래의 유튜브 채널 "따배"를 따라서 학습한 내용입니다. 

 

 

[관련 이론]


 

 

[Precondition]

(1) 테스트 환경

(1.1) Rocky Linux Cluster 

: 직접 구성

[root@k8s-master ~]# k get nodes -o wide
NAME         STATUS   ROLES           AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                            KERNEL-VERSION                  CONTAINER-RUNTIME
k8s-master   Ready    control-plane   30d   v1.27.2   192.168.56.30   <none>        Rocky Linux 8.10 (Green Obsidian)   4.18.0-553.33.1.el8_10.x86_64   containerd://1.6.32
k8s-node1    Ready    <none>          30d   v1.27.2   192.168.56.31   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
k8s-node2    Ready    <none>          30d   v1.27.2   192.168.56.32   <none>        Rocky Linux 8.8 (Green Obsidian)    4.18.0-477.10.1.el8_8.x86_64    containerd://1.6.21
[root@k8s-master ~]#

 

(1.2) Ubuntu Cluster 

: kodekloud 테스트 환경 활용

controlplane ~ ➜  kubectl get nodes -o wide
NAME           STATUS   ROLES           AGE     VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION   CONTAINER-RUNTIME
controlplane   Ready    control-plane   9m6s    v1.31.0   192.6.94.6    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26
node01         Ready    <none>          8m31s   v1.31.0   192.6.94.9    <none>        Ubuntu 22.04.4 LTS   5.4.0-1106-gcp   containerd://1.6.26

https://learn.kodekloud.com/user/courses/udemy-labs-certified-kubernetes-administrator-with-practice-tests

 

(2) 사전 필요 설정 

   : 테스트에 사용할 existing pod "eshop-cart-pod"를 미리 생성해 둔다. 

controlplane ~ ➜  vi 5-eshop-cart.yaml 

controlplane ~ ➜  k apply -f 5-eshop-cart.yaml 
pod/eshop-cart-app created

controlplane ~ ➜  cat 5-eshop-cart.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: eshop-cart-app
spec:
  containers:
  - image: busybox
    name: cart-app
    command:
    - /bin/sh
    - -c    
    - 'i=1;while :;do  echo -e "$i: Price: $((RANDOM % 10000 + 1))" >> /var/log/cart-app.log;
      i=$((i+1)); sleep 2; done'
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - emptyDir: {}
    name: varlog

controlplane ~ ➜

 

 

[Question]

An existing Pod needs to be integrated into the Kubernetes built-in logging architecture 
(e.g. kubectl logs).
Adding a streaming sidecar container is a good and common way to accomplish this requirement.

- Add a sidecar container named sidecar, using busybox image, to existing Pod eshop-cart-app
- The new sidecar container has to run the following command: /bin/sh, -c, "tail -n+1 -F /var/log/cart-app.log
- Use a volume, mounted at /var/log, to make the log file cart-app.log available to the sidecar container.
- Don't modify the cart-app

 

 

[Solve]

(1) 존재하는 "eshop-cart-app" pod를 yaml 파일로 저장

controlplane ~ ➜  k get pods 
NAME             READY   STATUS    RESTARTS   AGE
eshop-cart-app   1/1     Running   0          2m22s

controlplane ~ ➜  k get pods eshop-cart-app -o yaml > eshop.yaml

 

 

(2) 저장한 eshop.yaml에서 sidecar 컨테이너를 추가  

controlplane ~ ➜  vi eshop.yaml

 

 

(3) pod 삭제/재배포 

 

controlplane ~ ➜ k delete pods eshop-cart-app 
pod "eshop-cart-app" deleted

controlplane ~ ➜  k apply -f eshop.yaml 
pod/eshop-cart-app created

 

 

(4) 생성된 pod 확인

: describe 명령으로 cart-app, sidecar 컨테이너가 생성됨을 확인

: log 명령으로 sidecar 컨테이너의 로그 출력이 정상적으로 진행됨을 확인 


 

 

[사용 커맨드 정리]

k get pods eshop-cart-app -o yaml > eshop.yaml

vi eshop.yaml

- name: sidecar
  image: busybox
  command: ['sh', '-c', 'tail -n+1 -F /var/log/cart-app.log']
  volumeMounts:
  - name: varlog
    mountPath: /var/log

 

k delete pods eshop-cart-app 
k apply -f eshop.yaml 

 

k get pods 

k describe pods eshop-cart-app

k logs pods/eshop-cart-app -c sidecar 

 

반응형