๐Ÿš‹ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค Deployments [๋กค๋ง ์—…๋ฐ์ดํŠธ, RollOut, RollBack]

๊น€์„ฑ์ธยท2023๋…„ 10์›” 12์ผ
0

[DevOps] ๐ŸณDocker & Kubernetes

๋ชฉ๋ก ๋ณด๊ธฐ
21/62

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

Deployment๋Š” Pod์™€ ReplicaSet์— ์„ ์–ธ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฅผ ์ œ๊ณตํ•จ.

  • Deployment ๋‚ด๋ถ€์— ์›ํ•˜๋Š” ์ƒํƒœ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์ปจํŠธ๋กค ๋‹จ๊ณ„์—์„œ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟˆ
  • ์ƒˆ๋กœ์šด ReplicaSet์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜, ์กด์žฌํ•˜๋Š” ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ์‚ญ์ œํ•˜๊ณ ๋‚˜, ์ƒˆ๋กœ์šด ๋””ํ”Œ๋กœ์ด๋จผํŠธ์— ์ข…์† ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ.
  • Deployment์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ReplicaSet์„ ๋‹ค๋ฃจ์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•จ.

UseCase

  • ReplicaSet์„ ๋กค ์•„์›ƒํ•˜๊ธฐ์œ„ํ•ด ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ์ƒ์„ฑ
    • ๋ ˆํ”Œ๋ฆฌ์นด์…‹์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ Pod๋ฅผ ๋งŒ๋“ค๋•Œ, rollout ์ƒํƒœ๋ฅผ ํ†ตํ•ด ์„ฑ๊ณต์—ฌ๋ถ€๋ฅผ ํ™•์ธ
  • Deployment์˜ PodTemplateSpec์„ ์ˆ˜์ •ํ•˜์—ฌ Pod์˜ ์ƒˆ๋กœ์šด ์ƒํƒœ๋ฅผ ์„ ์–ธ
    • ์ƒˆ๋กœ์šด ReplicaSet์„ ์ƒ์„ฑ์‹œํ‚ค๊ณ  ์ด์ „ ReplicaSet์— ์กด์žฌํ•˜๋Š” Pod๋ฅผ ์ƒˆ๋กœ์šด ReplicaSet์œผ๋กœ ์ด๋™์‹œํ‚ด (at a controlled rate)
    • ๊ฐ๊ฐ์˜ ์ƒˆ๋กœ์šด ReplicaSet์€ Deployment์˜ ์ˆ˜์ •์‚ฌํ•ญ์„ ์ตœ์‹ ํ™”ํ•œ๋‹ค.
  • ํ˜„์žฌ Deployment ์ƒํƒœ๊ฐ€ ์ •์ƒ์ ์ด์ง€ ์•Š๋‹ค๋ฉด Deployment ์ด์ „ ์ˆ˜์ •์ƒํƒœ๋กœ Rollback
    • Deployment์˜ ์ˆ˜์ •์‚ฌํ•ญ์„ ๊ฐ rollback์ด ์ˆ˜์ •ํ•จ
  • ๋” ๋งŽ์€ load๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด Deployment์˜ ์ˆ˜์น˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ด. (Scale Up)
  • Deployment์˜ PodTemplateSpec์„ ๋‹ค์ค‘์œผ๋กœ ์ ์šฉํ•˜๊ธฐ์œŒํ•ด rollout์„ ๋ฉˆ์ถ”์—ˆ๋‹ค๊ฐ€, ๋‹ค์‹œ ์ƒˆ๋กœ์šด roll out์„ ์ˆ˜ํ–‰
  • rollout ๋ฉˆ์ถค ์ƒํƒœ๋ฅผ ์‹๋ณ„ํ•  Deployment ์ƒํƒœ๋ฅผ ์‚ฌ์šฉ
  • ๋”์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ReplicaSet์„ ์‚ญ์ œ

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx # ๊ฐ™์€๊ฐ’
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx  # ๊ฐ™์€๊ฐ’
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
  • replicas : ํŒŒ๋“œ๋ฅผ ๋ช‡๊ฐœ ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€ ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ, ๋ ˆํ”Œ๋ผํ‚ค์…‹๊ณผ ๋™์ผํ•™ ใ…”์ž‘์„ฑ
  • matchLabels: metadata.labels ํ•˜์œ„ํ•„๋“œ์™€ ๊ฐ™์€ ๊ฐ’์„ ํ• ๋‹นํ•จ.
  • template.spec.containers: ํŒŒ๋“œ์˜ ์„ค์ •์ •๋ณด๊ฐ€ ๋‹ด๊ธด ์ปจํ…Œ์ด๋„ˆ ์ •๋ณด

๋ช…๋ น์–ด

๋””ํ”Œ๋กœ์ด๋จผํŠธ ์Šค์ผ€์ผ๋ง

kubectl edit deploy <deploy name> : replicas ์ง์ ‘ ์ˆ˜์ •
kubectl edit deploy <deploy name> --replicas=<number> : replicas ๊ฐ’ ์ˆ˜์ •


๋กค๋ง ์—…๋ฐ์ดํŠธ

kubectl create -f http-go-deployment.hyml --record=true

  • --record=trueํžˆ์Šคํ† ๋ฆฌ์— ์‚ฌ์šฉํ•  ๋ช…๋ น์–ด๋ฅผ ์ €์žฅ ๊ฐ€๋Šฅ. (์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—…๋ฐ์ดํŠธ ํžˆ์Šคํ† ๋ฆฌ)
apiVersion: apps/v1
kind: Deployment
metadata:
    name: http-go-deployment
 	labels:
		app: http-go
spec:
	replicas: 3
	selector:
		matchLabels:
			app: http-go
	template:
		metadata:
			name: http-go
            labels:
            	app: http-go
        spec:                                
            containers:
            	- image: gasbugs/http-go:v1
                  name: http-go
#rollingupdate ์ „๋žต
spec:
	strategy:
    	type: RollingUpdate
  • type: RollingUpdate -> ๊ธฐ๋ณธ๊ฐ’
    • ์˜ค๋ž˜๋œ ํฌ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ์ œ๊ฑฐํ•˜๋Š” ๋™์‹œ์— ์ƒˆ๋กœ์šด ํฌ๋“œ ์ถ”๊ฐ€
    • ์š”์ฒญ ์ฒ˜๋ฆฌํ• ์ˆ˜ ์žˆ๋Š”์–‘ ๊ทธ๋Œ€๋กœ ์œ ์ง€
  • type: Recreate
    • ์ƒˆํฌ๋“œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์ „์— ์ด์ „ ํฌ๋“œ๋ฅผ ๋ชจ๋‘ ์‚ญ์ œ
    • ์—ฌ๋Ÿฌ ๋ฒ„์ „ ๋™์‹œ ์‹คํ–‰ ๋ถˆ๊ฐ€๋Šฅ
    • ํƒ€์ž„ ๋‹ค์šด ์กด์žฌ
# ์ตœ๋Œ€/์ตœ์†Œ ๋ช‡๊ฐœ์˜ pod์„ ์šด์˜ํ• ์ง€
spec:
	strategy:
    	rollingUpdate:
        	maxSurge: 1
            maxUnvaliable: 1
        type: RollingUpdate
  • maxSurge
    • ๊ธฐ๋ณธ๊ฐ’ 25%, ๊ฐœ์ˆ˜๋กœ๋„ ์„ค์ • ๊ฐ€๋Šฅ
    • ์ตœ๋Œ€๋กœ ์ถ”๊ฐ€ ๋ฐฐํฌ๋ฅผ ํ—ˆ์šฉํ•  ๊ฐœ์ˆ˜ ์„ค์ •
    • 4๊ฐœ์ธ ๊ฒฝ์šฐ 1๊ฐœ๊ฐ€ ์„ค์ •๋จ (์ด 5๊ฐœ๊นŒ์ง€ ํฌ๋“œ ๋™์‹œ ์šด์˜)
  • maxUnavailable
    • ๊ธฐ๋ณธ๊ฐ’ 25%, ๊ฐœ์ˆ˜ ์„ค์ • ๊ฐ€๋Šฅ
    • ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ํฌ๋“œ์ด ๊ฐœ์ˆ˜ ์„ค์ •
    • 4๊ฐœ์ธ ๊ฒฝ์šฐ 1๊ฐœ ์„ค์ • (์ด 4-1๊ฐœ๋Š” ์šด์˜ํ•œ๋‹ค)

๊ฐ’์ด ํฌ๋ฉด ๋ถ€ํ•˜๊ฐ€ ์žˆ์ง€๋งŒ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋น ๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

spec:
	revisionHistoryLimit: 10
  • revision ๊ฐœ์ˆ˜ ์œ ์ง€ (๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ • ์ƒํƒœ ๋ฒ„์ „ ๊ฐœ์ˆ˜)

sudo get deploy <deployname> -o yaml


๋””ํ”Œ๋กœ์ด๋จผํŠธ ์—…๋ฐ์ดํŠธ

kubectl set image deploy <deployment ์ด๋ฆ„> <์ปจํ…Œ์ด๋„ˆ์ด๋ฆ„>=<์ด๋ฏธ์ง€ํŒŒ์ผ> : ์ด๋ฏธ์ง€ ์—…๋ฐ์ดํŠธ

'kubectl patch deploy <deployment ์ด๋ฆ„> -p '{"spec": {"minReadySeconds": 10}}': ์—…๋ฐ์ดํŠธ ๊ณผ์ •์„ ์œ„ํ•ด ์—…๋ฐ์ดํŠธ ์†๋„ ์กฐ์ ˆ


๋””ํ”Œ๋กœ์ด๋จผํŠธ expose

kubectl expose deploy <deployment ์ด๋ฆ„> --port 8080

๋ชจ๋‹ˆํ„ฐ๋ง ์‹œ์Šคํ…œ ๊ด€์ฐฐ

kubectl run -it --rm --image busybox -- bash

์—…๋ฐ์ดํŠธ ํ›„ ๋‹ค์‹œ ์‹คํ–‰

kubectl set image deploy <deployment ์ด๋ฆ„> <์ปจํ…Œ์ด๋„ˆ์ด๋ฆ„>=<์ด๋ฏธ์ง€ํŒŒ์ผ>

์ „์—๋Š” ReplicaSet์ด http-go-deployment-5c4fc94c99 ์˜€๋Š”๋ฐ, ํ•ด๋‹น ๋ ˆํ”Œ๋ฆฌ์นด์…‹์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง„ pod๋“ค์ด Terminating ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฐ˜๋ฉด ์ƒˆ๋กœ์šด Replicaset์ธ d46c7cd68์ด ์ƒˆ๋กœ ์ƒ์„ฑ๋˜์–ด ์ด๋ฏธ์ง€๊ฐ€ ์—…๋ฐ์ดํŠธ๋œ pod๊ฐ€ pending ์ƒํƒœ์—์„œ Running ์ƒํƒœ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์„ ํ™•์ธ๊ฐ€๋Šฅ

๋‹ค์‹œ http ์š”์ฒญ์„ ํ™•์ธ

Pending ๋˜์—ˆ๋‹ค๊ฐ€ Pod๊ฐ€ ๊ต์ฒด๋˜๋Š” ์ƒํ™ฉ์—์„œ๋Š” "Welcome! v1" ๊ณผ "Welcome! v2"๊ฐ€ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์ถœ๋ ฅ๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค.
ํ•˜์ง€๋งŒ ์ง€๊ธˆ์€ ๋ชจ๋“  ReplicaSet์˜ ๊ต์ฒด๋ฅผ Deployment๊ฐ€ ์™„๋ฃŒํ•œ ์ƒํƒœ์—ฌ์„œ Welcome! v2 ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๋ชจ์Šต


์—…๋ฐ์ดํŠธ ํ›„ ํ™•์ธ

์•ฝ 10์ดˆ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ์ƒˆ๋กœ์šด pod๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.


๋ ˆ์ฝ”๋“œ ํ™•์ธ

kubectl rollout status deploy <deployment ์ด๋ฆ„> : statusํ™•์ธ
kubectl rollout history deploy <deployment ์ด๋ฆ„> : historyํ™•์ธ

  • ์ด 10๊ฐœ ์ค„ ์ €์žฅ, ๋ ˆํ”Œ๋ฆฌ์นด์…‹์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•จ

์ด๋ฏธ์ง€ ์—…๋ฐ์ดํŠธ ํ›„ ๋ ˆ์ฝ”๋“œ ํ™•์ธ

๋ ˆํ”Œ๋ฆฌ์นด์…‹์„ ์—ฌ๋Ÿฌ๊ฐœ ๋„์› ์ง€๋งŒ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” pod์˜ ๊ฐœ์ˆ˜์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.
(์ด์ „ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์€ ๋ฐฑ์—…๋ณธ์ด ๋˜๋Š”๊ฒƒ)


ํ•œ๋‹จ๊ณ„ ๋” ์—…๋ฐ์ดํŠธ

5fc96f8f65 : ๋ ˆํ”Œ๋ฆฌ์นด์…‹์ด ์ƒˆ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ์‹œ์ž‘๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Œ.


๋กค๋ฐฑ

  1. kubectl set image deploy <deployment์ด๋ฆ„> <์ปจํ…Œ์ด๋„ˆ์ด๋ฆ„>=<์ด๋ฏธ์ง€ํŒŒ์ผ>
  2. kubectl rollout undo deploy <deployment์ด๋ฆ„> : undo ์ด์ „ ๋ฒ„์ „์œผ๋กœ ๋Œ์•„๊ฐ
  3. kubectl rollout undo deploy <deployment์ด๋ฆ„> --to-revision=1 : ํŠน์ • ๋ฒ„์ „์œผ๋กœ ๋Œ์•„๊ฐ

    ์ด์ „๋ฒ„์ „์œผ๋กœ ๋Œ์•„๊ฐ€๊ณ , 2๋ฒˆ-> 4๋ฒˆ์œผ๋กœ ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ๋” ์ตœ๊ทผ revision์œผ๋กœ ๋ณ€๊ฒฝ๋จ.

  • 1๋ฒ„์ „์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ€๊ณ  ๋ ˆํ”Œ๋ฆฌ์นด์…‹์— pod๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธ ๊ฐ€๋Šฅํ•จ.
  • history๋„ 1->5๋ฒˆ์œผ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธ๊ฐ€๋Šฅ

๋กค์•„์›ƒ ์ผ์‹œ์ค‘์ง€/์žฌ์‹œ์ž‘

kubectl rollout pause deploy <deployment ์ด๋ฆ„> : ์—…๋ฐ์ดํŠธ ์ค‘์— ์ผ์‹œ์ •์ง€
kubectl rollout undo deploy <deployment ์ด๋ฆ„> : ์—…๋ฐ์ดํŠธ ์ผ์‹œ์ค‘์ง€ ์ค‘ ์ทจ์†Œ
kubectl rollout resume deploy <deployment ์ด๋ฆ„> : ์—…๋ฐ์ดํŠธ ์žฌ์‹œ์ž‘

์—…๋ฐ์ดํŠธ ์ž˜๋˜๋Š”์ง€ ์ž ์‹œ ์ค‘์‹œํ•ด๋†“๊ณ  ์˜ฌ๋ผ๊ฐ„ Pod ํ™•์ธ ํ›„ ๋‹ค์‹œ ์—…๋ฐ์ดํŠธ ์žฌ์‹œ์ž‘\

  • ์—…๋ฐ์ดํŠธ ์‹คํŒจ ์ผ€์ด์Šค
    • ํ• ๋‹น๋Ÿ‰ ๋ถ€์กฑ | ๋ ˆ๋””๋„ค์Šค ํ”„๋กœ๋ธŒ ์‹คํŒจ | ์ด๋ฏธ์ง€ ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜ | ๊ถŒํ•œ ๋ถ€์กฑ | ์ œํ•œ ๋ฒ”์œ„ | ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜
spec:
    processDeadlineSeconds: ์—…๋ฐ์ดํŠธ ์‹คํŒจํ›„ ์—…๋ฐ์ดํŠธ๋ฅผ ์ค‘์ง€์‹œํ‚ค๊ธฐ๊นŒ์ง€์˜ ์‹œ๊ฐ„

0๊ฐœ์˜ ๋Œ“๊ธ€