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

[Kubernetes Best Practice] 8. ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ

by Seongpyo Hong 2021. 3. 1.

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

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


์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์Šค์ผ€์ค„๋Ÿฌ

Kubernetes Master Node์— ์กด์žฌํ•˜๋Š” ์Šค์ผ€์ค„๋Ÿฌ๋Š” ํด๋Ÿฌ์Šคํ„ฐ์™€ ์‚ฌ์šฉ์ž์˜ ๋ฆฌ์†Œ์Šค ์ œ์•ฝ์— ์ตœ์ ํ™” ๋˜๋„๋ก Pod์„ ๋ฐฐ์น˜ํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ ๋…ผ๋ฆฌ ์กฐ๊ฑด ๋ฐ ์šฐ์„  ์ˆœ์œ„ ๊ธฐ๋ฐ˜์˜ ์Šค์ฝ”์–ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • ๋…ผ๋ฆฌ ์กฐ๊ฑด
    ์Šค์ผ€์ค„๋ง ์‹œ ์‚ฌ์šฉํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๊ธฐ๋Šฅ์€ ์ฐธ/๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋…ผ๋ฆฌ ์กฐ๊ฑด ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋…ผ๋ฆฌ์กฐ๊ฑด์— ๋Œ€ํ•ด ๊ฑฐ์ง“์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋Š” ์Šค์ผ€์ค„๋ง ํ›„๋ณด์—์„œ ์ œ๊ฑฐ๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ์—์„œ๋Š” ์ด๋ฅผ Filtering์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์šฐ์„ ์ˆœ์œ„
    ๋…ผ๋ฆฌ ์กฐ๊ฑด์œผ๋กœ ์ฐธ์— ํ•ด๋‹นํ•˜๋Š” ๋…ธ๋“œ๋Š” ์šฐ์„  ์ˆœ์œ„ ์Šค์ฝ”์–ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ์ˆœ์œ„๊ฐ€ ์ •ํ•ด์ง‘๋‹ˆ๋‹ค. ์ด ์ค‘ ๊ฐ€์žฅ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๋…ธ๋“œ์— Pod์ด ์Šค์ผ€์ค„๋ง๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ™์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด Round Robin Tournament ๋ฐฉ์‹์œผ๋กœ ๋ฐฐ์น˜ํ•  ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ํ†ตํ•ด ์ตœ์ ํ™”๋œ ์Šค์ผ€์ค„๋ง ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์ง€๋งŒ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๊ฒฝ์šฐ ๋”์šฑ ์„ธ๋ฐ€ํ•œ ์Šค์ผ€์ค„๋ง์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • PodAffinity / PodAntiAffinity
    Pod์˜ ๊ฐ„์˜ ๋ฐฐ์น˜ ๊ทœ์น™์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. PodAffinity๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” Pod์ด ์žˆ๋Š” ๋…ธ๋“œ์— ๋ฐฐํฌ๋ฅผ ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋ฉฐ PodAntiAffinity๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” Pod์ด ์žˆ๋Š” ๋„๋Š์— ๋ฐฐํฌ๋ฅผ ํ•˜์ง€ ๋ง๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: server
spec:
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      labels:
        app: server
    spec:
      containers:
        - name: server
          image: sphong.com/server:v1.1.0
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - server
            topologyKey: "kubernetes.io/hostname"
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - redis
            topologyKey: "kubernetes.io/hostname"

 

์œ„์˜ ์˜ˆ์ œ๋Š” server ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋ฐฐํฌํ•  ๋•Œ, PodAffinity๋ฅผ ํ†ตํ•ด redis pod์ด ๋ฐฐํฌ๋œ ๋…ธ๋“œ์— ๋ฐฐํฌ๋ฅผ ํ•ด์•ผํ•˜๋ฉฐ, server pod์ด ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์—๋Š” ๋ฐฐํฌํ•  ์ˆ˜ ์—†์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜์˜ ๋…ธ๋“œ๊ฐ€ ์‹คํŒจํ•˜๋”๋ผ๋„ Pod์„ ๋‹ค๋ฅธ ๋…ธ๋“œ์— ๋ถ„์‚ฐํ•˜์—ฌ ๋ฐฐ์น˜ํ•จ์œผ๋กœ์จ ์žฅ์• ์— ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • NodeSelector / NodeAffinity
    NodeSelector๋Š” Node Label์„ ํ†ตํ•ด ํŠน์ • ๋…ธ๋“œ์— Pod์„ ์Šค์ผ€์ค„๋งํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. NodeAffinity๋Š” Pod Affinity์™€ ์œ ์‚ฌํ•˜๊ฒŒ Node Label์„ ํ†ตํ•ด ์–ด๋–ค ๋…ธ๋“œ์— ์Šค์ผ€์ค„๋งํ•  ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ์ „์— ์ž‘์„ฑํ–ˆ๋˜ Elasticsearch Helm Chart๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

// Node Selector
nodeSelector:
  cloud.google.com/gke-nodepool: ssd-node-pool
---
// NodeAffinity
affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
      	- key: cloud.google.com/gke-nodepool
     	  operator: In
      	  values:
      	  - ssd-node-pool

 

  • Taint / Toleration
    Taint๋Š” Pod๊ฐ€ ์Šค์ผ€์ค„๋ง ๋˜์ง€ ๋ชปํ•˜๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. AntiAffinity์™€ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ฐฉ์‹ ๋ฐ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Taints๋Š” ํŠน์ˆ˜ํ•œ ํ•˜๋“œ์›จ์–ด๋ฅผ ๊ฐ€์ง„ ์žฅ๋น„๋‚˜, ์ „์šฉ ๋…ธ๋“œ ๋ฆฌ์†Œ์Šค๋ฅผ ์„ ํƒํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ํŠน์ • ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง„ Pod๋งŒ ์Šค์ผ€์ค„๋งํ•˜๊ณ  ๊ทธ ์™ธ์˜ ๋‹ค๋ฅธ pod์€ ์Šค์ผ€์ค„๋ง ํ•˜์ง€ ์•Š์œผ๋ ค๋Š” ์ƒํ™ฉ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ Taint๋œ ๋…ธ๋“œ์— ์Šค์ผ€์ค„๋ง ํ•˜๋ ค๋ฉด Toleration์„ ํ†ตํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Taint ํƒ€์ž…์€ NoScehule, PreferNoSchedule, NoExecute, NodeCondition ํƒ€์ž…์ด ์กด์žฌํ•˜๋ฉฐ ๊ฐ ํƒ€์ž…์ด ์ˆ˜ํ–‰ํ•˜๋Š” ๊ธฐ๋Šฅ์€ ๋ฌธ์„œ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    Node์˜ Taint ์„ค์ •์€ kubectl taint nodes node1 key1=value1:NoSchedule์˜ CLI๋ฅผ ํ†ตํ•ด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ์ด ๋…ธ๋“œ์— ์Šค์ผ€์ค„๋ง ํ•˜๊ธฐ ์œ„ํ•œ toleration์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

tolerations:
- key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoSchedule"

NoSchedule๊ณผ NoExecute์˜ ์ฐจ์ด๋ฅผ ์กฐ๊ธˆ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด, NoSchedule ํƒ€์ž…์˜ ํ…Œ์ธํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜๋”๋ผ๋„ ๊ธฐ์กด์— ์Šค์ผ€์ค„๋ง ๋œ Pod์€ ๊ทธ๋Œ€๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ NoExecute ํƒ€์ž…์˜ ํ…Œ์ธํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ๊ธฐ์กด์— ์Šค์ผ€์ค„๋ง ๋œ Pod์€ ์ถ•์ถœ๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


Pod ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ

ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ฆฌ์†Œ์Šค ํ™œ์šฉ์„ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Pod์˜ ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ๋ฅผ ๊ณ ๋ คํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ์— ๋Œ€ํ•ด ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € Request์™€ Limit์— ๋Œ€ํ•ด ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Request๋Š” ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ์Šค์ผ€์ค„๋ง ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ฆฌ์†Œ์Šค์˜ ์–‘์œผ๋กœ CPU์™€ Memory๋ฅผ ํ†ตํ•ด ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ Request ๋งŒํผ์˜ ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค๋ฉด Pod์€ ์Šค์ผ€์ค„๋ง ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Limit์€ Pod์ด ์‹คํ–‰ ์ค‘์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฆฌ์†Œ์Šค ์–‘์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. Request์™€ ๋™์ผํ•˜๊ฒŒ CPU์™€ Memory๋ฅผ ํ†ตํ•ด ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 


OverCommitted

request์— ๋งž๊ฒŒ ๋ฆฌ์†Œ์Šค๋ฅผ ํ• ๋‹นํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑ, ์šด์˜ํ•˜๋˜ ๋„์ค‘ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋” ํ•„์š”ํ•˜๊ฒŒ ๋˜๋ฉด limit๊นŒ์ง€ ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ์˜ limit ์ดํ•ฉ์ด ์‹ค์ œ ์‹œ์Šคํ…œ์ด ๊ฐ€์šฉ๊ฐ€๋Šฅํ•œ reousrce์˜ ์–‘๋ณด๋‹ค ๋งŽ์•„์ง€๋Š” ํ˜„์ƒ (=Overcommitted)์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Overcommitted ์ƒํƒœ๊ฐ€ ๋˜์—ˆ์„ ๊ฒฝ์šฐ CPU์™€ Memory๋Š” ๊ฐ์ž ๋‹ค๋ฅธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • CPU Overcommitted
    ์‹ค์ œ CPU ์‚ฌ์šฉ๋Ÿ‰์„ request์— ์ •์˜๋œ ์ƒํƒœ๊นŒ์ง€ ๋‚ฎ์ถฅ๋‹ˆ๋‹ค(Throttling). ์ดํ›„์— Overcommitted ์ƒํƒœ๊ฐ€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์šฐ์„  ์ˆœ์œ„์— ๋”ฐ๋ผ ์šด์˜ ์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ•์ œ ์ข…๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค.

  • Memory Overcommitted
    ์‚ฌ์šฉ์ค‘์ธ ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ผ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„  ์ˆœ์œ„์— ๋”ฐ๋ผ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ฐ•์ œ ์ข…๋ฃŒ ์‹œํ‚ต๋‹ˆ๋‹ค. ์ดํ›„ ์ดˆ๊ธฐ request์— ๋งž๊ฒŒ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์žฌํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.


Qos Class

Pod์ด ์ƒ์„ฑ๋˜๋ฉด Guaranteed, Burstable, Best-Effort ์ค‘ ํ•˜๋‚˜์˜ QoS ํด๋ž˜์Šค๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. request์™€ limit์ด ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” Guranteed, limit > request์ธ ๊ฒฝ์šฐ์—๋Š” Burstable, request๋‚˜ limit์„ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด Best-Effort๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” Pod ์ž์›์ด ์—†๋Š” ๊ฒฝ์šฐ ์ถ•์ถœ๋˜๋Š” Pod์„ ์„ ํƒํ•  ๋•Œ ์šฐ์„ ์ˆœ์œ„์— ์˜ํ–ฅ์„ ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์„ธ๋ฐ€ํ•œ ์กฐ์ •์„ ํ•˜์ง€ ๋ชปํ•˜๋ฉด Overcommitted ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์œผ๋กœ Guaranteed ํด๋ž˜์Šค๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 

๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


PodDisruptionBudget

Pod์ด ์ถ•์ถœ๋˜๋Š” ์œ ํ˜•์€ 2๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋“œ์›จ์–ด ์žฅ์• , ๋ฆฌ๋กœ์Šค ๋ถ€์กฑ์œผ๋กœ ์ธํ•œ ๋น„์ž๋ฐœ์  ์ค‘๋‹จ๊ณผ ํด๋Ÿฌ์Šคํ„ฐ ์˜คํ† ์Šค์ผ€์ผ๋ง ํ•ด์ œ, PodTemplate ์—…๋ฐ์ดํŠธ๋กœ ์ธํ•œ ์ž๋ฐœ์  ์ถ•์ถœ์ž…๋‹ˆ๋‹ค. PodDisruptionBudget์€ Pod์ด ์ถ•์ถœ๋  ๋•Œ ์ตœ์†Œ ๊ฐ€์šฉ Pod๊ณผ ์ตœ๋Œ€ ๋ถˆ๊ฐ€์šฉ Pod์— ๋Œ€ํ•œ ์ •์ฑ…์„ ์„ค์ •ํ•จ์œผ๋กœ์จ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ€๋™ ์‹œ๊ฐ„์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋Š” server ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํ•ญ์ƒ ์ตœ์†Œ 5๊ฐœ๊ฐ€ ๊ฐ€์šฉํ•ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: server-pdb
spec:
  minAvailable: 5
  selector:
    matchLabels:
      app: server

ResourceQuota

๋‹จ์ผ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๊ฐ€ ๊ณต์œ ํ•  ๊ฒฝ์šฐ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ResourceQuota๋ฅผ ์„ค์ •ํ•ด ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋ถ„ํ• ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค๋Š” cpu, memory, storage, pvc, storageClass, service/deployment/replicaSet์˜ ๊ฐœ์ˆ˜ ๋“ฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ์‹œ๋Š” sphong์ด๋ผ๋Š” namespace์— cpu ๋ฐ memory, pvc ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: dev-rq
  namespace: sphong
spec:
  hard:
    request.cpu: "1"
    request.memory: 2Gi
    limits.cpu: "3"
    limits.memory: 2Gi
    persistentvolumeclaims: "5"

LimitRange

ResourceQuota๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ request์™€ limit์ด ๋ช…์‹œ๋˜์ง€ ์•Š์œผ๋ฉด ์Šค์ผ€์ค„๋ง์ด ๊ฑฐ์ ˆ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•ด ์ž˜ ์•Œ์ง€ ๋ชปํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฅผ ์„ค์ •ํ•˜๊ธฐ์— ์–ด๋ ค์›€์ด ์กด์žฌํ•˜๊ณ  ์‹ค์ˆ˜๋กœ ๋นผ๋จน์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ƒํ™ฉ์„ ์œ„ํ•˜ LimitRange๋ผ๋Š” Admission Controller๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์˜ˆ์‹œ๋Š” memory์— ๋Œ€ํ•œ defaultRequest๋ฅผ ์„ค์ •ํ•˜๋Š” LimitRange ์ž…๋‹ˆ๋‹ค.

apiVersion: v1
kind: LimitRange
metadata:
  name: sphong-lr
spec:
  limits:
  - default: 
      memory: 256Mi
    defaultRequest:
      memory: 128Mi
    type: Container

HPA

์›Œํฌ๋กœ๋“œ์˜ ์ฆ๊ฐ€๊ฐ€ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฝ์šฐ ์ˆ˜๋™ ํ™•์žฅ์„ ํ†ตํ•ด ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์ž๋™์œผ๋กœ ์›Œํฌ๋กœ๋“œ๋ฅผ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” HPA๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. HPA๋Š” addon์œผ๋กœ ์ œ๊ณต๋˜๋Š” metrics-server๋กœ ๋ถ€ํ„ฐ ๋ฉ”ํŠธ๋ฆญ์„ ์กฐํšŒํ•ด ์ˆ˜ํ‰ ํ™•์žฅ์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋ณธ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

  • horizontal-pod-autoscaler-sync-period(๋ฉ”ํŠธ๋ฆญ ๋™๊ธฐํ™” ์ฃผ๊ธฐ) : 30s

  • horizontal-pod-autoscaler-upscale-delay(ํ™•์žฅ ์ง€์—ฐ ์‹œ๊ฐ„) : 3m

  • horizontal-pod-autoscaler-downscale-delay(์ถ•์†Œ ์ง€์—ฐ ์‹œ๊ฐ„) : 5m

HPA๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ ๋ฌดํ•œ ํ™•์žฅ/์ถ•์†Œ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์†Œ/์ตœ๋Œ€ ํ™•์žฅ ๊ฐœ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•˜๋ฉฐ, ํ™•์žฅ์„ ์ง„ํ–‰ํ•  ๋ฆฌ์†Œ์Šค ๊ธฐ์ค€์„ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ CLI๋Š” cpu๋ฅผ 60% ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ตœ์†Œ 1๊ฐœ์—์„œ ์ตœ๋Œ€ 5๊ฐœ์˜ ๋ ˆํ”Œ๋ฆฌ์นด๋ฅผ ์ƒ์„ฑํ•˜๋Š” HPA๋ฅผ ์„ค์ •ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

> kubectl autoscale deployment server --cpu-percent=60 --min=1 --max=5

 

metrics-server์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”ํŠธ๋ฆญ ์ด์™ธ์—๋„ ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”ํŠธ๋ฆญ์„ ์‚ฌ์šฉํ•˜์—ฌ HPA๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” API Apapter๋ฅผ ํ†ตํ•ด custom metric์„ API๋กœ ๋…ธ์ถœ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์™€ ๊ด€๋ จ๋œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ alice_k106๋‹˜์˜ ๋ธ”๋กœ๊ทธ์—์„œ ์‰ฝ๊ฒŒ ์„ค๋ช…๋˜์–ด ์žˆ์–ด ๋งํฌ๋ฅผ ๋‚จ๊ฒจ๋‘๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€