TIL

Horizontal Pod Autoscaler

동작 원리

메트릭 분류 데이터 출처 처리
per-Pod resource (CPU, memory) metrics.k8s.io utilization(요청 대비 %) 또는 raw 값. 모든 대상 Pod의 평균을 desired와 비교
per-Pod custom custom.metrics.k8s.io utilization 없이 raw 값만. 평균 후 비교
object / external custom.metrics.k8s.io / external.metrics.k8s.io 단일 값. autoscaling/v2에선 비교 전 Pod 수로 나눌 수 있음

Algorithm details

Pod readiness와 autoscaling metrics

옵션 기본값 역할
--horizontal-pod-autoscaler-cpu-initialization-period 5m Pod 시작 후 이 기간 동안 — Ready 상태이고 그 Ready 구간 동안 측정된 메트릭만 카운트. 그 외 CPU 사용량은 무시
--horizontal-pod-autoscaler-initial-readiness-delay 30s Pod 시작 후 이 기간 동안 — 현재 Unready인 Pod은 여전히 initializing으로 취급. 짧게 Ready로 전환됐다가 다시 Unready된 경우에도 무시

API object

메트릭 소스

타입 키 필드 용도
Resource resource Pod 전체의 CPU·memory 사용량
ContainerResource containerResource Pod 내 특정 컨테이너의 CPU·memory 사용량
Pods pods Pod별 custom 메트릭 (raw 값, 평균 비교)
Object object K8s 객체 단위 메트릭 (예: Ingress의 RPS)
External external 클러스터 외부 시스템 메트릭 (예: 클라우드 큐 길이)

Resource metrics

Container resource metrics

Custom metrics

Multiple metrics

Metrics APIs

API 메트릭 종류 제공 주체
metrics.k8s.io resource (CPU·memory) metrics-server 애드온 (별도 설치 필요)
custom.metrics.k8s.io custom 벤더의 adapter API 서버 (예: Prometheus Adapter)
external.metrics.k8s.io external (클러스터 외부) 위 adapter들이 함께 제공하기도 함

Workload scale 안정성 (flapping)

Rolling update 중 HPA

Configurable scaling behavior

Scaling policies

Stabilization window

Tolerance

Default behavior

방향 stabilization 정책 selectPolicy
scaleDown 300s Percent 100% / 15s (최소 replica까지 한 번에 가능) Max
scaleUp 0s (즉시) Percent 100% / 15s + Pods 4 / 15s Max

자주 쓰는 패턴

kubectl 지원

Implicit maintenance-mode

Deployment / StatefulSet을 HPA로 마이그레이션

적용 방식 절차
Client-Side Apply (기본) kubectl apply edit-last-applied deployment/<name>로 last-applied annotation에서 spec.replicas 제거 → 매니페스트에서도 제거 → 이후 정상 apply
Server-Side Apply transferring ownership 가이드를 따라 replicas 필드의 소유권을 HPA로 이전