请选择 进入手机版 | 继续访问电脑版
MSIPO技术圈 首页 IT技术 查看内容

k8s集群统计实际pod request、limit、实际资源使用率

2023-07-13

#!/bin/bash
echo "namespace,pod,container,request_cpu,limit_cpu,usage_cpu,request_mem,limit_mem,usage_mem" >> count.result
for ns in $(kubectl get ns -o jsonpath='{.items[*].metadata.name}'); do
    #for pod in $(kubectl get pod -n $ns -o jsonpath='{.items[*].metadata.name}'); do
    for pod in $(kubectl get pod -n $ns |grep Running|awk '{print $1}'); do
        for container in $(kubectl get pod $pod -n $ns -o jsonpath='{.spec.containers[*].name}'); do
            req_cpu=$(kubectl get pod $pod -n $ns -o jsonpath="{.spec.containers[?(@.name=='$container')].resources.requests.cpu}")
            req_mem=$(kubectl get pod $pod -n $ns -o jsonpath="{.spec.containers[?(@.name=='$container')].resources.requests.memory}")
            lim_cpu=$(kubectl get pod $pod -n $ns -o jsonpath="{.spec.containers[?(@.name=='$container')].resources.limits.cpu}")
            lim_mem=$(kubectl get pod $pod -n $ns -o jsonpath="{.spec.containers[?(@.name=='$container')].resources.limits.memory}")
            usage_cpu=$(kubectl top pod $pod -n $ns --containers=true --no-headers | awk '{print $3}' | awk '{s+=$1} END {print s}')
            usage_mem=$(kubectl top pod $pod -n $ns --containers=true --no-headers | awk '{print $4}' | awk '{s+=$1} END {print s}')
            if [[ "$req_cpu" != "" && "$req_mem" != "" && "$lim_cpu" != "" && "$lim_mem" != "" ]]; then
                echo "$ns,$pod,$container,$req_cpu,$lim_cpu,$usage_cpu,$req_mem,$lim_mem,$usage_mem" >> count.result
            fi
        done
    done
done

相关阅读

手机版|MSIPO技术圈 皖ICP备19022944号-2

Copyright © 2024, msipo.com

返回顶部