How to Stop All Kubernetes Deployments
This article will uncover the fastest and easiest way to stop all Kubernetes deployments.
Before We Begin
In Kubernetes API, there is no verb “stop”. Technically speaking, it is not possible to “stop” something in Kubernetes. However, instead, we can set the number of replicas to zero. This action will instruct the deployment controller to delete all the existing pods of a given deployment. After that, no new pods will be created unless the replica count is increased back to more than zero. Applying this setting is a literal equivalent to stopping a deployment.
Stop a Single Deployment
To stop a deployment, you can run the following kubectl command:
kubectl --namespace default scale deployment my-deployment --replicas 0
Stop Multiple Deployments
Kubectl allows you to perform the same operation on multiple objects at once. By doing some Linux shell magic we can obtain a list of all deployments and scale all of them using a single command.
To stop all Kubernetes deployments, run the following kubectl command:
kubectl --namespace default scale deployment $(kubectl --namespace default get deployment | awk '{print $1}') --replicas 0
Also StatefulSets
Deployment is not the only resource that manages Kubernetes workloads, there are also stateful sets. You might want to scale them as well. The command to stop a stateful set is almost identical to the command for stopping a deployment.
To stop all Kubernetes stateful sets, run the following command:
kubectl --namespace default scale statefulset --replicas 0 $(kubectl --namespace default get statefulset | awk '{print $1}')
Delete All Resources
If you want to perform a complete cleanup of your Kubernetes cluster, you can delete all your resources at once:
kubectl delete all --all --namespace default
The examples in this article assume you’re running your workloads in a default namespace. If you are using some different namespace, replace “default” with your namespace name.
If you haven’t yet created your first Kubernetes cluster, it’s a perfect time to do so. And the easiest option is described in another article that we have – 5 ways to install a Kubernetes cluster
You can find other interesting articles on our blog: https://yourdevopsmentor.com/blog/
Apply for individual mentorship here: https://yourdevopsmentor.com/apply/
Connect with the author on LinkedIn: https://www.linkedin.com/in/vladimir-mukhin-devops/