# 获取 CPU 利用率失败：无法获取资源 cpu 的指标：无法从资源指标 API 获取指标：服务器找不到请求的资源（get pods.metrics.k8s.io）

- **ID:** `kubernetes/horizontal-pod-autoscaler-unable-to-get-metrics`
- **领域:** kubernetes
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

Metrics Server 未部署或未正常运行，导致 Horizontal Pod Autoscaler 无法获取 CPU/内存指标。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Metrics Server v0.6.4 | active | — | — |
| Kubernetes v1.28.0 | active | — | — |
| Kubernetes v1.29.0 | active | — | — |

## 解决方案

1. ```
   部署 Metrics Server：`kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml`。
   ```
2. ```
   如果 Metrics Server 已安装但无法工作，检查其日志：`kubectl logs -n kube-system deployment/metrics-server`。常见问题包括 TLS 或节点名称解析问题。
   ```
3. ```
   对于 Minikube 或 kind 集群，启用 metrics-server 插件：`minikube addons enable metrics-server` 或使用 kind 的配置部署它。
   ```

## 无效尝试

- **Increasing HPA target thresholds or replica limits** — HPA still needs metrics to function; changing thresholds won't fix the missing metrics source. (95% 失败率)
- **Restarting the HPA controller manager** — The HPA controller is healthy; the issue is the metrics API backend, not the controller. (80% 失败率)
