I trying to write a script to delete pods status CrashLoopBackOff
from all namespaces.
#!/bin/bash
# This script is basically check all avialble namespaces
# and delete pods in any particular status like 'Evicted',
# 'CrashLoopBackOff','Terminating'
NAMESPACE="popeye"
delpods2=$(sudo kubectl get pods -n ${NAMESPACE} |
grep -i 'CrashLoopBackOff' |
awk '{print $1 }')
for i in ${delpods2[@]}; do
sudo kubectl delete pod $i --force=true --wait=false \
--grace-period=0 -n ${NAMESPACE}
done
The above script works with a specified namespace but how we can set if I have multiple namespaces and check for the pods in each one.
I would suggest a better way for doing this. You can simply run
This way is much simple and neat. However, note that this doesn't delete
evicted
andCrashLoopBackOff
pods only , but also pods that have failed due to different reasons ("ContainerCannotRun", "Error", "ContainerCreating", etc.).We can also make this even better. Using
-A
instead of--all-namespaces
.So, the final command is
Happy Hacking!