728x90
<Kubernetes :: 사이드카 컨테이너 sidecar container 제작 실습 1>
- 동일한 Pod 내에 다른 컨테이너와 함께 실행되는 보조 컨테이너
- 주 컨테이너가 실행되는 동안 Sidecar 컨테이너는 주 컨테이너를 지원하거나 보조 기능을 제공
1. 실습에 필요한 pod 제작
<sidecar-pod.yaml>
- app-container: nginx:1.25.3 이미지 사용
- sidecar-container: Debian 이미지를 사용하며, 로그를 기록하는 역할
- 셸 스크립트를 사용하여 1초마다 현재 시간을 /date-log/index.html 파일에 기록
- html-log라는 이름의 빈 디렉토리 볼륨이 정의되어 두 컨테이너 간에 공유
- 웹 서버 컨테이너와 로깅 사이드카 컨테이너가 동일한 볼륨을 통해 데이터 공유
apiVersion: v1
kind: Pod
metadata:
name: log-pod
spec:
containers:
- name: app-container
image: nginx:1.25.3
volumeMounts:
- name: html-log
mountPath: /usr/share/nginx/html
- name: sidecar-container
image: debian:10
volumeMounts:
- name: html-log
mountPath: /date-log
command: ["/bin/sh", "-c"]
args:
- while true;
do date >> /date-log/index.html;
sleep 1;
done
volumes:
- name: html-log
emptyDir: {}
2. 포드 실행
PS C:\Users\admin\Desktop\k8s-pods> kubectl apply -f sidecar-pod.yaml
- app-container 컨테이너에 있는 /usr/share/nginx/html/index.html 파일의 내용을 실시간으로 볼 수 있도록 하는 명령
PS C:\Users\admin\Desktop\k8s-pods> kubectl exec log-pod -c app-container -- tail -f /usr/share/nginx/html/index.html
<출력 결과>
<Kubernetes :: 사이드카 컨테이너 sidecar container 제작 실습 2>
1. yaml 파일 제작
<blog-web.yaml>
- blog-web-container: Nginx 웹 서버로 /var/log/nginx 경로에 마운트된 shared-logs 볼륨을 사용합니다. 이 볼륨은 로그 파일을 저장
- sidecar-container: Debian 이미지를 사용해 로그 파일을 읽고 실시간으로 표시 /var/log/nginx/access.log 및 /var/log/nginx/error.log 파일의 내용을 5초마다 출력
- blog-web-container에서 생성된 로그 파일을 sidecar-container에서 실시간으로 모니터링
apiVersion: v1
kind: Pod
metadata:
name: blog-web
labels:
name: blog
spec:
volumes:
- name: shared-logs
emptyDir: {}
containers:
- name: blog-web-container
image: nginx:1.25.3-alpine
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
- name: sidecar-container
image: debian:10
command:
[
"/bin/bash",
"-c",
"while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 5; done",
]
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
2. pod 실행
PS C:\Users\admin\Desktop\k8s-pods> kubectl apply -f blog-web.yaml
pod/blog-web created
3. 로그 기록 실습
- Nginx 웹 서버의 액세스 로그 파일인 access.log의 내용을 실시간으로 확인
PS C:\Users\admin\Desktop\k8s-pods> kubectl exec blog-web -c blog-web-container -- tail -f /var/log/nginx/access.log
'클라우드 + DevOps > Kubernetes (k8s)' 카테고리의 다른 글
Kubernetes :: 쿠버네티스 클러스터 모니터링 서비스 KubeShark (0) | 2024.04.24 |
---|---|
Kubernetes :: 라벨 및 네임스페이스 제작 실습 (0) | 2024.04.23 |
Kubernetes 쿠버네티스 :: StatefulSet 활용 (0) | 2024.04.22 |
Kubernetes :: 이니셜 컨테이너 initial container 실습 (0) | 2024.04.21 |
Kubernetes 쿠버네티스 실습 :: 생성한 Deployment 수정 (0) | 2024.04.21 |