k8s cluster fs 체크

진웅·2025년 7월 16일

K8S Basics

목록 보기
22/40

#!/bin/bash

Kubernetes 클러스터 원라인 체크 명령어 모음

마스터 노드에서 실행

SSH_USER=${1:-"root"}

echo "========================================="
echo " K8s 클러스터 원라인 체크 명령어 실행"
echo "========================================="
echo ""

1. 모든 노드의 디스크 사용량 체크

echo "1. 📊 모든 노드 디스크 사용량:"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
disk_usage=(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "df / | tail -1 | awk '{print $5}'" 2>/dev/null)
if [[ -n "$disk_usage" ]]; then
printf "%-20s %s\n" "node""node" "disk_usage"
else
printf "%-20s %s\n" "$node" "연결실패"
fi
done
echo ""

2. 모든 노드의 메모리 사용량 체크

echo "2. 🧠 모든 노드 메모리 사용량:"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
memory_usage=(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "free | grep Mem | awk '{printf \"%.0f%%\", $3/$2 * 100.0}'" 2>/dev/null)
if [[ -n "$memory_usage" ]]; then
printf "%-20s %s\n" "node""node" "memory_usage"
else
printf "%-20s %s\n" "$node" "연결실패"
fi
done
echo ""

3. containerd 디렉토리 크기

echo "3. 📦 containerd 디렉토리 크기:"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
containerd_size=(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "du -sh /var/lib/containerd 2>/dev/null | cut -f1" 2>/dev/null)
if [[ -n "$containerd_size" ]]; then
printf "%-20s %s\n" "node""node" "containerd_size"
else
printf "%-20s %s\n" "$node" "N/A"
fi
done
echo ""

4. kubelet Pod 볼륨 크기

echo "4. 🏗️ kubelet Pod 볼륨 크기:"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
pod_size=(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "du -sh /var/lib/kubelet/pods 2>/dev/null | cut -f1" 2>/dev/null)
if [[ -n "$pod_size" ]]; then
printf "%-20s %s\n" "node""node" "pod_size"
else
printf "%-20s %s\n" "$node" "N/A"
fi
done
echo ""

5. 컨테이너 불일치 체크

echo "5. 🐳 컨테이너/Pod 불일치 체크:"
echo "노드 crictl kubectl 차이"
echo "--------------------------------------------"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
result=(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "
crictl_count=$(crictl ps --state running --quiet 2>/dev/null | wc -l)
kubectl_count=$(kubectl get pods --field-selector spec.nodeName=$(hostname) --all-namespaces --no-headers 2>/dev/null | grep Running | wc -l)
diff=$((crictl_count - kubectl_count))
echo \"$crictl_count $kubectl_count $diff\"
" 2>/dev/null)

if [[ -n "$result" ]]; then
    crictl_count=$(echo "$result" | awk '{print $1}')
    kubectl_count=$(echo "$result" | awk '{print $2}')
    diff=$(echo "$result" | awk '{print $3}')
    
    if [[ $diff -gt 20 ]]; then
        printf "%-20s %-7s %-8s %-4s ⚠️ 심각\n" "$node" "$crictl_count" "$kubectl_count" "$diff"
    elif [[ $diff -gt 10 ]]; then
        printf "%-20s %-7s %-8s %-4s ⚠️ 중간\n" "$node" "$crictl_count" "$kubectl_count" "$diff"
    else
        printf "%-20s %-7s %-8s %-4s ✅\n" "$node" "$crictl_count" "$kubectl_count" "$diff"
    fi
else
    printf "%-20s %-7s %-8s %-4s ❌ 연결실패\n" "$node" "N/A" "N/A" "N/A"
fi

done
echo ""

6. 정리 가능한 항목 요약

echo "6. 🧹 정리 가능한 항목 요약:"
echo "노드 중지컨테이너 미사용이미지 완료Pod 오래된로그"
echo "--------------------------------------------------------------"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
result=(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "
exited_containers=0
unused_images=0
completed_pods=0
old_logs=0

    if command -v crictl &> /dev/null; then
        exited_containers=\$(crictl ps --state exited --quiet 2>/dev/null | wc -l)
        unused_images=\$(crictl images --filter dangling=true --quiet 2>/dev/null | wc -l)
    fi
    
    if command -v kubectl &> /dev/null; then
        completed_pods=\$(kubectl get pods --field-selector spec.nodeName=\$(hostname) --all-namespaces --field-selector=status.phase=Succeeded --no-headers 2>/dev/null | wc -l)
    fi
    
    if [[ -d \"/var/log/pods\" ]]; then
        old_logs=\$(find /var/log/pods -type f -name \"*.log\" -mtime +7 2>/dev/null | wc -l)
    fi
    
    echo \"\$exited_containers \$unused_images \$completed_pods \$old_logs\"
" 2>/dev/null)

if [[ -n "$result" ]]; then
    printf "%-20s %-11s %-11s %-7s %s\n" "$node" $(echo "$result" | awk '{print $1, $2, $3, $4}')
else
    printf "%-20s %-11s %-11s %-7s %s\n" "$node" "N/A" "N/A" "N/A" "N/A"
fi

done
echo ""

7. 노드별 상위 큰 디렉토리 (containerd 관련)

echo "7. 📁 노드별 containerd 상위 큰 디렉토리 (TOP 3):"
kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do
echo "🔍 node:" ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no $SSH_USER@node "
if [[ -d \"/var/lib/containerd\" ]]; then
du -sh /var/lib/containerd/*/ 2>/dev/null | sort -hr | head -3 | while read size path; do
echo \" $size $(basename $path)\"
done
else
echo \" containerd 디렉토리 없음\"
fi
" 2>/dev/null || echo " 연결 실패"
echo ""
done

8. 클러스터 전체 통계

echo "8. 📊 클러스터 전체 통계:"
total_nodes=(kubectlgetnodesnoheaderswcl)readynodes=(kubectl get nodes --no-headers | wc -l) ready_nodes=(kubectl get nodes --no-headers | grep Ready | grep -v NotReady | wc -l)
total_pods=(kubectlgetpodsallnamespacesnoheaderswcl)runningpods=(kubectl get pods --all-namespaces --no-headers | wc -l) running_pods=(kubectl get pods --all-namespaces --no-headers | grep Running | wc -l)

echo " 총 노드: $total_nodes개 (Ready: $ready_nodes개)"
echo " 총 Pod: $total_pods개 (Running: $running_pods개)"

네임스페이스별 Pod 수

echo " 네임스페이스별 Pod 수 (TOP 5):"
kubectl get pods --all-namespaces --no-headers | awk '{print $1}' | sort | uniq -c | sort -nr | head -5 | while read count ns; do
echo " $ns: $count개"
done
echo ""

9. 빠른 정리 명령어 제안

echo "9. ⚡ 빠른 정리 명령어 제안:"
echo ""
echo "# 모든 노드에서 중지된 컨테이너 정리:"
echo "kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do"
echo " echo \"정리 중: $node\""
echo " ssh $SSH_USER@$node \"crictl rm \$(crictl ps -a --state=exited -q) 2>/dev/null || true\""
echo "done"
echo ""

echo "# 모든 노드에서 미사용 이미지 정리:"
echo "kubectl get nodes --no-headers -o custom-columns=NAME:.metadata.name | while read node; do"
echo " echo \"정리 중: $node\""
echo " ssh $SSH_USER@$node \"crictl rmi --prune 2>/dev/null || true\""
echo "done"
echo ""

echo "# 완료된 Pod 정리:"
echo "kubectl delete pods --field-selector=status.phase=Succeeded --all-namespaces"
echo ""

echo "========================================="
echo " 체크 완료"
echo "========================================="

profile
bytebliss

0개의 댓글