Files
k3s/cluster/check_resources.sh

41 lines
1.5 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 "🔋 高資源使用 Pod (>80% CPU 或 >1G Memory)"
echo "------------------------------------------"
kubectl top pods -A 2>/dev/null | awk -v OFS='\t' 'NR>1 && ($3 > 80 || $4 > 1000) {print $1, $2, $3"%", $4"Mi"}' || echo "無法計算"
echo ""
echo "📊 資源請求 vs 限制"
echo "------------------------------------------"
echo "查看 Pod 是否設定了資源限制:"
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.spec.containers[*].resources.requests.cpu}{"\t"}{.spec.containers[*].resources.limits.cpu}{"\n"}{end}' | \
awk '{if ($3 != "<none>" && $4 == "<none>") print $1"/"$2 " ❌ 只有 request 無 limit"; else if ($3 == "<none>") print $1"/"$2 " ⚠️ 無資料設定"}' | head -10 || echo -e "${GREEN}所有 Pod 都有設定${NC}"
echo ""
echo "=========================================="