kubectl top

查看节点和 Pod 的 CPU、内存实时使用情况

语法

kubectl top <node|pod> [name] [flags]

参数

参数说明示例级别
--containers 显示 Pod 内各容器的资源使用 kubectl top pod web-pod --containers 常用
--sort-by 按 cpu 或 memory 排序 kubectl top pods --sort-by=memory 常用
-l --selector 按标签筛选 kubectl top pods -l app=web 常用
-A --all-namespaces 查看所有命名空间 kubectl top pods -A --sort-by=cpu 常用

示例

查看节点资源使用

kubectl top nodes
需要集群安装 Metrics Server

查看 Pod 资源使用

kubectl top pods --sort-by=memory
按内存使用量排序,快速找到内存大户

查看容器级别资源

kubectl top pod web-app --containers
多容器 Pod 查看各容器占用

全集群 CPU 排行

kubectl top pods -A --sort-by=cpu
找出 CPU 使用最高的 Pod

常见错误

Metrics API not available 集群未安装 Metrics Server,需要先部署:kubectl apply -f metrics-server.yaml
metrics not available yet Metrics Server 刚部署,等待 1-2 分钟收集数据

技巧

相关命令