CKA 30제 05: Side-car container Pod 실행

주영·2023년 10월 29일
0

CKA 30제

목록 보기
5/7
post-thumbnail

본 게시물은 [따배씨] 05. Side-car Container Pod 실행하기 영상을 참고하여 작성한 글입니다.

kubernetes 공식 문서 : [Kubernetes docs] sidecar container with logging agent

이론

1. Main Container (기록)

  • Client 사용자가 접속 시 /var/log/html 위치에 access, error 로그가 남겨짐
  • 기본(volume 마운트 X)은 컨테이너 안에만 로그가 남겨짐

2. Volume

  • 별도의 kubernetes 볼륨을 만들어서 데이터 저장
  • Main Container에 해당 볼륨을 마운트하면 /var/log/html에 쌓이는 access, error 로그가 실제로는 여기에 쌓임

3. Sidecar Container (가공)

  • Main Container의 데이터를 가공하고 싶을 경우 사용
  • Sidecar Container의 /data 디렉터리에 있는 데이터를 가지고 로그 분석을 하는 웹 로그 분석 앱 컨테이너일 경우, /data 디렉터리에 웹 로그가 있어야 함
  • Main Container의 로그가 저장되는 Volume을 /data로 마운트

⇒ 하나의 Pod 안에서 2개의 컨테이너(Main, Sidecar)가 같이 만들어져서 동작

문제

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

풀이

1. Pod 조회

$ kubectl get pod eshop-cart-app
NAME             READY   STATUS    RESTARTS      AGE
eshop-cart-app   1/1     Running   4 (11d ago)   14d

2. 파드 yaml 템플릿 얻기

$ kubectl get pod eshop-cart-app -o yaml > eshop.yaml

3. yaml 확인

⇒ 'cart-app'이라는 메인 컨테이너가 'varlog'라는 볼륨 공간을 /var/log에서 'varlog'라는 이름으로 사용중

$ vi eshop.yaml

apiVersion: v1
kind: Pod
metadata:
  name: eshop-cart-app
spec:
  containers:
  - 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'
    image: busybox
    name: cart-app
    volumeMounts:
    - mountPath: /var/log
      name: varlog
  volumes:
  - emptyDir: {}
    name: varlog

4. 공식 문서 참고 - sidecar 컨테이너 부분

[Kubernetes docs] sidecar container with logging agent > admin/logging/two-files-counter-pod-streaming-sidecar.yaml

  - name: count-log-1
    image: busybox:1.28
    args: [/bin/sh, -c, 'tail -n+1 -F /var/log/1.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log

5. yaml 수정

$ vi eshop.yaml

apiVersion: v1
kind: Pod
metadata:
  name: eshop-cart-app
spec:
  containers:
  - 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'
    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

6. 기존 pod 삭제

$ kubectl delete pod eshop-cart-app --force

7. yaml 적용

$ kubectl apply -f eshop.yaml
pod/eshop-cart-app created

8. pod 실행 및 컨테이너 개수(2개) 확인

$ kubectl get pod eshop-cart-app
NAME             READY   STATUS    RESTARTS   AGE
eshop-cart-app   2/2     Running   0          34s

9. 모니터

  • 로그 정보가 출력되는지 확인
$ kubectl logs eshop-cart-app -c sidecar
...
27: Price: 1547
28: Price: 380

0개의 댓글