Skip to content

Instantly share code, notes, and snippets.

@ianychoi
Created July 27, 2021 16:27
Show Gist options
  • Select an option

  • Save ianychoi/f96c6c033b966ee60d39bb9f89c66246 to your computer and use it in GitHub Desktop.

Select an option

Save ianychoi/f96c6c033b966ee60d39bb9f89c66246 to your computer and use it in GitHub Desktop.
쿠버네티스 - Blue Green 배포 (클러스터 서비스 활용)

클러스터 IP 서비스를 활용해 Blue Green 배포 전략을 실습해봅니다.

  • 디플로이먼트 2개 생성
  • nginx11.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx11
spec:
  replicas: 3
  selector:
    matchLabels:
      app: deploy-nginx11
  template:
    metadata:
      labels:
        app: deploy-nginx11
    spec:
      containers:
      - name: deploy-nginx11
        image: nginx:1.11
  • nginx12.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-nginx12
spec:
  replicas: 3
  selector:
    matchLabels:
      app: deploy-nginx12
  template:
    metadata:
      labels:
        app: deploy-nginx12
    spec:
      containers:
      - name: deploy-nginx12
        image: nginx:1.12
  • 파드 생성 및 접근 확인
# 파드 생성
curl -s -O https://raw.githubusercontent.com/gasida/DKOS/main/5/nginx11.yaml
curl -s -O https://raw.githubusercontent.com/gasida/DKOS/main/5/nginx12.yaml
kubectl apply -f nginx11.yaml
kubectl apply -f nginx12.yaml

# nginx v1.11 의 index.html 내용 변경
for pod in $(kubectl get pod -l app=deploy-nginx11 |awk 'NR>1 {print $1}'); do kubectl exec $pod -- /bin/sh -c "hostname > /usr/share/nginx/html/index.html; echo 'nginx:v1.11 END' >> /usr/share/nginx/html/index.html"; done

# nginx v1.12 의 index.html 내용 변경
for pod in $(kubectl get pod -l app=deploy-nginx12 |awk 'NR>1 {print $1}'); do kubectl exec $pod -- /bin/sh -c "hostname > /usr/share/nginx/html/index.html; echo 'nginx:v1.12 END' >> /usr/share/nginx/html/index.html"; done

# nginx v1.11 의 Pod IP로 curl 접속 시도
for podIP in $(kubectl get pod -o wide -l app=deploy-nginx11 |awk 'NR>1 {print $6}'); do curl -s $podIP; done

# nginx v1.12 의 Pod IP로 curl 접속 시도
for podIP in $(kubectl get pod -o wide -l app=deploy-nginx12 |awk 'NR>1 {print $6}'); do curl -s $podIP; done
  • svc-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
spec:
  ports:
    - name: svc-nginx
      port: 9000
      targetPort: 80
  selector:
    app: deploy-nginx11
  • 서비스 생성 및 접근 확인
# 서비스 생성
curl -s -O https://raw.githubusercontent.com/gasida/DKOS/main/5/svc-nginx.yaml
kubectl apply -f svc-nginx.yaml

# 서비스/IP 확인
kubectl get service svc-nginx
kubectl get service svc-nginx -o jsonpath='{.spec.clusterIP}' ; echo

# 엔드포인트 IP 확인
kubectl get endpoints svc-nginx

# 모든 노드에서 SVC IP 로 접근 가능
curl -s <SVC IP>:9000

# Pod 로그 실시간 확인
kubectl logs -l 'app in (deploy-nginx11,deploy-nginx12)' -f --max-log-requests 8

# 마스터 노드 자체에서 실행 : 아래 while 실행 후 동작 확인
SVC=<SVC IP>
for i in {1..100}; do curl -s $SVC:9000 ; done | sort | uniq -c | sort -nr
while true; do curl -s --connect-timeout 1 $SVC:9000 ; echo "--------------" ; date "+%Y-%m-%d %H:%M:%S" ; sleep 1; done

# Blue/Green 업데이트 실행
kubectl get svc svc-nginx -o yaml | sed -e "s/app: deploy-nginx11/app: deploy-nginx12/" | kubectl apply -f -

# 버전 롤백 실행
kubectl get svc svc-nginx -o yaml | sed -e "s/app: deploy-nginx12/app: deploy-nginx11/" | kubectl apply -f -
kubectl get svc svc-nginx -o yaml | sed -e "s/app: deploy-nginx11/app: deploy-nginx12/" | kubectl apply -f -

# 정상 업데이트 후 기본 11 버전을 삭제 혹은 replicas 갯수를 0으로 줄여놓기
kubectl scale deployment deploy-nginx11 --replicas=0
  • 다음 실습을 위해 생성한 오브젝트 전부 삭제
kubectl delete deploy,svc --all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment