Kubernetes HPA 如何评估内存指标
How memory metric is evaluated by Kubernetes HPA
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: backend-hpa
spec:
maxReplicas: 10
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: backend
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 1
periodSeconds: 100
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Pods
value: 2
periodSeconds: 35
我对如何计算内存利用率感到困惑? 70%是基于什么阈值?
➜ kd hpa
Name: prod-backend-hpa-v1
Namespace: prod
Labels: argocd.argoproj.io/instance=ccl-backend-prod
Annotations: <none>
CreationTimestamp: Mon, 13 Sep 2021 17:39:44 -0700
Reference: Deployment/prod-backend-v1
Metrics: ( current / target )
resource memory on pods (as a percentage of request): 31% (85408426666m) / 70%
resource cpu on pods (as a percentage of request): 0% (1m) / 70%
Min replicas: 3
Max replicas: 10
Behavior:
Scale Up:
Stabilization Window: 60 seconds
Select Policy: Max
Policies:
- Type: Pods Value: 2 Period: 35 seconds
Scale Down:
Stabilization Window: 300 seconds
Select Policy: Max
Policies:
- Type: Pods Value: 1 Period: 100 seconds
Deployment pods: 3 current / 3 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True ReadyForNewScale recommended size matches current size
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from memory resource utilization (percentage of request)
ScalingLimited True TooFewReplicas the desired replica count is less than the minimum replica count
Events: <none>
这怎么样
resource memory on pods (as a percentage of request): 31% (85408426666m) / 70%
计算?
我的部署具有以下requests/limits设置
Containers:
backend:
Port: 8080/TCP
Host Port: 0/TCP
Command:
web
Limits:
cpu: 1
memory: 1Gi
Requests:
cpu: 200m
memory: 256Mi
我现在的pods
➜ k top po
W0914 13:38:13.793286 3006942 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag
NAME CPU(cores) MEMORY(bytes)
prod-backend-v1-7858bddc4-5t8r5 1m 80Mi
prod-backend-v1-7858bddc4-67llh 1m 81Mi
prod-backend-v1-7858bddc4-98wj2 1m 82Mi
所以好像31%的计算方式是81/256 ~31%.
但是这样做正确吗?
我认为 81/LIMIT = 81/1024 = ~8%.
在逻辑上是正确的
类型=利用率&& averageUtilization:70
是所有相关 pods 的资源指标平均值的目标值,表示为 资源请求值 的百分比 pods。目前仅对 Resource metric source type
有效
type=AverageValue && averageValue: 500Mi
averageValue 是所有相关 pods 指标平均值的目标值(作为数量)
所以我的 HPA 内存指标变成了:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: backend-hpa
spec:
maxReplicas: 10
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: backend
metrics:
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 500Mi # averageValue is the target value of the average of the metric across all relevant pods (as a quantity), https://www.pulumi.com/docs/reference/pkg/kubernetes/autoscaling/v2beta2/horizontalpodautoscalerlist/#metrictarget
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # represented as a percentage of the requested value of the resource for the pods.
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 1
periodSeconds: 100
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Pods
value: 2
periodSeconds: 35
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: backend-hpa
spec:
maxReplicas: 10
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: backend
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 1
periodSeconds: 100
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Pods
value: 2
periodSeconds: 35
我对如何计算内存利用率感到困惑? 70%是基于什么阈值?
➜ kd hpa
Name: prod-backend-hpa-v1
Namespace: prod
Labels: argocd.argoproj.io/instance=ccl-backend-prod
Annotations: <none>
CreationTimestamp: Mon, 13 Sep 2021 17:39:44 -0700
Reference: Deployment/prod-backend-v1
Metrics: ( current / target )
resource memory on pods (as a percentage of request): 31% (85408426666m) / 70%
resource cpu on pods (as a percentage of request): 0% (1m) / 70%
Min replicas: 3
Max replicas: 10
Behavior:
Scale Up:
Stabilization Window: 60 seconds
Select Policy: Max
Policies:
- Type: Pods Value: 2 Period: 35 seconds
Scale Down:
Stabilization Window: 300 seconds
Select Policy: Max
Policies:
- Type: Pods Value: 1 Period: 100 seconds
Deployment pods: 3 current / 3 desired
Conditions:
Type Status Reason Message
---- ------ ------ -------
AbleToScale True ReadyForNewScale recommended size matches current size
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from memory resource utilization (percentage of request)
ScalingLimited True TooFewReplicas the desired replica count is less than the minimum replica count
Events: <none>
这怎么样
resource memory on pods (as a percentage of request): 31% (85408426666m) / 70%
计算?
我的部署具有以下requests/limits设置
Containers:
backend:
Port: 8080/TCP
Host Port: 0/TCP
Command:
web
Limits:
cpu: 1
memory: 1Gi
Requests:
cpu: 200m
memory: 256Mi
我现在的pods
➜ k top po
W0914 13:38:13.793286 3006942 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag
NAME CPU(cores) MEMORY(bytes)
prod-backend-v1-7858bddc4-5t8r5 1m 80Mi
prod-backend-v1-7858bddc4-67llh 1m 81Mi
prod-backend-v1-7858bddc4-98wj2 1m 82Mi
所以好像31%的计算方式是81/256 ~31%.
但是这样做正确吗?
我认为 81/LIMIT = 81/1024 = ~8%.
类型=利用率&& averageUtilization:70
是所有相关 pods 的资源指标平均值的目标值,表示为 资源请求值 的百分比 pods。目前仅对 Resource metric source type
有效type=AverageValue && averageValue: 500Mi
averageValue 是所有相关 pods 指标平均值的目标值(作为数量)
所以我的 HPA 内存指标变成了:
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: backend-hpa
spec:
maxReplicas: 10
minReplicas: 3
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: backend
metrics:
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 500Mi # averageValue is the target value of the average of the metric across all relevant pods (as a quantity), https://www.pulumi.com/docs/reference/pkg/kubernetes/autoscaling/v2beta2/horizontalpodautoscalerlist/#metrictarget
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # represented as a percentage of the requested value of the resource for the pods.
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 1
periodSeconds: 100
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Pods
value: 2
periodSeconds: 35