0%

Shell脚本-定期删除异常Pod

需求

有的Pod重启次数过多

1
2
3
4
5
6
7
8
9
[cicd-test@ops-jenkins-master ~]$ kubectl get pods |awk '{if ($4 >= 1) print $0 }'
NAME READY STATUS RESTARTS AGE
cmsservice-vcg-com-57974bc4cf-vq6mk 1/1 Running 1 6m18s
edgeservice-vcg-com-755b96fb78-v96s5 1/1 Running 1 3d23h
edgeserviceweb-vcg-com-5655fc9899-8lxql 1/1 Running 1 3d23h
node-visualchina-web-8575cf78dc-8jr2k 1/1 Running 1 4h11m
vdam-gateway-service-6b7d864d67-rdzrg 1/1 Running 1 3h36m
vdam-passport-service-7c97d84b58-xhn6k 1/1 Running 6 3d22h

脚本
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