๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿคท๐Ÿผ‍โ™€๏ธ Etc.../- linux, aws, docker, k8s

[์ฟ ๋ฒ„๋„คํ‹ฐ์Šค] k8s์˜ ๋””ํ”Œ๋กœ์ด๋จผํŠธ, Deployment ์— ๋Œ€ํ•ด์„œ

by Wonit 2021. 5. 13.

์ง€๋‚œ ์‹œ๊ฐ„ ์šฐ๋ฆฌ๋Š” Pod ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ง€๋‚œ ์‹œ๊ฐ„์— Pod ๋ฅผ ์ƒ์„ฑํ•˜๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ์ƒ์œ„ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ด์šฉํ•œ ํฌ๋“œ์˜ ์ƒ์„ฑ์—์„œ ReplicaSet ์— ๋Œ€ํ•ด์„œ๋„ ํ™•์ธํ–ˆ์—ˆ๋Š”๋ฐ, ์˜ค๋Š˜์€ ReplicaSet ๋ณด๋‹ค ํฐ ์ƒ์œ„ ์˜ค๋ธŒ์ ํŠธ์ธ Deployment ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋ ค ํ•œ๋‹ค.

 

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

๋‹ค์‹œ ํ•œ ๋ฒˆ ์ด์•ผ๊ธฐ ํ•˜์ง€๋งŒ ๋Œ€๋ถ€๋ถ„ ์˜ ์šด์˜ ์ƒํ™ฉ์—์„œ๋Š” Pod๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ์ผ์€ ๋“œ๋ฌผ๋‹ค.


๋ณดํ†ต ReplicaSet๊ณผ Pod ์ •๋ณด๋ฅผ ์ •์˜ํ•˜๋Š” ์ƒ์œ„ ์˜ค๋ธŒ์ ํŠธ์ธ ๋””ํ”Œ๋กœ์ด๋จผํŠธ(deployment)๋ฅผ yml ํŒŒ์ผ์— ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

์ด Deployment๋Š” ํฌ๋“œ์™€ ๋”๋ถˆ์–ด์„œ ์•ž์œผ๋กœ ์ž์ฃผ ๋ณด๊ฒŒ ๋  ์˜ค๋ธŒ์ ํŠธ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

 

์šฐ๋ฆฌ๊ฐ€ Deployment ์— ๋Œ€ํ•œ ์„ค์ • ์ƒํƒœ ์ •๋ณด๋“ค์„ yml ํŒŒ์ผ์— ์ •์˜ํ•˜๋ฉด ๊ผญ ํ•„์š”ํ•œ ํ•„์ˆ˜ ๊ตฌ์„ฑ ์š”์†Œ๋“ค์ด 3 ๊ฐœ ์žˆ๋‹ค.

 

  1. ๋””ํ”Œ๋กœ์ด๋จผํŠธ์˜ ์„ค์ • ์ •๋ณด (apiVersion, kind, metadata)
  2. ๋ ˆํ”Œ๋ฆฌ์นด ์…‹์˜ ์„ค์ • ์ •๋ณด (replicas, selector)
  3. ํฌ๋“œ์˜ ์„ค์ • ์ •๋ณด (template)

๋ ˆํ”Œ๋ฆฌ์นด ์…‹(ReplicaSet)์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด ํฌ๋“œ์˜ ๊ฐœ์ˆ˜์„ ์œ ์ง€ ์‹œ์ผœ์ฃผ๋Š” ์˜ค๋ธŒ์ ํŠธ์ด๋‹ค. ๋งŒ์•ฝ ์šฐ๋ฆฌ๊ฐ€ replicas๋ฅผ 3์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ํ•ด๋‹น ๋””ํ”Œ๋กœ์—๋จผํŠธ๋Š” ๋ ˆํ”Œ๋ฆฌ์นด์…‹์— ์˜ํ•ด์„œ 3๊ฐœ์˜ ํฌ๋“œ๊ฐ€ ๊ณ„์†ํ•ด์„œ ์šด์˜๋  ๊ฒƒ์ด๋‹ค.

 

์ด๋Ÿฐ ๊ตฌ์„ฑ ์š”์†Œ๋“ค๋กœ ์šฐ๋ฆฌ๋Š” ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ์ •์˜ํ•˜๊ณ  ์ƒ์„ฑํ•˜๋ฉด ํฌ๋“œ์™€ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์„ ํ•จ๊ป˜ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ  ๊ทธ๋“ค์„ ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

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

๋ฐฑ๋ฌธ์ด ๋ถˆ์—ฌ์ผ๊ฒฌ์ด๋ผ๊ณ  ๊ฐ€๋ณ๊ฒŒ ๋””ํ”Œ๋กœ์ด๋จผํŠธ ํ•˜๋‚˜๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋„์›Œ๋ณด์ž.

 

apiVersion: apps/v1
kind: Deployment # ํ•ด๋‹น yml ์ด deployment ๋กœ ์ƒ์„ฑํ•  ๊ฒƒ์ž„์„ ๋ช…์‹œ
metadata:
  name: my-nginx-deployment
spec:
  replicas: 3 # ์œ ์ง€ํ•  ํฌ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ 3์œผ๋กœ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์— ์ง€์ •
  selector:
    matchLabels:
      app: my-nginx # my-nginx ๋ผ๋Š” ๋ผ๋ฒจ์˜ ํฌ๋“œ๋ฅผ ์œ ์ง€

  template: # ํฌ๋“œ์— ๋Œ€ํ•œ ์„ค์ • ์ •๋ณด
    metadata:
      name: my-nginx-pod
      labels:
        app: my-nginx # ํฌ๋“œ์˜ ๋ผ๋ฒจ
    spec: # ํฌ๋“œ ์ƒ์„ฑ ์ŠคํŽ™
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80

 

๊ทธ๋ฆฌ๊ณ  apply ์ปค๋งจ๋“œ๋ฅผ kubectl๋กœ ์ž…๋ ฅํ•ด์„œ ๋””ํ”Œ๋กœ์ด๋จผํŠธ ํ•˜๋‚˜๋ฅผ ๋„์›Œ๋ณด์ž.

 

 

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด kubectl get deployment์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋ฉด ์ˆœ์ฐจ์ ์œผ๋กœ ํฌ๋“œ 3๊ฐœ๊ฐ€ ์˜ฌ๋ผ์˜ค๊ฒŒ ๋œ๋‹ค.

 

clear๋กœ ์ปค๋งจ๋“œ๋ผ์ธ์„ ๋” ๊น”๋”ํ•˜๊ฒŒ ํ•˜๊ณ  Pod, ReplicaSet, Deployment์˜ ์ •๋ณด๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ถœ๋ ฅํ•ด๋ณด์ž.

 

์ •๋ณด๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ kubectl get deployment, replicaset, pod ๋ช…๋ น์–ด๋ฅผ ์ฐจ๋ก€๋กœ ์‹คํ–‰์‹œ์ผœ ๋ณผ ๊ฒƒ์ด๋‹ค.

 

 

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

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

 

๋””ํ”Œ๋กœ์ด๋จผํŠธ๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๋Š”๋ฐ, ๋งŒ์•ฝ nginx์˜ ํŠน์ • ๋ฒ„์ „๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ์š”๊ตฌ์‚ฌํ•ญ์ด ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ? ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ๊ป๋‹ค ๋‹ค์‹œ ์ผœ์•ผํ• ๊นŒ?

 

์•„๋‹ˆ๋‹ค. ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋Š” ์ด๋Ÿฐ ์ƒํ™ฉ์—์„œ ๋‹จ๊ณ„์ ์ธ ์—…๋ฐ์ดํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

์ฆ‰, ๊ธฐ์กด์— ์ƒ์„ฑ๋œ ๋””ํ”Œ๋กœ์ด๋จผํŠธ์˜ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์ด ์ƒˆ๋กœ์šด ๋ฒ„์ „์˜ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์„ scalu-up ํ•˜๊ณ  ๊ธฐ์กด์˜ ๋ฒ„์ „์„ scale-down ํ•œ๋‹ค.

 

์šฐ๋ฆฌ์˜ yml ํŒŒ์ผ์—์„œ nginx ์ด๋ฏธ์ง€์˜ ๋ฒ„์ „ ํƒœ๊ทธ๋ฅผ 1.16.1 ๋ฒ„์ „์œผ๋กœ ๋ฐ”๊ฟ”๋ณด์ž.

 

 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ apply ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋„์šฐ์ž.

 

โฏ kubectl apply -f p04-deployment-nginx.yml

deployment.apps/my-nginx-deployment configured

 

ํ•ด๋‹น ๋ช…๋ น์„ ์ถœ๋ ฅํ•˜๋ฉด ์ด๋ฒˆ์—๋Š” created๊ฐ€ ์•„๋‹ˆ๋ผ configured๊ฐ€ ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Š” ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•œ ๊ฒƒ์œผ๋กœ ์ด์ œ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์ด ๋‹จ๊ณ„์ ์œผ๋กœ scaling ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

 

์‚ฌ์‹ค๋ž‘ ๋””ํ”Œ๋กœ์ด๋จผํŠธ์˜ yml ์„ค์ • ์ •๋ณด๋“ค๊ณผ ๋ ˆํ”Œ๋ฆฌ์นด์…‹์˜ yml ์„ค์ • ์ •๋ณด๋Š” ์ฐจ์ด๊ฐ€ ๊ฑฐ์˜ ์—†๋‹ค.


๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ณดํ†ต์˜ ์šด์˜ ์ƒํ™ฉ์—์„œ๋Š” ๋ ˆํ”Œ๋ฆฌ์นด์…‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๊ทธ ์ด์œ ๋Š” ๋ฐ”๋กœ ๋ฐฐํฌ์— ์žˆ๋‹ค.

 

๋””ํ”Œ๋กœ์ด๋จผํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ ˆํ”Œ๋ฆฌ์นด์…‹์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ €์žฅํ•˜๋Š” Revision์„ ๋‚จ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— Rollback ์ด ๊ฐ€๋Šฅํ•ด์ง€๊ณ  ์—ฌ๋Ÿฌ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ ์ „๋žต์„ ์ทจํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

์ด ๋‚ด์šฉ์€ ์ถ”ํ›„์— ๋” ๋ฐฐ์šธ ๋‚ด์šฉ์ธ ์นด๋‚˜๋ฆฌ ์—…๋ฐ์ดํŠธ, ๋กค๋ง ์—…๋ฐ์ดํŠธ, blue-green ์—…๋ฐ์ดํŠธ์—์„œ ์•Œ์•„๋ณด์ž.

๋Œ“๊ธ€