feat: add check_events.sh, check_resources.sh, check_all.sh for diagnostics

This commit is contained in:
2026-04-01 17:10:03 +08:00
parent 2793bce239
commit 18bce33a1e
3 changed files with 116 additions and 0 deletions

40
cluster/check_resources.sh Executable file
View File

@@ -0,0 +1,40 @@
#!/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 "=========================================="