본문 바로가기
컨테이너/쿠버네티스

사이드카 패턴 예

by ^..^v 2022. 1. 28.
728x90
반응형

하나의 파드에 웹 서버 컨테이너와 깃헙에서 최신 컨텐츠를 다운받는 컨테이너를 조합

 

깃헙에서 정기적으로 컨텐츠를 다운받는 쉘을 작성

c:\kubernetes\sidecar\contents-cloner

#!/bin/bash
# 최신 Web 데이터를 GitHub로부터 취득 

# 환경변수가 설정되어 있지 않으면 에러 종료
if [ -z $CONTENTS_SOURCE_URL ]; then
   exit 1
fi

# 처음에는 GitHub에서 클론 
git clone $CONTENTS_SOURCE_URL /data

# 이후에는 1준 간격으로 git pull을 수행
cd /data
while true
do
   date
   sleep 60
   git pull
done

 

"standard_init_linux.go:219: exec user process caused: no such file or directory" 오류 발생 시 VSCode 하단에 개행문자 적용 방식을 LF로 변경

 

Dockerfile 작성

c:\kubernetes\sidecar\Dockerfile

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y git
COPY ./contents-cloner /contents-cloner
RUN chmod a+x /contents-cloner
WORKDIR /
CMD ["/contents-cloner"]

 

이미지 빌드 및 레지스트리 등록

C:\kubernetes\sidecar> docker build --tag myanjini/contents-cloner:1.0 .
[+] Building 181.8s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                    0.1s
 => => transferring dockerfile: 240B                                                                    0.0s
 => [internal] load .dockerignore                                                                       0.0s
 => => transferring context: 2B                                                                         0.0s
 => [internal] load metadata for docker.io/library/ubuntu:16.04                                         4.6s
 => [auth] library/ubuntu:pull token for registry-1.docker.io                                           0.0s
 => [internal] load build context                                                                       0.0s
 => => transferring context: 406B                                                                       0.0s
 => [1/5] FROM docker.io/library/ubuntu:16.04@sha256:0f71fa8d4d2d4292c3c617fda2b36f6dabe5c8b6e34c3dc    8.8s
				:
 => [2/5] RUN apt-get update && apt-get install -y git                                                165.9s
 => [3/5] COPY ./contents-cloner /contents-cloner                                                       0.1s
 => [4/5] RUN chmod a+x /contents-cloner                                                                0.3s
 => exporting to image                                                                                  1.9s
 => => exporting layers                                                                                 1.9s
 => => writing image sha256:604bdf429ea90710c6956d3d273b8d49002b9d71d57cfb7047666b06dc41cdf1            0.0s
 => => naming to docker.io/myanjini/contents-cloner:1.0                                                 0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them


C:\kubernetes\sidecar> docker image push myanjini/contents-cloner:1.0
The push refers to repository [docker.io/myanjini/contents-cloner]
11a9cfa58cc8: Pushed
00cd0ded56ed: Pushed
1251204ef8fc: Mounted from library/ubuntu
47ef83afae74: Mounted from library/ubuntu
df54c846128d: Mounted from library/ubuntu
be96a3f634de: Mounted from library/ubuntu
1.0: digest: sha256:dd7a6cb4751d536658d0445f363d213daa63ffc56badd32fa19e43c3e9fe2ce1 size: 1776

 

깃헙 레포지터리 생성

web-contents 이름의 레포지터리 생성

 

index.html 파일 생성

 

사이드카 구성 매니페스트 작성

#
# /home/vagrant/sidecar/webserver.yaml
#
apiVersion: v1
kind: Pod
metadata:
  name: webserver
spec:
  containers:          ## 메인 컨테이너
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: contents-vol
      readOnly: true
      
  - name: cloner       ## 사이드카 컨테이너
    image: myanjini/contents-cloner:1.0
    env:
    - name: CONTENTS_SOURCE_URL
      value: "https://github.com/myanjini/web-contents"
    volumeMounts:
    - mountPath: /data
      name: contents-vol
      
  volumes:             ## 공유 볼륨
  - name: contents-vol
    emptyDir: {}

 

파드 생성 및 확인

root@master:/home/vagrant/sidecar# kubectl apply -f webserver.yaml
pod/webserver created

root@master:/home/vagrant/sidecar# kubectl get pods -o wide
NAME        READY   STATUS              RESTARTS   AGE   IP       NODE       NOMINATED NODE   READINESS GATES
webserver   0/2     ContainerCreating   0          49s   <none>   worker-2   <none>           <none>

root@master:/home/vagrant/sidecar# kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
webserver   2/2     Running   0          53s   10.233.99.2   worker-2   <none>           <none>

 

대화형 파드(임시 파드, 테스트 파드)를 기동해서 파드의 초기 컨텐츠 출력

root@master:/home/vagrant/sidecar# kubectl run busybox --image=busybox --restart=Never --rm -it sh
If you don't see a command prompt, try pressing enter.
/ # wget -q -O - http://10.233.99.2
<!DOCTYPE html>
<html>
<head>
  <title>사이드카 패턴</title>
</head>
<body>
  <h1>사이드카 패턴</h1>
</body>
</html>

 

깃헙 리포지터리의 컨텐츠를 수정 후 커밋 & 푸시

 

약 1분 후 wget을 통해 수정한 내용이 자동으로 반영된 것을 확인

/ # wget -q -O - http://10.233.99.2
<!DOCTYPE html>
<html>
<head>
  <title>사이드카 패턴</title>
</head>
<body>
  <h1>사이드카 패턴</h1>
  <p>새로운 내용을 추가</p>
</body>
</html>

 

파드 삭제

root@master:/home/vagrant/sidecar# kubectl delete pods webserver
pod "webserver" deleted
728x90
반응형

댓글