Kubernetes HorizontalPodAutoscaler 가이드
Control Plane 설정
- kube-controller-manager
--horizontal-pod-autoscaler-sync-period 15s- metric을 업데이트하는 주기입니다.
--horizontal-pod-autoscaler-cpu-initialization-period 5m--horizontal-pod-autoscaler-initial-readiness-delay 30s--horizontal-pod-autoscaler-downscale-stabilization 5m--horizontal-pod-autoscaler-tolerance 0.1
HorizontalPodAutoscaler
Specification
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
spec:
scaleTargetRef:
name: <name>
maxReplicas: 10
metrics: []
scaleTargetRef- 같은 Namespace에 존재하는 리소스여야 합니다.
apiVersion: <apiVersion>kind: <kind>name: <name>
minReplicas: 1maxReplicas: <int>metrics: []type: Resource|ContainerResource|External|Object|Podsresourcename: cpu|memorytargettype: Utilization|Value|AverageValue
containerResourcecontainer: <containerName>name: cpu|memorytargettype: Utilization|Value|AverageValue
externalobjectpods
behavior- kubernetes/enhancements GitHub / Configurable scale up/down velocity for HPA
scaleUp|scaleDown- Default Behavior
stabilizationWindowSeconds: <seconds>- 주어진 시간 동안 제안된 desiredReplicas 중 가장 큰(down)/작은(up) 값을 적용합니다.
- 0 ~ 3600 설정 가능합니다.
tolerance: <ratio>- 기본값은
0.1입니다.
- 기본값은
selectPolicy: MaxMax: 정책 중 가장 높은 수치를 적용합니다.Min: 정책 중 가장 낮은 수치를 적용합니다.Disabled: 정책을 적용하지 않습니다.
policies: []type: Percent|PodsPercent: 최대currentReplicas * value / 100만큼 scale up/down 합니다.Pods: 최대value만큼 scale up/down 합니다.
value: <int>periodSeconds: <seconds>- 정책이 선택되고 다음 정책이 선택되기까지의 시간입니다.
- 1 ~ 1800 설정 가능합니다.
External metric
spec:
metrics:
- type: External
external:
metric:
name: <metricName>
selector:
matchLabels:
<key>: <value>
target:
type: Value|AverageValue
value: <quantity>
HPA는 External 사용 시 GET /apis/external.metrics.k8s.io/v1beta1/namespaces/<namespace>/<metricName>?labelSelector=<selector>를 호출하여 ExternalMetricValueList 리소스를 JSON 형태로 받길 기대합니다.
따라서 external metrics exporter는 아래와 같은 APIService를 통해 등록되어야 합니다.
apiVersion: apiregistration.k8s.io/v1
kind: APIService
spec:
service:
name: <serviceName>
namespace: <namespace>
port: 443
group: external.metrics.k8s.io
version: v1beta1
groupPriorityMinimum: 100
versionPriority: 100
Algorithm
desiredReplicas := currentReplicas
ratio := currentMetricValue / desiredMetricValue
if math.Abs(ratio-1) > tolerance {
desiredReplicas = int32(math.Ceil(float64(currentReplicas) * ratio))
}
desiredReplicas와 recommendations에서 upStabilizationWindowSeconds와 downStabilizationWindowSeconds를 사용하여 upRecommendation과 downRecommendation을 구하고, desiredReplicas를 recommendations에 추가합니다.
desiredReplicas = currentReplicas
if desiredReplicas < upRecommendation {
desiredReplicas = upRecommendation
}
if desiredReplicas > downRecommendation {
desiredReplicas = downRecommendation
}
정책을 적용합니다.
if desiredReplicas > currentReplicas {
desiredReplicas = upPolicy(desiredReplicas)
} else if desiredReplicas < currentReplicas {
desiredReplicas = downPolicy(desiredReplicas)
}
Default Behavior
spec:
behavior:
scaleUp:
selectPolicy: Max
policies:
- type: Pods
value: 4
periodSeconds: 60
- type: Percent
value: 100
periodSeconds: 60
stabilizationWindowSeconds: 0
scaleDown:
policies:
- type: Percent
value: 100
periodSeconds: 60
stabilizationWindowSeconds: 300