PPK
PPK

Reputation: 1427

Scale down Kubernetes Pods

I am using

kubectl scale --replicas=0 -f deployment.yaml

to stop all my running Pods. Please let me know if there are better ways to bring down all running Pods to Zero keeping configuration, deployments etc.. intact, so that I can scale up later as required.

Upvotes: 133

Views: 322733

Answers (10)

Anil Bhaskar
Anil Bhaskar

Reputation: 3978

If you want a non command way, if you are using k9s interface.

  • Go to deployments, select the deployment you want to scale up or down.
  • Press s from keyboard, this is a shortcut to Scale a deployment.
  • Edit the number of replicas in popup.

Upvotes: 1

Vaibhav Sharma
Vaibhav Sharma

Reputation: 1742

You can scale multiple resources as given below:

kubectl scale --replicas=0 deployment/app deployment/database

And You can scale ALL resource as given below:

kubectl scale --all --replicas=0 --namespace=default deployment

Upvotes: 7

Saif Haider
Saif Haider

Reputation: 621

kubectl get svc | awk '{print $1}' | xargs kubectl scale deploy --replicas=0

Upvotes: -2

timboslicecreative
timboslicecreative

Reputation: 201

Use the following to scale down/up all deployments and stateful sets in the current namespace. Useful in development when switching projects.

kubectl scale statefulset,deployment --all --replicas=0

Add a namespace flag if needed

kubectl scale statefulset,deployment -n mynamespace --all --replicas=0

Upvotes: 20

Vivek Raveendran
Vivek Raveendran

Reputation: 1185

The solution is pretty easy and straightforward

kubectl scale deploy -n <namespace> --replicas=0 --all 

Upvotes: 104

Tiago Medici
Tiago Medici

Reputation: 2204

  kubectl get deployments
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
app-gke   3/3     3            3           13m

  kubectl scale deploy app-gke --replicas=5
deployment.extensions/app-gke scaled


kubectl get pods
NAME                       READY   STATUS              RESTARTS   AGE
app-gke-7b768cd6d7-b25px   2/2     Running             0          11m
app-gke-7b768cd6d7-glj5v   0/2     ContainerCreating   0          4s
app-gke-7b768cd6d7-jdt6l   2/2     Running             0          11m
app-gke-7b768cd6d7-ktx87   2/2     Running             0          11m
app-gke-7b768cd6d7-qxpgl   0/2     ContainerCreating   0          4s

enter image description here

Upvotes: 7

elhay efrat
elhay efrat

Reputation: 167

If you want generic patch:

namespace=devops-ci-dev
kubectl get deployment -n ${namespace} --no-headers| awk '{print $1}' | xargs -I elhay kubectl patch deployment -n ${namespace} -p '{"spec": {"replicas": 1}}' elhay 

Change namespace=devops-ci-dev, to be your name space.

Upvotes: 0

akahuna
akahuna

Reputation: 1

If you need more granularity with pipes or grep, here is another shell solution:

for i in $(kubectl get deployments | grep -v NAME | grep -v app | awk '{print $1}'); do kubectl scale --replicas=2 deploy $i; done

Upvotes: 0

R0MARI0
R0MARI0

Reputation: 289

Here we go. Scales down all deployments in a whole namespace:

kubectl get deploy -n <namespace> -o name | xargs -I % kubectl scale % --replicas=0 -n <namespace>

To scale up set --replicas=1 (or any other required number) accordingly

Upvotes: 28

mdaniel
mdaniel

Reputation: 33231

You are doing the correct action; traditionally the scale verb is applied just to the resource name, as in kubectl scale deploy my-awesome-deployment --replicas=0, which removes the need to always point at the specific file that describes that deployment, but there's nothing wrong (that I know of) with using the file if that is more convenient for you.

Upvotes: 170

Related Questions