๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“š Book Review/๐Ÿ“˜ Kubernetes Best Practice

[Kubernetes Best Practice] 6. ๋ฒ„์ „, ๋ฆด๋ฆฌ์Šค, ๋กค์•„์›ƒ

by Seongpyo Hong 2021. 3. 1.

์ด ๊ธ€์€ Kubernetes Best Practice๋ฅผ ์ฝ๊ณ  ๊ฐœ์ธ์ ์œผ๋กœ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ์ž˜ ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ๋‚ด์šฉ์€ ์ฑ…์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์œผ๋ฉฐ ์ž์„ธํ•œ ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์ฑ…์„ ๊ตฌ๋งคํ•ด์„œ ์ฝ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ๋“œ๋ฆฝ๋‹ˆ๋‹ค. 

์ตœ๊ทผ ์„œ๋น„์Šค ๋ฉ”์‹œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฑฐ๋‚˜ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋“ค์—ˆ๋˜ ์ƒ๊ฐ์€ ์ด ๋ฐฉ๋ฒ•์ด ์ตœ์„ ์ธ๊ฐ€? ๋ผ๋Š” ์ƒ๊ฐ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ๊ถ๊ธˆ์ฆ์„ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ๋„์›€์ด ๋ ๋งŒํ•œ ์ฑ…์„ ๋ฐœ๊ฒฌํ•ด์„œ ์ฝ์–ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.


 

์‹œ์Šคํ…œ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์—…๊ทธ๋ ˆ์ด๋“œ, ๋ฒ„์ „ ๊ด€๋ฆฌ, ๋ณ€๊ฒฝ์— ๋Œ€ํ•ด ์ž๋™ํ™” ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š”๋ฐ ๋งŽ์€ ์–ด๋ ค์›€์ด ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค, ํ•˜์‹œ์ฝ”ํ”„ ๋…ธ๋งˆ๋“œ์™€ ๊ฐ™์€ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ดํ„ฐ๋Š” ๋Ÿฐํƒ€์ž„์— ์ž๋™ํ™” ๊ด€๋ จ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฒ„์ „ ๊ด€๋ฆฌ, ๋ฆด๋ฆฌ์Šค, ๋ฐฐํฌ์— ๋Œ€ํ•œ ๋ณต์žก๋„๋ฅผ ๋‚ฎ์ถ”๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ๋ฒ„์ „, ๋ฆด๋ฆฌ์Šค, ๋ฐฐํฌ์— ๋Œ€ํ•œ ๋ชจ๋ฒ” ์‚ฌ๋ก€์— ๋Œ€ํ•˜์—ฌ ๋‹ค๋ฃจ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๋ฒ„์ „

์ผ๋ฐ˜์ ์œผ๋กœ ๋ฒ„์ „ ํ‘œ๊ธฐ์—๋Š” ์‹œ๋ฉ˜ํ‹ฑ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์‹œ๋ฉ˜ํ‹ฑ ๋ฒ„์ „์€ (Major).(Minor).(Patch) ํ˜•์‹์œผ๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค. ํŒจ์น˜ ๋ฒ„์ „์€ ์‚ฌ์†Œํ•œ ๋ณ€๊ฒฝ์‚ฌํ•ญ ๋งˆ๋‹ค ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋˜๊ณ , ๋งˆ์ด๋„ˆ ๋ฒ„์ „์€ ๋ฒ„์ „์˜ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ–ˆ์ง€๋งŒ ์ด์ „ ๋ฒ„์ „๊ณผ ํ•˜์œ„ ํ˜ธํ™˜์ด ๊ฐ€๋Šฅํ•œ ๋ณ€๊ฒฝ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์ด์ € ๋ฒ„์ „์€ ๋Œ€๊ทœ๋ชจ์˜ ์ฝ”๋“œ ๋ณ€๊ฒฝ์œผ๋กœ ๋ฉ”์ด์ € ๋ฒ„์ „ ์‚ฌ์ด์˜ API๋Š” ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.


๋ฆด๋ฆฌ์Šค

 ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ๋ฆด๋ฆฌ์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๋”ฐ๋กœ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆด๋ฆฌ์Šค ์ •๋ณด๋ฅผ Deployment metadata.labels, pod.spec.template.metadata.label ํ•„๋“œ์— ๋„ฃ์Œ์œผ๋กœ์จ ๊ด€๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฆด๋ฆฌ์Šค ์ •๋ณด๋ฅผ ๋„ฃ๋Š” ์‹œ์ ์€ CD๋ฅผ ํ†ตํ•ด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ์˜ํ•ญ์„ ๋ผ์น  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๋ช…์‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ข€ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด Helm์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Helm์€ ๋ฆด๋ฆฌ์Šค ๋‚ด์—ญ์„ ์ถ”์ ํ•ด์ฃผ๋ฉฐ ๋Œ€๋ถ€๋ถ„์˜ CD ํˆด์€ Helm๊ณผ ํ†ตํ•ฉํ•˜์—ฌ ๋ฆด๋ฆฌ์Šค ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ด์ค๋‹ˆ๋‹ค.

๋ฆด๋ฆฌ์Šค ์ด๋ฆ„์€ ์„œ๋น„์Šค ๋ฉ”์‹œ๊ฐ™์€ ์„ธ๋ฐ€ํ•œ ๋ผ์šฐํŒ…์„ ์„ค์ •ํ•  ๋•Œ ์šด์˜ ๋ฐ ๊ด€๋ฆฌ์˜ ์šฉ์ด์„ฑ์„ ์œ„ํ•ด ์•ฝ์†๋œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ex) stable, canary


๋กค์•„์›ƒ

Deployment๊ฐ€ ๋“ฑ์žฅํ•˜๊ธฐ ์ด์ „์˜ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ๋Š” ReplicaSet์„ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ์œ„ํ•œ ๋ฐฉ๋ฒ•์€ CLI๋ฅผ ํ†ตํ•œ ๋ฐฉ๋ฒ•๋ฐ–์— ์—†์—ˆ๊ณ , ์ด๋Š” ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๊ฐ€ ์ง€ํ–ฅํ•˜๋Š” ์„ ์–ธ์ „ ํŒจ๋Ÿฌ๋‹ค์ž„์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์ดํ›„ ๋“ฑ์žฅํ•œ Deployment Controller๋Š” spec.template์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์„ ์–ธ๋œ ์ƒํƒœ๋ฅผ ์ฝ์–ด ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•˜๋ฉฐ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

Deployment๊ฐ€ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ RollingUpdate์™€ recreate ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. RollingUpdate๋Š” ์„œ๋น„์Šค์˜ ์ค‘๋‹จ์—†์ด ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, recreate๋Š” ๋ชจ๋“  Pod์„ ์ค‘๋‹จํ•˜๋”๋ผ๋„ ์„œ๋น„์Šค์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š” ํŠน์ • ์›Œํฌ๋กœ๋“œ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


๋ฒ„์ „, ๋ฆด๋ฆฌ์Šค, ๋กค์•„์›ƒ ๋ชจ๋ฒ” ์‚ฌ๋ก€

  • ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€์™€ ๋ฐฐํฌ ๋ฒ„์ „์€ ๋…๋ฆฝ์ ์ธ ๋ผ์ดํ”„์‚ฌ์ดํด์„ ๊ฐ€์ง„๋‹ค๋Š” ์ ์„ ์œ ์˜ํ•˜์ž. ์•„๋ž˜ ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€์˜ ๋ฒ„์ „์€ 1.1.6์ด์ง€๋งŒ Pod์˜ ๋ช…์„ธ๋Š” 1.1.8์„ ๋”ฐ๋ฅด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ConfigMap, Secret๊ณผ ๊ฐ™์€ ๋ณ€๊ฒฝ์œผ๋กœ ์ธํ•ด pod ๋ช…์„ธ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ „์ฒด ๋ฐฐํฌ๋ฒ„์ „์€ 1.3.5๋กœ ์ „์ฒด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์กด์žฌํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

apiVersion: apps/v1
kind: Deployment
metadata: 
    name: server-deploy
    appVersion: 1.3.5
    env: prod
    release: stable
    release number: 1234567
spec:
    selector:
    	matchLabels:
            app: server
            appVersion: 1.1.8
            env: prod
    template:
    	metadata:
            labels:
            	app: server
                appVersion: 1.1.8
                env: prod
        spec:
            containers:
            - name: web-server
              image: sphong0417/web-server:v1.1.6
...

 

  • ๋ฆด๋ฆฌ์Šค ์ด๋ฆ„ ๋ฐ ๋ฒˆํ˜ธ๋ฅผ ํ†ตํ•ด CI/CD ๋ฆด๋ฆฌ์Šค๋ฅผ ์ถ”์ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์ด metadata ํ•„๋“œ์— CI/CD์˜ ์‹ค์ œ ๋ฆด๋ฆฌ์Šค ์ •๋ณด์™€ ์—ฐ๊ด€๋˜๋Š” ๋ ˆ์ด๋ธ”์„ ์ถ”๊ฐ€ํ•˜์—ฌ CI/CD ๊ณผ์ • ์ถ”์  ๋ฐ ๋กค๋ฐฑ์„ ์‹๋ณ„ํ•˜๊ธฐ ์šฉ์ดํ•ด์ง‘๋‹ˆ๋‹ค.

  • Helm์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Helm ์ฐจํŠธ์— ํฌํ•จ๋˜์ง€ ์•Š์€ ์„œ๋น„์Šค๋“ค๋„ ํ•จ๊ป˜ ์—…๋ฐ์ดํŠธ ๋ฐ ๋กค๋ฐฑ์ด ๋  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ๋ คํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด Helm Chart์— ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•œ ๋ฉ”๋‹ˆํŽ˜์ŠคํŠธ๊ฐ€ ์กด์žฌํ•˜๊ณ  ์ด๋ฅผ ์—…๋ฐ์ดํŠธ ๋ฐ ๋กค๋ฐฑ ํ•  ๋•Œ Helm ์™ธ๋ถ€์˜ DB ๋ฐฑ์—…์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ด ์ฒ˜๋ฆฌ๋ฅผ ํ•จ๊ป˜ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. Helm์€ lifecycle Hook์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ์ด๋Ÿฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.

๋Œ“๊ธ€