How to restart 1000's of pods at a time

6/22/2020

We are having 1000's of pods running for our application. Due to some reason we need to restart 100's of pods.

Is there any way we can do it in kubernetes using kubectl or any other tool. Please advice. It should be pure pod restart.

-- mcbss
amazon-eks
kubernetes
kubernetes-helm
kubernetes-pod

4 Answers

6/22/2020

You can do this with a selector. Note that this is deleting the pods so you'll want to be sure they are running with a higher level construct like a deployment. And be sure your selector doesn't match pods you don't want to touch.

kubectl delete pods -l name=myLabel
-- BMitch
Source: StackOverflow

2/17/2022

Restart all deployments in a cluster (multiple namespaces)

kubectl get deployments --all-namespaces | tail +2 | awk '{ cmd=sprintf("kubectl rollout restart deployment -n %s %s", $1, $2) ; system(cmd) }'
-- George Cimpoies
Source: StackOverflow

6/22/2020

One way

kubectl scale deploymennt <your-deployment-goes-here> --replicas=0

and then

kubectl scale deploymennt <your-deployment-goes-here> --replicas=1000

Another way:

Write a script that: 1. will acquire a list of all active pods that belong to specific deployment 2. issue kubectl delete pod <item-from-a-list> in a loop

Yet another way (works if your pods belong to a dedicated namespace 'foo'):

kubectl delete --all pods --namespace=foo
-- Mark Bramnik
Source: StackOverflow

6/22/2020

If all the pods belong to a specific namespace, you can delete them all with kubectl delete pods --all --namespace=foo to recreate them (this may cause downtime).

If all the pods are controlled by the same deployment, you can trigger a rolling restart with kubectl rollout restart deployment/my-deployment-name.

Editing some deployment trivial value will also trigger a Rolling Update (possible no downtime) for all controlled pods (e.g., changing the terminationGracePeriodSeconds from 30 to 31).

If the pods are from multiple different deployments, here is a bash script that "refreshes" all pods in all deployments by namespace.

rollout restart is only available on kubernetes v1.15+

-- Eduardo Baitello
Source: StackOverflow