mcbss
mcbss

Reputation: 7

How to restart 1000's of pods at a time

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.

Upvotes: 0

Views: 520

Answers (4)

George Cimpoies
George Cimpoies

Reputation: 1004

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) }'

Upvotes: 0

BMitch
BMitch

Reputation: 264986

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

Upvotes: 0

Eduardo Baitello
Eduardo Baitello

Reputation: 11376

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+

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42541

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 in a loop

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

kubectl delete --all pods --namespace=foo

Upvotes: 2

Related Questions