#!/bin/bash
# 创建一个包含标题行的 CSV 文件
echo "资源类型,名称,CPU 使用量,内存使用量,CPU 限制,内存限制" > k8s_resources.csv
# 获取所有 StatefulSet 的资源使用情况和限制
statefulsets=$(kubectl get statefulset --all-namespaces -o json)
for namespace in $(echo "$statefulsets" | jq -r '.items[].metadata.namespace' | uniq); do
echo "正在处理 StatefulSet,命名空间:$namespace"
sts_names=$(echo "$statefulsets" | jq -r ".items[] | select(.metadata.namespace==\"$namespace\") | .metadata.name")
for sts_name in $sts_names; do
echo "正在处理 StatefulSet:$sts_name"
cpu_usage=$(kubectl describe statefulset "$sts_name" -n "$namespace" | grep "cpu:" | awk '{print $2}')
memory_usage=$(kubectl describe statefulset "$sts_name" -n "$namespace" | grep "memory:" | awk '{print $2}')
cpu_limit=$(kubectl get statefulset "$sts_name" -n "$namespace" -ojsonpath='{.spec.template.spec.containers[*].resources.limits.cpu}')
memory_limit=$(kubectl get statefulset "$sts_name" -n "$namespace" -ojsonpath='{.spec.template.spec.containers[*].resources.limits.memory}')
echo "StatefulSet,$sts_name,$cpu_usage,$memory_usage,$cpu_limit,$memory_limit" >> k8s_resources.csv
done
done
# 获取所有 Deployment 的资源使用情况和限制
deployments=$(kubectl get deployment --all-namespaces -o json)
for namespace in $(echo "$deployments" | jq -r '.items[].metadata.namespace' | uniq); do
echo "正在处理 Deployment,命名空间:$namespace"
deploy_names=$(echo "$deployments" | jq -r ".items[] | select(.metadata.namespace==\"$namespace\") | .metadata.name")
for deploy_name in $deploy_names; do
echo "正在处理 Deployment:$deploy_name"
cpu_usage=$(kubectl describe deployment "$deploy_name" -n "$namespace" | grep "cpu:" | awk '{print $2}')
memory_usage=$(kubectl describe deployment "$deploy_name" -n "$namespace" | grep "memory:" | awk '{print $2}')
cpu_limit=$(kubectl get deployment "$deploy_name" -n "$namespace" -ojsonpath='{.spec.template.spec.containers[*].resources.limits.cpu}')
memory_limit=$(kubectl get deployment "$deploy_name" -n "$namespace" -ojsonpath='{.spec.template.spec.containers[*].resources.limits.memory}')
echo "Deployment,$deploy_name,$cpu_usage,$memory_usage,$cpu_limit,$memory_limit" >> k8s_resources.csv
done
done
# 获取所有 DaemonSet 的资源使用情况和限制
daemonsets=$(kubectl get daemonset --all-namespaces -o json)
for namespace in $(echo "$daemonsets" | jq -r '.items[].metadata.namespace' | uniq); do
echo "正在处理 DaemonSet,命名空间:$namespace"
ds_names=$(echo "$daemonsets" | jq -r ".items[] | select(.metadata.namespace==\"$namespace\") | .metadata.name")
for ds_name in $ds_names; do
echo "正在处理 DaemonSet:$ds_name"
cpu_usage=$(kubectl describe daemonset "$ds_name" -n "$namespace" | grep "cpu:" | awk '{print $2}')
memory_usage=$(kubectl describe daemonset "$ds_name" -n "$namespace" | grep "memory:" | awk '{print $2}')
cpu_limit=$(kubectl get daemonset "$ds_name" -n "$namespace" -ojsonpath='{.spec.template.spec.containers[*].resources.limits.cpu}')
memory_limit=$(kubectl get daemonset "$ds_name" -n "$namespace" -ojsonpath='{.spec.template.spec.containers[*].resources.limits.memory}')
echo "DaemonSet,$ds_name,$cpu_usage,$memory_usage,$cpu_limit,$memory_limit" >> k8s_resources.csv
done
done
echo "脚本执行完毕,结果已保存在 k8s_resources.csv 文件中。"
统计k8s中资源使用情况
最新推荐文章于 2025-03-07 08:21:42 发布
642

被折叠的 条评论
为什么被折叠?



