Restart ALL pods in Kubernetes cluster

Jbn1233
1 min readApr 13, 2021

--

Yes, someday you may need this.

  1. Do it soft way.

This one takes some time to finish. Depends on the number of POD in your cluster.

kubectl get ns -o name|awk -F/ {'print $2'} |grep -vE "^kube-system$"| xargs -I{} kubectl -n {} rollout restart deploy
kubectl get ns -o name|awk -F/ {'print $2'} |grep -vE "^kube-system$"| xargs -I{} kubectl -n {} rollout restart ds
kubectl get ns -o name|awk -F/ {'print $2'} |grep -vE "^kube-system$"| xargs -I{} kubectl -n {} rollout restart sts

2. Do it hard way.

#!/bin/bashfor i in $(kubectl get pods -A|grep -vE "^NAMESPACE|^kube-system"|shuf|awk {'print $1","$2'})
do
IFS=', ' read -r -a arr <<< "$i"echo "NameSpace: " ${arr[0]}
echo "Pod ID: " ${arr[1]}
echo "kubectl delete pod ${arr[1]} -n ${arr[0]}"
kubectl delete pod ${arr[1]} -n ${arr[0]} &>/dev/null &
#little sleep, reduce load to api-server and scheduler
sleep 0.5
done

Good luck.

--

--

Jbn1233
Jbn1233

Written by Jbn1233

Very short and simple notes for CKA/SRE and may not works on your environment | jbn1233@gmail.com | Bangkok, Thailand |

No responses yet