kubectl top
命令可以很方便地查看 k8s 集群中部署應(yīng)用的實際資源使用情況。
使用 kubectl top
命令需要先部署 metrics 組件,metrics 的部署請參考下面文章:
helm部署metrics-server文章來源地址http://www.zghlxwxcb.cn/news/detail-511622.html
- 查看指定命名空間資源使用情況
# 統(tǒng)計該命令空間的 pod 數(shù)量
[root@master1 ~]# kubectl get pod -n sentry |wc -l
37
# 查看每個 pod 的資源使用(CPU/內(nèi)存)
[root@master1 ~]# kubectl top pod -n sentry
W0223 17:52:34.971648 61956 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag
NAME CPU(cores) MEMORY(bytes)
sentry-clickhouse-0 2m 226Mi
sentry-clickhouse-1 7m 281Mi
sentry-clickhouse-2 2m 245Mi
sentry-cron-56cbcf6976-mcm74 2m 142Mi
sentry-kafka-0 3m 404Mi
sentry-kafka-1 4m 397Mi
...
# 統(tǒng)計該命令空間所有 pod 的 CPU 使用(單位:m)
[root@master1 ~]# kubectl top pod -n sentry --use-protocol-buffers |tail -n +2 |awk '{print $2}' |cut -dm -f1 |awk '{sum+=$1}END{print sum}'
260
# 統(tǒng)計該命令空間所有 pod 的內(nèi)存使用(單位:Mi)
[root@master1 ~]# kubectl top pod -n sentry --use-protocol-buffers |tail -n +2 |awk '{print $3}' |cut -dm -f1 |awk '{sum+=$1}END{print sum}'
8861
- 查看所有命名空間的資源使用情況
# 查看每個 pod 的資源使用(CPU/內(nèi)存)
[root@master1 ~]# kubectl top pod -A
W0223 17:54:23.758459 65143 top_pod.go:140] Using json format to get metrics. Next release will switch to protocol-buffers, switch early by passing --use-protocol-buffers flag
NAMESPACE NAME CPU(cores) MEMORY(bytes)
cattle-system cattle-cluster-agent-648c79b5cd-ddqq5 11m 220Mi
cattle-system cattle-cluster-agent-648c79b5cd-rkvzz 15m 296Mi
default apisix-85cdf966dc-52b5x 15m 275Mi
default apisix-dashboard-564b455b7-pr9wb 1m 18Mi
default apisix-etcd-0 293m 85Mi
default busybox 0m 0Mi
...
# 統(tǒng)計集群所有 pod 數(shù)量
[root@master1 ~]# kubectl get pod -A |wc -l
257
# 統(tǒng)計集群所有 pod 的 CPU 使用(單位:m)
[root@master1 ~]# kubectl top pod -A --use-protocol-buffers |tail -n +2 |awk '{print $3}' |cut -dm -f1 |awk '{sum+=$1}END{print sum}'
4827
# 統(tǒng)計集群所有 pod 的內(nèi)存使用(單位:Mi)
[root@master1 ~]# kubectl top pod -A --use-protocol-buffers |tail -n +2 |awk '{print $4}' |cut -dm -f1 |awk '{sum+=$1}END{print sum}'
53608
文章來源:http://www.zghlxwxcb.cn/news/detail-511622.html
到了這里,關(guān)于統(tǒng)計k8s集群的資源使用情況的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!