HorizontalPodAutoscaler) + control plane의 controller로 구현--horizontal-pod-autoscaler-sync-period (default 15s)scaleTargetRef로 대상 워크로드 찾기.spec.selector로 Pod 선택scale subresource를 통해 replica 갱신| 메트릭 분류 | 데이터 출처 | 처리 |
|---|---|---|
| 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 수로 나눌 수 있음 |
desiredReplicas = ceil(currentReplicas × currentMetricValue / desiredMetricValue)200m, desired 100m → ratio 2.0 → replicas 2배50m, desired 100m → ratio 0.5 → replicas 절반0.1 = 10%)targetAverageValue / targetAverageUtilization — 모든 대상 Pod의 평균을 currentMetricValue로 사용--horizontal-pod-autoscaler-downscale-stabilization (default 5분)| 옵션 | 기본값 | 역할 |
|---|---|---|
--horizontal-pod-autoscaler-cpu-initialization-period |
5m |
Pod 시작 후 이 기간 동안 — Ready 상태이고 그 Ready 구간 동안 측정된 메트릭만 카운트. 그 외 CPU 사용량은 무시 |
--horizontal-pod-autoscaler-initial-readiness-delay |
30s |
Pod 시작 후 이 기간 동안 — 현재 Unready인 Pod은 여전히 initializing으로 취급. 짧게 Ready로 전환됐다가 다시 Unready된 경우에도 무시 |
startupProbe — CPU spike가 지나기 전까지 통과하지 않게 구성readinessProbe의 initialDelaySeconds를 CPU spike 이후로 맞춤--horizontal-pod-autoscaler-cpu-initialization-period를 startup 시간만큼 늘림autoscalingautoscaling/v2 (memory·custom metrics 지원)autoscaling/v2에서 도입된 새 필드는 autoscaling/v1로 작업할 때 annotation으로 보존metrics 필드에 메트릭 소스 타입을 명시 — 같은 HPA에 여러 개 함께 지정 가능| 타입 | 키 필드 | 용도 |
|---|---|---|
Resource |
resource |
Pod 전체의 CPU·memory 사용량 |
ContainerResource |
containerResource |
Pod 내 특정 컨테이너의 CPU·memory 사용량 |
Pods |
pods |
Pod별 custom 메트릭 (raw 값, 평균 비교) |
Object |
object |
K8s 객체 단위 메트릭 (예: Ingress의 RPS) |
External |
external |
클러스터 외부 시스템 메트릭 (예: 클라우드 큐 길이) |
type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60 # 평균 사용률을 60%로 유지
current usage / requested resources 비율type: ContainerResource
containerResource:
name: cpu
container: application
target:
type: Utilization
averageUtilization: 60
autoscaling/v2부터 안정화. K8s 빌트인이 아닌 메트릭으로 스케일custom.metrics.k8s.io API를 통해 조회 — adapter 필요autoscaling/v2에서 여러 메트릭 동시 지정 가능maxReplicas 한도 내)| API | 메트릭 종류 | 제공 주체 |
|---|---|---|
metrics.k8s.io |
resource (CPU·memory) | metrics-server 애드온 (별도 설치 필요) |
custom.metrics.k8s.io |
custom | 벤더의 adapter API 서버 (예: Prometheus Adapter) |
external.metrics.k8s.io |
external (클러스터 외부) | 위 adapter들이 함께 제공하기도 함 |
behavior 필드의 stabilization window·tolerance·scaling policy 참고 (다음 세션)replicas 필드만 조정. Deployment controller가 롤링 중 신·구 ReplicaSet의 replica 분배를 알아서 처리autoscaling/v2의 behavior 필드 — scaleUp, scaleDown을 각 방향별로 따로 구성behavior.scaleUp.policies / behavior.scaleDown.policies 아래 1개 이상 지정Pods(절대 개수) 또는 Percent(현재 replica의 %)periodSeconds — 정책이 적용되는 시간 창. 최대 1800초(30분)여러 정책이 있으면 — default로 selectPolicy: Max → 가장 큰 변화를 허용하는 정책 선택
behavior:
scaleDown:
policies:
- type: Pods
value: 4 # 분당 최대 4 Pod 감소
periodSeconds: 60
- type: Percent
value: 10 # 분당 최대 10% 감소
periodSeconds: 60
Percent가 더 큰 변화 → 10% 감소 적용. 40 이하면 Pods 정책의 4가 더 큼 → 4 감소selectPolicy 값 — Max(기본) / Min(가장 작은 변화) / Disabled(해당 방향 스케일 완전 중단)scale-down 시 — 윈도우 내 추천값들 중 최댓값을 채택 (rolling max). Pod을 줄이자마자 다시 만드는 패턴을 막음
behavior:
scaleDown:
stabilizationWindowSeconds: 300 # 과거 5분치 desired state 고려
tolerance — 이 값 이하의 메트릭 편차에는 스케일링하지 않음예시 — memory target 100MiB, scale-up tolerance 5% → 105MiB 초과여야 scale up
behavior:
scaleUp:
tolerance: 0.05 # 5%
10% — --horizontal-pod-autoscaler-tolerance CLI 옵션으로만 변경 가능 (API로는 변경 불가)behavior에 일부만 지정하면 — 미지정 필드는 default와 merge됨| 방향 | stabilization | 정책 | selectPolicy |
|---|---|---|---|
scaleDown |
300s |
Percent 100% / 15s (최소 replica까지 한 번에 가능) |
Max |
scaleUp |
0s (즉시) |
Percent 100% / 15s + Pods 4 / 15s |
Max |
behavior:
scaleDown:
stabilizationWindowSeconds: 60
behavior:
scaleDown:
policies:
- type: Percent
value: 10
periodSeconds: 60
- type: Pods
value: 5
periodSeconds: 60
selectPolicy: Min # 더 작은 변화 선택
behavior:
scaleDown:
selectPolicy: Disabled
kubectl create / get hpa / describe hpa / delete hpakubectl autoscale 단축 명령
kubectl autoscale rs foo --min=2 --max=5 --cpu=80%
# ReplicaSet foo에 대해 CPU 80% target, replica 2~5인 HPA 생성
0 AND HPA의 minReplicas > 0ScalingActive: false 기록minReplicas 중 하나를 수동으로 조정spec.replicas 제거 권장kubectl apply -f deployment.yaml 할 때마다 매니페스트의 replicas 값으로 되돌림 → HPA 결정과 충돌하며 flappingspec.replicas의 default가 1이라 한 번 1로 떨어졌다 다시 올라갈 수 있음 (Pod count 일시 저하)| 적용 방식 | 절차 |
|---|---|
| Client-Side Apply (기본) | kubectl apply edit-last-applied deployment/<name>로 last-applied annotation에서 spec.replicas 제거 → 매니페스트에서도 제거 → 이후 정상 apply |
| Server-Side Apply | transferring ownership 가이드를 따라 replicas 필드의 소유권을 HPA로 이전 |