How to use "kubectl -l/--selector" to scale a specific pod in a deployment in Kubernetes

10/22/2019

I want to scale in a pod with a specific label, but it seems -l does not work for scaling.

For example, if I have pods bla-12345-aaaaa, bla-12345-bbbbb, and bla-12345-cccc, and I scale in to 2 replicas, I want bla-12345-aaaaa specifically to disappear.

I get this error:

kubectl scale --replicas=2 deployment/bla -l scaleIn=true
error: selectors and the all flag cannot be used when passing resource/name arguments

You can see it in the documentation:

-l, --selector='': Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)

-- surimi sticks
kubernetes

3 Answers

10/22/2019

You can also use the following format:

kubectl scale --replicas=2 deployment -l scaleIn=true

It will scale all the deployment that match the label selector.

-- Kamol Hasan
Source: StackOverflow

10/22/2019

Follow the below command

kubectl scale  deployment/bla --replicas=2

Note: You cant scale pods. you can scale replicaSets/Deployments/StatefulSets

Follow the below steps to scale deployment by label

[root@f482a0035623 work]# kubectl get deploy -l app=web   
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
webapp   1/1     1            1           88d
[root@f482a0035623 work]# kubectl scale deploy -l app=web --replicas=2
deployment.extensions/webapp scaled
[root@f482a0035623 work]# kubectl get deploy -l app=web
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
webapp   2/2     2            2           88d
-- P Ekambaram
Source: StackOverflow

10/22/2019

Like David Maze said, you cannot scale pods.

You can check that by doing kubectl scale explain:

Examples:

Scale a replicaset named 'foo' to 3.

kubectl scale --replicas=3 rs/foo

Scale a resource identified by type and name specified in "foo.yaml" to 3.

kubectl scale --replicas=3 -f foo.yaml

If the deployment named mysql's current size is 2, scale mysql to 3.

kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

Scale multiple replication controllers.

kubectl scale --replicas=5 rc/foo rc/bar rc/baz

Scale statefulset named 'web' to 3.

kubectl scale --replicas=3 statefulset/web

Also you can read in the documentation Horizontal Pod Autoscaler.

The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics). Note that Horizontal Pod Autoscaling does not apply to objects that can’t be scaled, for example, DaemonSets.

You can target the deployment to scale in 2 ways. One is to provide a name of the deployment like so:

kubectl scale --replicas=2 deployment/bla

or targeting it with a label:

kubectl scale deploy -l scaleIn=true --replicas=2

-- Spook
Source: StackOverflow