Sunday, August 13, 2023

How to Stop running workloads and in Kubernetes

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.

kubectl --namespace default scale deployment my-deployment --replicas 0

Now to get the deployment name, below command can be used 

kubectl get deploy -o wide

NAME           READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS                                             IMAGES                  SELECTOR

nginx-webapp   5/5     5            5           8h    sidecar-container1,sidecar-container2,main-container   busybox,busybox,nginx   app=nginx-webapp

So the command to stop the deployment in this scenario is 

kubectl --namespace default scale deployment nginx-webapp --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 

Deployment is not the only resource that manages Kubernetes workloads, there are also stateful sets.

kubectl --namespace default scale statefulset --replicas 0 $(kubectl --namespace default get statefulset  | awk '{print $1}')

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


references:

https://yourdevopsmentor.com/blog/how-to-stop-all-kubernetes-deployments/#:~:text=In%20Kubernetes%20API%2C%20there%20is,pods%20of%20a%20given%20deployment.


No comments:

Post a Comment