You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行kubectl top pod遇invalid resource name错误,无法获取Pod资源利用率

无法获取Kubernetes Pod资源利用率的问题

问题现象

执行kubectl get pod能看到正常运行的Pod:

$ kubectl get pod
NAME                               READY   STATUS    RESTARTS      AGE
spring-boot-k8s-556b578645-pwdhs   1/1     Running   3 (49m ago)   31d

但尝试获取该Pod资源利用率时出现多种错误:

$ kubectl top pod pod/spring-boot-k8s -n efault --containers
error: invalid resource name "pod/spring-boot-k8s": [may not contain '/']

$ kubectl top pod spring-boot-k8s -n efault --containers
Error from server (NotFound): pod "spring-boot-k8s" not found

$ kubectl top pod spring-boot-k8s-556b578645-pwdhs -n efault --containers
Error from server (NotFound): pod "spring-boot-k8s-556b578645-pwdhs" not found

$ kubectl top pod pod/spring-boot-k8s-556b578645-pwdhs -n efault --containers
error: invalid resource name "pod/spring-boot-k8s-556b578645-pwdhs": [may not contain '/']

Metrics Server已正常运行:

$ kubectl get all -n kube-system | grep metric
pod/metrics-server-6b76bd68b6-f68f4    1/1     Running   68 (54m ago)   38d
service/metrics-server   ClusterIP   10.103.98.14   <none>        443/TCP                  38d
deployment.apps/metrics-server   1/1     1            1           38d
replicaset.apps/metrics-server-6b76bd68b6   1         1         1       38d

能正常查看节点资源利用率:

$ kubectl top node
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
minikube   132m         1%     734Mi           4%   

但执行kubectl top pod时提示:

$ kubectl top pod
error: Metrics not available for pod default/spring-boot-k8s-556b578645-pwdhs, age: 747h9m12.416340419s

解决方法

1. 修正命名空间拼写错误

你所有kubectl top pod命令里的命名空间参数写成了-n efault,正确的默认命名空间是default,这是导致NotFound错误的直接原因。正确的命令应该是:

kubectl top pod spring-boot-k8s-556b578645-pwdhs -n default --containers

如果Pod确实在默认命名空间,也可以省略-n default参数:

kubectl top pod spring-boot-k8s-556b578645-pwdhs --containers

2. 排查Metrics Server采集问题

如果修正命名空间后仍然提示Metrics not available,按以下步骤检查:

  • 查看Metrics Server日志:检查是否有采集Pod指标的报错:
    kubectl logs -n kube-system pod/metrics-server-6b76bd68b6-f68f4
    
    常见问题包括证书配置错误、节点访问权限不足,或者版本与Kubernetes集群不兼容。
  • 验证kubelet的cadvisor接口:Metrics Server依赖kubelet的cadvisor接口采集Pod指标,确保kubelet正常运行,接口未被禁用:
    curl -k https://localhost:10250/metrics/cadvisor
    
  • 重启Metrics Server:如果日志显示临时采集错误,重启Deployment:
    kubectl rollout restart deployment metrics-server -n kube-system
    

3. 重启异常Pod

你的Pod已经运行了31天,长时间运行的Pod可能出现指标采集异常。尝试重启Pod:

kubectl delete pod spring-boot-k8s-556b578645-pwdhs

Deployment会自动重建新Pod,之后再执行kubectl top pod查看指标。


内容的提问来源于stack exchange,提问作者MsA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 23:47:10