无法获取 CPU 利用率:无法获取资源 CPU 的指标:无法从资源指标 API 获取指标:服务器找不到请求的资源(get pods.metrics.k8s.io)
failed to get cpu utilization: unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)
ID: kubernetes/horizontal-pod-autoscaler-missing-metrics
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| kubernetes 1.27 | active | — | — | — |
| metrics-server 0.6 | active | — | — | — |
| metrics-server 0.7 | active | — | — | — |
根因分析
metrics-server(或自定义指标适配器)未安装或未正常运行,导致 Horizontal Pod Autoscaler 无法检索 CPU/内存指标。
English
The metrics-server (or custom metrics adapter) is not installed or not functioning, so the Horizontal Pod Autoscaler cannot retrieve CPU/memory metrics.
官方文档
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-metrics-apis解决方案
-
使用 `kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml` 部署 metrics-server。验证其运行:`kubectl get pods -n kube-system | grep metrics-server`。检查日志错误:`kubectl logs -n kube-system deployment/metrics-server`。
-
运行 `kubectl get apiservices.apiregistration.k8s.io | grep metrics` 查看 `v1beta1.metrics.k8s.io` 或 `v1beta1.custom.metrics.k8s.io` 是否可用。如果不可用,确保 metrics-server 部署健康且其服务端点可达。
无效尝试
常见但无效的做法:
-
95% 失败
Changing the target doesn't fix the missing metrics API; the HPA still can't get any data.
-
90% 失败
The HPA controller is not the source of metrics; it relies on the metrics API which is unavailable.
-
50% 失败
Conflicting adapters can cause API registration issues; the correct approach is to ensure only one metrics provider is active.