首先,确保在Kubernetes集群中启用了指标服务器(metrics-server)。
如果启用了指标服务器,则需要确定指标服务器是否可以访问Pod。可以尝试运行以下命令,查看指标服务器是否返回Pod的CPU利用率。
kubectl get --raw "/apis/metrics.k8s.io/v1beta1/pods"
如果返回正确的输出,则指标服务器可以访问Pod,请继续下一步。
然后,确保Pod有足够的资源来运行,并且有足够的CPU资源可供水平Pod自动缩放器使用。
接下来,检查您的HPA定义是否正确。确保有正确的参数,例如目标利用率(targetCPUUtilizationPercentage),Pod的最小和最大副本数等。
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: example-hpa
spec:
maxReplicas: 10
minReplicas: 2
targetCPUUtilizationPercentage: 50
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: example-deployment
最后,如果仍然无法获取CPU利用率,请检查Pod的标签是否与HPA定义中的标签匹配。只有匹配的标签才会被考虑。
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
selector:
matchLabels:
app: example-app
...
通过上述步骤,您应该能够成功获取CPU利用率,并让水平Pod自动缩放器正常工作。