Files
k3s/cluster/check_resources.sh

57 lines
1.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 用途檢查資源使用率CPU/Memory
set -euo pipefail
# 顏色定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo "=========================================="
echo " K3s 資源使用率"
echo "=========================================="
echo ""
# 節點資源
echo "🖥️ 節點資源"
echo "------------------------------------------"
kubectl top nodes 2>/dev/null || echo "需要 metrics-server請稍後再試"
echo ""
echo "📦 Pod 資源使用 (Top 10)"
echo "------------------------------------------"
kubectl top pods -A --sort-by=cpu 2>/dev/null | head -11 || echo "需要 metrics-server"
echo ""
echo "🔋 資源不足警告 (CPU>80% 或 MEM>1Gi)"
echo "------------------------------------------"
HIGH_PODS=$(kubectl top pods -A 2>/dev/null | awk -v OFS='\t' 'NR>1 {split($4, v, /([0-9]+)/); mem=int(v[2]); if ($3+0 > 80 || mem > 1024) print $0}')
if [ -z "$HIGH_PODS" ]; then
echo -e "${GREEN}✓ 無高資源使用 Pod${NC}"
else
echo "$HIGH_PODS" | while IFS=$'\t' read -r ns name cpu mem; do
echo -e "${YELLOW}$ns/$name${NC}"
echo " CPU: $cpu Memory: $mem"
done
fi
echo ""
echo "📊 無資源限制的 Pod"
echo "------------------------------------------"
NO_LIMIT=$(kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"/"}{.metadata.name}{"\t"}{.spec.containers[*].resources.requests.cpu}{"\t"}{.spec.containers[*].resources.limits.cpu}{"\n"}{end}' | \
awk -F'\t' '$3 == "<none>" {print $1}')
if [ -z "$NO_LIMIT" ]; then
echo -e "${GREEN}✓ 所有 Pod 都有資料設定${NC}"
else
echo "$NO_LIMIT" | while read -r pod; do
echo -e "${YELLOW}$pod${NC} - 無資源限制"
done | head -10
fi
echo ""
echo "=========================================="