본 게시물은 [따배씨] 05. Side-car Container Pod 실행하기 영상을 참고하여 작성한 글입니다.
kubernetes 공식 문서 : [Kubernetes docs] sidecar container with logging agent
1. Main Container (기록)
2. Volume
3. Sidecar Container (가공)
⇒ 하나의 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
$ kubectl get pod eshop-cart-app
NAME READY STATUS RESTARTS AGE
eshop-cart-app 1/1 Running 4 (11d ago) 14d
$ kubectl get pod eshop-cart-app -o yaml > eshop.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
[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
$ 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
$ kubectl delete pod eshop-cart-app --force
$ kubectl apply -f eshop.yaml
pod/eshop-cart-app created
$ kubectl get pod eshop-cart-app
NAME READY STATUS RESTARTS AGE
eshop-cart-app 2/2 Running 0 34s
$ kubectl logs eshop-cart-app -c sidecar
...
27: Price: 1547
28: Price: 380