Side-car Container Pod란?
하나의 Pod에 다음 2가지의 컨테이너가 동작할 때
1) nginx 컨테이너 → 별도의 Kubenetes volume에 rw로 마운트하여 error.log와 access.log 를 저장하도록 설정→ Main container
2) 로그 분석 컨테이너 → 별도의 Kubernetes volume에 ro로 마운트하여 error.log와 access.log를 분석 → Side-car container
[실습 환경 구성]
cat exam.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
kubectl apply -f exam.yaml
[문제]
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
# eshop-cart-app Pod를 yaml 형식으로 생성
kubectl get pods eshop-cart-app -o yaml > eshop.yaml
# 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
kubernetes 공식 사이트에서 logging을 검색하여 Side car 부분을 복사하여 eshop.yaml을 적용한다.
https://kubernetes.io/docs/concepts/cluster-administration/logging/
공식 사이트에 있는 내용을 문제에 맞게 수정 후 eshop.yaml 파일에 적용
- name: sidecar
image: busybox
args: [/bin/sh, -c, 'tail -n+1 -F /var/log/cart-app.log']
volumeMounts:
- name: varlog
mountPath: /var/log
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
# 기존에 동작중인 pod 제거
kubectl delete pods eshop-cart-app --force
# 새로 작성한 yaml 파일로 pod 생성
kubectl apply -f eshop.yaml
# 정상 동작 확인
kubectl get pods
# 메인 pod에서 생성된 로그를 sidecar에서 확인
kubectl logs eshop-cart-app -c sidecar
[참고]
- 유투브 따배씨
'자격증 > CKA' 카테고리의 다른 글
[CKA] Rolling Update & Roll Back (0) | 2023.03.15 |
---|---|
[CKA] Deployment & Pod Scale (0) | 2023.03.14 |
[CKA] multi-container Pod 생성 (0) | 2023.03.05 |
[CKA] Static Pod 생성 (0) | 2023.03.05 |
[CKA] Pod 생성하기 (0) | 2023.03.04 |