簡(jiǎn)介:不知知道各位是如何清理的,我嘗試過用阿里的任何一個(gè)面板清理,但是還要換頁(yè)就很煩,所以自己就寫了一個(gè)小腳本,更GOOD!的是你還可以把他放到你的定時(shí)任務(wù)里面去,為啥要這么做,不得不說,咱的集群有點(diǎn)小垃圾,不過那也沒辦法,集群也不敢動(dòng),誰知道啥時(shí)候崩了,鍋誰來背,哈哈!
一個(gè)簡(jiǎn)單的不能再簡(jiǎn)單的shell腳本了
#!/bin/bash
source /etc/profile
printf "\n本次操作是用來清理集群內(nèi)部狀態(tài)為 Evicted 和 Failed 的Pod !\n"
printf "\nA. Aliyun-product B. Aliyun-test C. k8s-Intranet\n\n"
read -p "你需要清理的集群是:" mess
if [ $mess == A ];then
cluster=Aliyun-product
elif [ $mess == B ];then
cluster=Aliyun-test
elif [ $mess == C ];then
cluster=k8s-Intranet
fi
printf "\n以切換至您需要操作的集群!\n"
/usr/local/bin/kubecm switch $cluster > /dev/null
read -p "您是否需要選擇清理 (yes/no): " judge
if [ $judge == yes ];then
echo "正在清理中,請(qǐng)勿 ctrl+c 中斷操作!"
/usr/bin/kubectl get pods --all-namespaces -o go-template='{{range .items}} {{if (or (eq .status.phase "Evicted") (eq .status.phase "Failed" ))}} {{.metadata.name}}{{" "}} {{.metadata.namespace}} {{"\n"}}{{end}} {{end}}' | while read epod namespace; do kubectl -n $namespace delete pod $epod; done; >> /root/shell/pod/log/$mess-$(date +%Y-%m-%d-%H-%M-%S).txt
logfile=$mess-$(date +%Y-%m-%d-%H-%M-%S).txt
else
echo "正在停止并退出!"
exit
fi
if [ $? != 0 ];then
printf "\n在${cluster}集群未能清理成功\n"
elif [ ! -s ${logfile} ];then
printf "\n在${cluster}集群中沒有可清理的POD\n"
/usr/bin/rm -rf /root/shell/pod/log/${logfile}
else
printf "\n在${cluster}集群以清理完畢!\n"
printf "\n清理的POD保存在/root/shell/pod/log/${logfile}中\(zhòng)n"
fi
/usr/local/bin/kubecm switch k8s-Intranet > /dev/null
里面使用到了kubecm工具去切換集群,估計(jì)你們也都有用過,如果沒用過的小伙伴,可以去下載下,用法很簡(jiǎn)單。
然后加入定時(shí)任務(wù),讓他自己動(dòng)起來文章來源:http://www.zghlxwxcb.cn/news/detail-828853.html
00 */6 * * * /usr/bin/echo -e "A\nyes" | /bin/sh /root/shell/Clean-up-the-Failed-Evicted-pod.sh
最后,希望多多支持,點(diǎn)贊,也可以稍微的評(píng)論下,讓我有點(diǎn)存在感!哈哈哈哈!文章來源地址http://www.zghlxwxcb.cn/news/detail-828853.html
到了這里,關(guān)于清理k8s集群Evicted,F(xiàn)ailed的Pod!的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!