This commit is contained in:
2025-11-26 17:02:14 +08:00
parent 2418891634
commit ba1f52996f
33 changed files with 415 additions and 1216 deletions

82
k8s/xiongxiao.me/clean.sh Executable file
View File

@@ -0,0 +1,82 @@
#!/bin/bash
# 清理 Kubernetes 集群中失败和未就绪的 Pod
# tags: kubernetes, k3s, cleanup, maintenance
# description: 自动清理所有失败、未就绪或处于错误状态的 Pod
# title: Kubernetes Pod 清理脚本
# createdAt: 2025-11-26
set -e
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo "======================================"
echo "Kubernetes Pod 清理脚本"
echo "======================================"
echo ""
# 获取所有失败或未就绪的 Pod
echo -e "${YELLOW}正在扫描失败的 Pod...${NC}"
echo ""
# 查找所有问题 Pod
FAILED_PODS=$(kubectl get pods -A -o json | jq -r '
.items[] |
select(
.status.phase != "Running" and
.status.phase != "Succeeded" or
(.status.containerStatuses // [] | any(.ready == false))
) |
"\(.metadata.namespace) \(.metadata.name) \(.status.phase // "Unknown")"
' 2>/dev/null || echo "")
if [ -z "$FAILED_PODS" ]; then
echo -e "${GREEN}✓ 没有发现失败的 Pod${NC}"
echo ""
exit 0
fi
echo "发现以下问题 Pod"
echo "$FAILED_PODS" | while read namespace name status; do
echo -e "${RED} [$status] $namespace/$name${NC}"
done
echo ""
# 确认删除
read -p "是否删除这些 Pod(y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}取消清理${NC}"
exit 0
fi
# 删除问题 Pod
echo ""
echo -e "${YELLOW}开始清理...${NC}"
DELETED=0
FAILED=0
echo "$FAILED_PODS" | while read namespace name status; do
if [ -n "$namespace" ] && [ -n "$name" ]; then
printf "删除 ${namespace}/${name} ... "
if kubectl delete pod "$name" -n "$namespace" --grace-period=0 --force 2>/dev/null; then
echo -e "${GREEN}${NC}"
((DELETED++)) || true
else
echo -e "${RED}${NC}"
((FAILED++)) || true
fi
fi
done
echo ""
echo -e "${GREEN}======================================"
echo "清理完成"
echo "======================================${NC}"
echo ""
echo "提示:相关的 Deployment/StatefulSet 会自动重新创建 Pod"
echo ""