1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #!/bin/bash
#Author:zhy #Version:1.0 #Date:20190603 # #删除重启次数超过5次的POD
set -e
Time=`date +%Y%m%d%H%M%S`
echo $Time >> /home/cicd-pro/cron/k8s_pod_restart.log
if [[ `/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $0 }' |grep -v 'NAME' |wc -l` -eq 0 ]];then echo '无重启次数超过5次的POD' echo '无重启次数超过5次的POD' >> /home/cicd-pro/cron/k8s_pod_restart.log else echo '将要删除如下pod:' echo '将要删除如下pod:' >> /home/cicd-pro/cron/k8s_pod_restart.log /usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $1 }'| grep -v 'NAME' echo `/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $1 }'| grep -v 'NAME'` >> /home/cicd-pro/cron/k8s_pod_restart.log for i in `/usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config get pods | awk '{if ($4 >= 5) print $1 }' | grep -v 'NAME'`; do echo $i /usr/local/bin/kubectl --kubeconfig /home/cicd-pro/.kube/config delete pod $i done fi
|