How to Restart All Pods in a Kubernetes Namespace
Where I work, we use a repo-per-namespace setup and so it often happens that I want to restart all pods and deployments in a single Kubernetes namespace. Maybe I want to see the startup logs, or maybe I want to shut down production for a few seconds. Don’t question my motives.
Anyway, what matters is that bouncing all deployments one after another is inconvenient and I don’t like typing.
The best way to bounce (kubectl >= 1.15)
I recently found out from a friend that there is an easier way as of kubectl 1.15+. Restarting all the pods in a namespace is as easy as running the following kubectl command.
kubectl -n {NAMESPACE} rollout restart deploy
The old way (kubectl <= 1.14)
In older versions of kubectl you needed to run a command for each deployment in the namespace. In true lazy developer fashion I wrote a little script that will do it for me:
deploys=`kubectl -n $1 get deployments | tail -n +2 | cut -d ' ' -f 1`
for deploy in $deploys; do
kubectl -n $1 rollout restart deployments/$deploy
done
It’s fairly simple to use. Assuming I named the script kubebounce.sh:
./kubebounce.sh {NAMESPACE}
I made a little open-source repo with installation instructions if you want to add it to your $PATH. Be sure to star the repo if you find it useful.
How It Works
Bash isn’t exactly the easiest language to read. Let’s go over each portion of the script.
deploys=`kubectl -n $1 get deployments | tail -n +2 | cut -d ' ' -f 1`
In bash, $1 refers to the first command-line argument, the namespace in our case. In essence, this line gets all the deployments in the target namespaces and saves them into a deploys variable. We pipe the output of the kubectl get deployments command into a tail -n +2 command, which just strips off the first line of the output. Then we run that output through a cut command which leaves us with a nice list of all the deployment names.
That’s the trickier part, next we just loop over all the deployments and restart them one by one:
for deploy in $deploys; do
kubectl -n $1 rollout restart deployments/$deploy
done
PS: I’m the author of a fully interactive Kubernetes course on Boot.dev if you’d like to learn more about Kubernetes.
Related Articles
Systems and Processes that Aren't in Code are Terrifying
Oct 03, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
My worst enemy is processes that a developer spun up years ago on a server everyone has forgotten about. I don’t know how to find these systems reliably, I don’t know where they came from, what depends on them, and if they are safe to delete. For example, the dreaded 15 6 2 1 * /home/lane/backup.sh. You may recognize this as a Unix cronjob, a job that is scheduled to run on a server periodically.
Automatic Cross-Platform Deployments with Electron
Aug 08, 2019 by Lane Wagner - Boot.dev co-founder and backend engineer
This is a tutorial on how to set up an Electron app on Travis CI, so that new versions are deployed to GitHub Releases with a simple pull request.