kubectl-cost 集群成本查询
kubectl-cost 是 kubectl 插件,通过 OpenCost/Kubecost API 查询命名空间、Deployment、Pod 和节点的成本。它把资源请求、使用量和价格关联起来,适合做成本分摊和优化。
核心概念:cluster cost、namespace cost、workload cost、idle cost 和 efficiency。成本数据来自 Prometheus 和云厂商价格。
安装 Krew 和插件:
kubectl krew install cost
kubectl cost version
查看集群总成本:
kubectl cost cluster
按命名空间查看:
kubectl cost namespace
kubectl cost namespace --window 7d
查看 Deployment 成本:
kubectl cost deployment -n demo
kubectl cost deployment demo -n demo --window 30d
查看 Pod 成本:
kubectl cost pod -n demo
查看节点成本:
kubectl cost node
输出效率和闲置成本:
kubectl cost namespace --show-efficiency
kubectl cost cluster --show-idle
指定 OpenCost 端点:
kubectl cost cluster --opencost-url http://opencost.opencost:9003
--window 的时间范围和 Prometheus 保留期要匹配。kubectl cost — 查询成本kubectl get pods -n opencost — 查看 OpenCostkubectl port-forward — 访问 OpenCost UIkubectl top — 查看资源使用promtool — 校验成本指标查询