Files
k8s-docs/k8s/scripts/cleanup-bad-pods.sh

50 lines
1.3 KiB
Bash
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
# 一键清理集群中常见异常状态的 Pod
# 会删除以下 STATUS 的 Pod
# - Evicted
# - Error
# - ImagePullBackOff
# - ContainerStatusUnknown
set -euo pipefail
STATUSES=("Evicted" "Error" "ImagePullBackOff" "ContainerStatusUnknown")
echo "======================================"
echo "Kubernetes 异常 Pod 清理脚本"
echo "======================================"
echo
for status in "${STATUSES[@]}"; do
# 过滤出该状态的 PodNAMESPACE NAME READY STATUS ...
MAPFILE -t pods < <(kubectl get pods -A 2>/dev/null | awk -v s="$status" '$4==s {print $1" "$2}')
if [[ ${#pods[@]} -eq 0 ]]; then
echo "[${status}] 无需清理"
continue
fi
echo "[${status}] 发现 ${#pods[@]} 个 Pod"
for line in "${pods[@]}"; do
echo " $line"
done
for line in "${pods[@]}"; do
ns=$(awk '{print $1}' <<<"$line")
name=$(awk '{print $2}' <<<"$line")
if [[ -z "$ns" || -z "$name" ]]; then
continue
fi
echo "删除 $ns/$name ..."
kubectl delete pod -n "$ns" "$name" --grace-period=0 --force || true
done
echo
done
echo "======================================"
echo "清理完成"
echo "======================================"
echo "提示Deployment/StatefulSet/DaemonSet 会自动重建对应 Pod如果存在"