Kubernetes: restart the pods of all deployments

Question

How can I restart all pods in a Kubernetes cluster?

Is there a simple command to restart all pods in a Kubernetes cluster?

For example if you run a Rails migration on the database (rails db:migrate), then you need to invalidate the ActiveRecord cache using a restart.

For example on Heroku:

$ heroku restart

Is there an equivalent command for Kubernetes?

Answer

There’s a simple command to restart all the pods inside all deployments:

kubectl rollout restart deploy -n mynamespace

It will perform a rollout restart, meaning that the pods are replaced gradually to avoid any downtime.

Note that in the cluster there might be also bare pods (not associated to any deployment), Jobs, etc. that are not restarted by this command.