CLOUD/Docker&Kubernetes

[Kubernetes] Controller - Deployment (Recreate / RollingUpdate / BlueGreen)

alsruds 2023. 4. 26. 11:56

 

< Deployment >

- ReplicaSet 을 컨트롤

- 기본 방식 : RollingUpdate


 

 

● Deployment

Recreate 방식

# Recreate 방식
apiVersion: apps/v1
kind: Deployment		# deployment 선언
metadata:
  name: deployment-1		# deployment 이름
spec:
  replicas: 2			# 생성할 replicaset 개수
  strategy:
    type: Recreate
  revisionHistoryLimit: 1	# 기억할 업데이트 개수 (이전 버전으로 돌아갈 수 있음)
  selector:
    matchLabels:
      type: app
  template:
    metadata:
      labels:
        type: app
    spec:
      containers:
      - name: container
        image: nuy0307/hello:8000
        ports:
        - containerPort: 8000
      terminationGracePeriodSeconds: 0

 

RollingUpdate 방식

# Rolling Update 방식
apiVersion: apps/v1
kind: Deployment		# deployment 생성
metadata:
  name: deployment-2
spec:
  selector:
    matchLabels:
      type: app
  replicas: 4
  strategy:
    type: RollingUpdate		# 롤링업데이트 방식
  minReadySeconds: 10		# 파드가 생성되고 프로그램이 실행되는 시간
  template:
    metadata:
      labels:
        type: app
    spec:
      containers:
      - name: container
        image: nuy0307/hello:8000
      terminationGracePeriodSeconds: 3

 

Blue / Green 방식

- 첫 번째 디플로이먼트 생성

# deployment-1 (recreate 방식)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-1
spec:
  replicas: 2
  strategy:
    type: Recreate
  revisionHistoryLimit: 1
  selector:
    matchLabels:
      ver: v1
  template:
    metadata:
      labels:
        ver: v1
    spec:
      containers:
      - name: container
        image: nuy0307/hello:1
      terminationGracePeriodSeconds: 5

 

- 두 번째 디플로이먼트 생성

# delployment-2 (recreate 방식)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-2
spec:
  replicas: 2
  strategy:
    type: Recreate
  revisionHistoryLimit: 1
  selector:
    matchLabels:
      ver: v2	# 라벨 변경
  template:
    metadata:
      labels:
        ver: v2	# 라벨 변경
    spec:
      containers:
      - name: container
        image: nuy0307/hello:2	# 다른 이미지 선택
      terminationGracePeriodSeconds: 5

 

- 서비스 생성 : selector 를 v1 으로 연결했다가 편집으로 v2 로 변경

apiVersion: v1
kind: Service
metadata:
  name: hello-svc
spec:
  selector:
    ver: v1
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer

 

- 확인하기

: 첫 번째 디플로이먼트 생성(ver: v1) & 서비스 연결 후 명령어 실행

while true; do curl http://[service 외부엔드포인트 ip]/; sleep 1; done

: 두 번째 디플로이먼트 생성(ver: v2) 후 서비스 셀렉터 변경 > 실행했던 명령어 결과 확인

 

잘 변경이 됐어요~

 

(+) 생성했던 이미지 도커파일 + hello.js

# Dockerfile
FROM node:lts-slim
ADD ./hello.js /hello.js
EXPOSE 80
CMD node /hello.js

# hello.js
var http = require('http');
var content = function(req, resp) {
 resp.end("Hello Kubernetes! v1\n");	# nuy0307/hello:1 > v1 | nuy0307/hello:2 > v2
 resp.writeHead(200);
}
var w = http.createServer(content);
w.listen(80);

 

 

[ 이전 버전으로 돌아가기 ]

● 명령어로 확인가능

- revisionHistoryLimit 으로 설정해주었던 업데이트 개수만큼 기억

# 업데이트 내역 확인
kubectl rollout history deployment [deployment 이름]

# 이전 버전으로 돌아가기
kubectl rollout undo deployment --to-revision=[버전 숫자]

높은 숫자가 현재 버전