Net Eng

[CKA] - Side-car Container Pod 생성하기 본문

Cloud/Kubernetes

[CKA] - Side-car Container Pod 생성하기

欲心 2024. 1. 19. 16:01

Multi-Container Pod 패턴

- adapter

- support

- side-car

 

side-car 컨테이너란?

- 부가 기능을 제공하는 컨테이너

 

주요 애플리케이션이나 메인 컨테이너는 기본 기능을 담당하고, sidecar 컨테이너는 여러 가지 작업을 처리하며 주 컨테이너에 대한 보조적인 역할을 수행한다.

 

예를 들어, 마이크로서비스 아키텍처에서는 서비스 디스커버리, 로깅, 모니터링 등의 기능을 sidecar 컨테이너로 분리하여 구현할 수 있다. 이는 각각의 컨테이너가 단일 책임 원칙을 따르며 서로 간섭 없이 독립적으로 개발 및 배포될 수 있도록 해준다.

 


 

[선수 작업]

kubectl apply -f - <<EOF
kind: Pod
apiVersion: v1
metadata:
  name: eshop-cart-app
  namespace: default
spec:
  containers:
  - name: cart-app
    image: busybox
    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:
  - name: varlog
    emptyDir: {}
EOF

 


 

[문제]

Q. An existing Pod needs to be integrated into the kubernetes built-in logging architecture(ex: kubectl logs).

 

Adding a streaming sidecar container is a good and common way to accomplish this requirement.

 

TASKs:

Add a sidecar container named sidecar, using the busybox image, to the 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.

 


 

[풀이]

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

* 동작중인 eshop 파드 yaml 파일 생성

 

vi eshop-cart-app.yaml

* yaml 파일 수정

 

Logging Architecture

Application logs can help you understand what is happening inside your application. The logs are particularly useful for debugging problems and monitoring cluster activity. Most modern applications have some kind of logging mechanism. Likewise, container e

kubernetes.io

* 공식 문서 참고하여 컨테이너 정보 입력

 

apiVersion: v1
kind: Pod
metadata:
 name: eshop-cart-app
spec:
  containers:
  - command:
    - /bin/sh
    - -c
    - "i=1;  while :;  do \n  echo -e \": Price: 6341\" >> /var/log/cart-app.log;
      \n  i=1; \n  sleep 2; \ndone\n"
    image: busybox
    name: cart-app
    volumeMounts:
    - mountPath: /var/log
      name: varlog
  - name: sidecar
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -F /var/log/cart-app.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - emptyDir: {}
    name: varlog

 

kubectl delete pods eshop-cart-app --force

* 기존 Pod 삭제

 

kubectl apply -f eshop-cart-app.yaml

* yaml 파일 적용

 

kubectl get pods eshop-cart-app

* Pod 생성 확인

 

kubectl logs eshop-cart-app -c sidecar

* Log 확인

 

Pod와 Log 확인

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

[CKA] - Rolling update & Roll Back  (0) 2024.01.19
[CKA] - Deployment & Pod Scale  (0) 2024.01.19
[CKA] - Multi-Container Pod 생성하기  (0) 2024.01.19
[CKA] - Static Pod 생성하기  (0) 2024.01.18
[CKA] - Pod 생성하기  (0) 2024.01.18