HPA

Horizontal Pod Autoscaler (HPA)๋Š” Kubernetes ํ™˜๊ฒฝ์—์„œ Pod์˜ ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ์กฐ์ ˆํ•˜์—ฌ ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ๋ฅ ์„ ์ตœ์ ํ™”ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ์ด๋ฅผ ์„ค๋ช…ํ•  ๋•Œ ์–ด๋–ค ๊ด€์ ์œผ๋กœ ์ ‘๊ทผํ• ์ง€๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ฐ ๊ด€์ ์—์„œ์˜ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด์ž.

1. Kubernetes ๊ด€์ ์—์„œ์˜ HPA

Kubernetes ๊ด€์ ์—์„œ HPA๋Š” ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ๋ฅ ์— ๊ธฐ๋ฐ˜ํ•ด Pod์˜ ์ˆ˜๋ฅผ ์กฐ์ ˆํ•˜๋Š” Kubernetes ๊ฐ์ฒด์ด๋‹ค. CPU ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋ฅ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ปค์Šคํ…€ ๋ฉ”ํŠธ๋ฆญ์„ ํ†ตํ•ด์„œ๋„ ์Šค์ผ€์ผ๋ง์„ ์ง€์›ํ•œ๋‹ค.

์ฃผ์š” ๊ธฐ๋Šฅ

  • ์ž๋™ ์Šค์ผ€์ผ๋ง: CPU, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋ฅ  ๋˜๋Š” ์ปค์Šคํ…€ ๋ฉ”ํŠธ๋ฆญ์„ ๊ธฐ๋ฐ˜์œผ๋กœ Pod ์ˆ˜๋ฅผ ์ž๋™์œผ๋กœ ์กฐ์ ˆ.

  • ์ตœ์†Œ/์ตœ๋Œ€ ํŒŒ๋“œ ์ˆ˜ ์„ค์ •: minReplicas์™€ maxReplicas๋กœ Pod ์ˆ˜์˜ ์ตœ์†Œ ๋ฐ ์ตœ๋Œ€์น˜๋ฅผ ์„ค์ •.

  • API ๋ฒ„์ „ ์ฐจ์ด:

    • autoscaling/v1: CPU ์‚ฌ์šฉ๋ฅ ๋งŒ ์ง€์›.

    • autoscaling/v2: ๋ฉ”๋ชจ๋ฆฌ ๋ฐ ์ปค์Šคํ…€ ๋ฉ”ํŠธ๋ฆญ์„ ์ง€์›.

์„ค์ • ์˜ˆ์‹œ

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  annotations:
    meta.helm.sh/release-name: staging
    meta.helm.sh/release-namespace: nestjs-boilerplate-config-variable
  creationTimestamp: "2024-05-09T01:57:58Z"
  labels:
    app: staging
    app.kubernetes.io/instance: staging
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: staging
    chart: auto-deploy-app-2.80.1
    helm.sh/chart: auto-deploy-app-2.80.1
    heritage: Helm
    release: staging
  name: staging-auto-deploy
  namespace: nestjs-boilerplate-config-variable
  resourceVersion: "39297159"
  uid: d5d40b25-453f-43f2-a3d3-35e5d174fdf9
spec:
  maxReplicas: 10
  metrics:
  - resource:
      name: cpu
      target:
        averageUtilization: 70
        type: Utilization
    type: Resource
  minReplicas: 3
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: staging
status:
  conditions:
  - lastTransitionTime: "2024-05-09T01:58:13Z"
    message: recommended size matches current size
    reason: ReadyForNewScale
    status: "True"
    type: AbleToScale
  - lastTransitionTime: "2024-05-09T01:58:29Z"
    message: the HPA was able to successfully calculate a replica count from cpu resource
      utilization (percentage of request)
    reason: ValidMetricFound
    status: "True"
    type: ScalingActive
  - lastTransitionTime: "2024-05-09T04:49:43Z"
    message: the desired replica count is less than the minimum replica count
    reason: TooFewReplicas
    status: "True"
    type: ScalingLimited
  currentMetrics:
  - resource:
      current:
        averageUtilization: 0
        averageValue: 1m
      name: cpu
    type: Resource
  currentReplicas: 3
  desiredReplicas: 3
  lastScaleTime: "2024-05-09T04:44:28Z"

๊ณต์‹ ๋ฌธ์„œ

2. Helm ๊ด€์ ์—์„œ์˜ HPA

Helm์„ ์‚ฌ์šฉํ•˜๋ฉด HPA ๊ฐ์ฒด๋ฅผ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. values.yaml ํŒŒ์ผ์„ ์‚ฌ์šฉํ•ด ์„ค์ •๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜ํ™”ํ•˜์—ฌ ๋ฐ˜๋ณต์ ์ธ ๋ฐฐํฌ์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์š” ๊ธฐ๋Šฅ

  • ํ…œํ”Œ๋ฆฟํ™”: HPA ์„ค์ •์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ถ”์ถœํ•˜์—ฌ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

  • ๋ฒ„์ „ ๊ด€๋ฆฌ: Helm ์ฐจํŠธ ๋ฒ„์ „์„ ํ†ตํ•ด ๋ฐฐํฌ๋œ HPA ๋ฒ„์ „์„ ๊ด€๋ฆฌ.

  • ์ž๋™ํ™” ๋ฐฐํฌ: GitLab CI/CD ํŒŒ์ดํ”„๋ผ์ธ ๋˜๋Š” ๋‹ค๋ฅธ CI/CD ๋„๊ตฌ๋ฅผ ํ†ตํ•œ ์ž๋™ํ™” ๋ฐฐํฌ.

์„ค์ • ์˜ˆ์‹œ

  • hpa.yaml:

{{- if and .Values.hpa.enabled .Values.resources.requests -}}
{{- if .Values.hpa.metrics }}
apiVersion: autoscaling/v2
{{- else }}
apiVersion: autoscaling/v1
{{- end }}
kind: HorizontalPodAutoscaler
metadata:
  name: {{ template "fullname" . }}
  labels:
{{ include "sharedlabels" . | indent 4 }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ template "appname" . }}
  minReplicas: {{ .Values.hpa.minReplicas }}
  maxReplicas: {{ .Values.hpa.maxReplicas }}
{{- if .Values.hpa.metrics }}
  metrics:
{{- toYaml .Values.hpa.metrics | nindent 2 }}
{{- else }}
  targetCPUUtilizationPercentage: {{ .Values.hpa.targetCPUUtilizationPercentage }}
{{- end }}
{{- end}}
  • values.yaml:

hpa:
  enabled: true
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 80

Last updated