我正在阅读kubernetes文档中的HPA演练。当使用平均值而不是利用率时,我无法让HPA扩展部署。
我使用的是1.25 minikube集群,并且已经部署了度量服务器并打了补丁。
kubectl patch deployment metrics-server -n kube-system --type 'json' -p '[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--kubelet-insecure-tls"}]'
我能够成功执行Kubectl top pods/nodes命令成功,这对我来说意味着度量服务器工作正常。
Gauravs-MBP:K8s alieninvader$ kubectl top pods
NAME CPU(cores) MEMORY(bytes)
load-generator 6m 0Mi
php-apache-5b56f9df94-r74qk 501m 11Mi
Gauravs-MBP:K8s alieninvader$ kubectl top nodes
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
minikube 719m 11% 834Mi 10%
当我使用Utilization而不是AverageValue时,HPA能够扩展部署。我使用了相同的php-apache部署,并使用它作为我的清单来创建HPA。
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
namespace: default
spec:
maxReplicas: 10
metrics:
- resource:
name: cpu
target:
averageValue: 50
type: AverageValue
type: Resource
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
当我查看HPA的事件时,我看到HPA仍然将其计算为在推荐范围内。
kubectl describe hpa php-apache
Warning: autoscaling/v2beta2 HorizontalPodAutoscaler is deprecated in v1.23+, unavailable in v1.26+; use autoscaling/v2 HorizontalPodAutoscaler
Name: php-apache
Namespace: default
Labels: <none>
Annotations: <none>
CreationTimestamp: Tue, 20 Dec 2022 13:43:19 -0500
Reference: Deployment/php-apache
Metrics: ( current / target )
resource cpu on pods: 501m / 50
Min replicas: 1
Max replicas: 10
Deployment pods: 1 current / 1 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 cpu resource
ScalingLimited False DesiredWithinRange the desired count is within the acceptable range
Events: <none>
Gauravs-MBP:K8s alieninvader$ kubectl get hpa -w
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 1m/50 1 10 1 37s
php-apache Deployment/php-apache 189m/50 1 10 1 45s
php-apache Deployment/php-apache 501m/50 1 10 1 60s
php-apache Deployment/php-apache 502m/50 1 10 1 90s
php-apache Deployment/php-apache 500m/50 1 10 1 105s
php-apache Deployment/php-apache 501m/50 1 10 1 2m
php-apache Deployment/php-apache 500m/50 1 10 1 2m15s
php-apache Deployment/php-apache 501m/50 1 10 1 2m30s
php-apache Deployment/php-apache 499m/50 1 10 1 2m45s
php-apache Deployment/php-apache 502m/50 1 10 1 3m
php-apache Deployment/php-apache 501m/50 1 10 1 3m15s
php-apache Deployment/php-apache 500m/50 1 10 1 3m30s
php-apache Deployment/php-apache 501m/50 1 10 1 3m45s
php-apache Deployment/php-apache 500m/50 1 10 1 4m
php-apache Deployment/php-apache 502m/50 1 10 1 4m15s
php-apache Deployment/php-apache 500m/50 1 10 1 4m30s
php-apache Deployment/php-apache 501m/50 1 10 1 4m45s
php-apache Deployment/php-apache 500m/50 1 10 1 5m
php-apache Deployment/php-apache 501m/50 1 10 1 5m15s
php-apache Deployment/php-apache 500m/50 1 10 1 5m30s
php-apache Deployment/php-apache 501m/50 1 10 1 5m45s
php-apache Deployment/php-apache 500m/50 1 10 1 6m15s
php-apache Deployment/php-apache 502m/50 1 10 1 6m30s
php-apache Deployment/php-apache 500m/50 1 10 1 6m45s
那么,为什么当使用AverageValue时,HPA认为这是在范围内,而kubectl中显示的CPU百分比显然要高得多。我错过了什么?
与averageUtilization
相反,averageValue
应该是一个文字值,而不是百分比(参见MetricTarget)。在您的设置中,它被设置为50核,这显然比pod上的负载高得多。你是说500米吗?