'Kubernetes: how to scale my pods

I'm new to Kubernetes. I try to scale my pods. First I started 3 pods:

./cluster/kubectl.sh run my-nginx --image=nginx --replicas=3 --port=80

There were starting 3 pods. First I tried to scale up/down by using a replicationcontroller but this did not exist. It seems to be a replicaSet now.

./cluster/kubectl.sh get rs
NAME                  DESIRED   CURRENT   AGE
my-nginx-2494149703   3         3         9h

I tried to change the amount of replicas described in my replicaset:

./cluster/kubectl.sh scale --replicas=5 rs/my-nginx-2494149703
replicaset "my-nginx-2494149703" scaled

But I still see my 3 original pods

./cluster/kubectl.sh get pods
NAME                        READY     STATUS    RESTARTS   AGE
my-nginx-2494149703-04xrd   1/1       Running   0          9h
my-nginx-2494149703-h3krk   1/1       Running   0          9h
my-nginx-2494149703-hnayu   1/1       Running   0          9h

I would expect to see 5 pods.

./cluster/kubectl.sh describe rs/my-nginx-2494149703
Name:       my-nginx-2494149703
Namespace:  default
Image(s):   nginx
Selector:   pod-template-hash=2494149703,run=my-nginx
Labels:     pod-template-hash=2494149703
        run=my-nginx
Replicas:   3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed

Why isn't it scaling up? Do I also have to change something in the deployment?

I see something like this when I describe my rs after scaling up: (Here I try to scale from one running pod to 3 running pods). But it remains one running pod. The other 2 are started and killed immediatly

  34s       34s     1   {replicaset-controller }            Normal      SuccessfulCreate    Created pod: my-nginx-1908062973-lylsz
  34s       34s     1   {replicaset-controller }            Normal      SuccessfulCreate    Created pod: my-nginx-1908062973-5rv8u
  34s       34s     1   {replicaset-controller }            Normal      SuccessfulDelete    Deleted pod: my-nginx-1908062973-lylsz
  34s       34s     1   {replicaset-controller }            Normal      SuccessfulDelete    Deleted pod: my-nginx-1908062973-5rv8u


Solution 1:[1]

TL;DR: You need to scale your deployment instead of the replica set directly.

If you try to scale the replica set, then it will (for a very short time) have a new count of 5. But the deployment controller will see that the current count of the replica set is 5 and since it knows that it is supposed to be 3, it will reset it back to 3. By manually modifying the replica set that was created for you, you are fighting with the system controller (which is untiring and will pretty much always outlast you).

Solution 2:[2]

This is working for me

kubectl scale --replicas=<expected_replica_num> deployment <deployment_label_name> -n <namespace>

Example

# kubectl scale --replicas=3 deployment xyz -n my_namespace

Solution 3:[3]

kubectl run my-nginx --image=nginx --replicas=3 --port=80 in this kubectl run will create a deployment or job to manage the created container(s).
Deployment-->ReplicaSet-->Pod this is how kubernetes works.
If you change the bottom-level object, its higher-level object will undo your change.You have to change the top-level object.

enter image description here

Solution 4:[4]

scale it down to zero and then to the number of pods you required (guess it equals to 3)

kubectl scale deployment <deployment-name> --replicas=0 -n <namespace>
kubectl scale deployment <deployment-name> --replicas=3 -n <namespace>

Solution 5:[5]

Not sure if this is the best way as I'm starting out with kubernetes, but I did this by updating my yaml file

# app.yaml
apiVersion: apps/v1
...
spec:
  replicas: <new value>

and running $ kubectl scale -f app.yaml --replicas=<new value>

you can verify your new number of replicas by running $ kubectl get pods

In my case I was also interested in scaling back my VMs, on google cloud. I did this with $ gcloud container clusters resize appName --size=1 --zone "my-zone"

Solution 6:[6]

Below example shows how you should scale up/down your "pods/resource/deployments".

k8smaster@k8smaster:~/debashish$ more createdeb_deployment1.yaml 


--- 
apiVersion: apps/v1beta2
kind: Deployment
metadata: 
  name: debdeploy-webserver
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: debdeploy1webserver
  template: 
    metadata: 
      labels: 
        app: debdeploy1webserver
    spec: 
      containers: 
        - 
          image: "docker.io/debu3645/debapachewebserver:v1"
          name: deb-deploy1-container 
          ports: 
            - 
              containerPort: 6060

deployment created -->

**kubectl -n debns1 create -f createdeb_deployment1.yaml**




k8smaster@k8smaster:~/debashish$ `kubectl scale --replicas=5 **deployment**/debdeploy-webserver -n debns1`

(Scale up 5 deployments)

k8smaster@k8smaster:~/debashish$ kubectl get pods -n debns1


NAME                                   READY   STATUS    RESTARTS   AGE
debdeploy-webserver-7cf4fb74c5-8wvzx   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-jrf6v   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-m9fpw   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running   0          16s
debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running   1          19h
resourcepod-deb1                       1/1     Running   5          6d18h




k8smaster@k8smaster:~/debashish$ **kubectl get ep -n debns1**



NAME                ENDPOINTS                                                     AGE
frontend-svc-deb    192.168.1.10:80,192.168.1.11:80,192.168.1.12:80 + 2 more...   18h
frontend-svc1-deb   192.168.1.8:80                                                14d
frontend-svc2-deb   192.168.1.8:80                                                5d19h




k8smaster@k8smaster:~/debashish$ **kubectl scale --replicas=2** deployment/debdeploy-webserver -n debns1 

(Scale down from 5 to 2)

deployment.extensions/debdeploy-webserver scaled

k8smaster@k8smaster:~/debashish$ **kubectl get pods -n debns1**


NAME                                   READY   STATUS        RESTARTS   AGE
debdeploy-webserver-7cf4fb74c5-8wvzx   1/1     Terminating   0          35m
debdeploy-webserver-7cf4fb74c5-jrf6v   1/1     Terminating   0          35m
debdeploy-webserver-7cf4fb74c5-m9fpw   1/1     Terminating   0          35m
debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running       0          35m
debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running       1          19h
resourcepod-deb1                       1/1     Running       5          6d19h


k8smaster@k8smaster:~/debashish$ **kubectl get pods -n debns1**


NAME                                   READY   STATUS    RESTARTS   AGE
debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running   0          37m
debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running   1          19h
resourcepod-deb1                       1/1     Running   5          6d19h

k8smaster@k8smaster:~/debashish$ kubectl **scale --current-replicas=4 --replicas=2** deployment/debdeploy-webserver -n debns1  (Check the current no. of deployments. If current replication is 4, then bring it down to 2, else dont do anything)


error: Expected replicas to be 4, was 2


k8smaster@k8smaster:~/debashish$ **kubectl scale --current-replicas=3 --replicas=10 deployment/debdeploy-webserver -n debns1**


error: Expected replicas to be 3, was 2


k8smaster@k8smaster:~/debashish$ **kubectl scale --current-replicas=2 --replicas=10 deployment/debdeploy-webserver -n debns1**

deployment.extensions/debdeploy-webserver scaled

k8smaster@k8smaster:~/debashish$ **kubectl get pods -n debns1**


    NAME                                   READY   STATUS              RESTARTS   AGE
    debdeploy-webserver-7cf4fb74c5-46bxg   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-d6qsx   0/1     ContainerCreating   0          6s
    debdeploy-webserver-7cf4fb74c5-fdq6v   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-gd87t   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-kqdbj   0/1     ContainerCreating   0          6s
    debdeploy-webserver-7cf4fb74c5-q9n7r   1/1     Running             0          47m
    debdeploy-webserver-7cf4fb74c5-qjvm6   1/1     Running             0          6s
    debdeploy-webserver-7cf4fb74c5-skxq4   0/1     ContainerCreating   0          6s
    debdeploy-webserver-7cf4fb74c5-ttw6p   1/1     Running             1          19h
    debdeploy-webserver-7cf4fb74c5-wlc7q   0/1     ContainerCreating   0          6s
    resourcepod-deb1                       1/1     Running             5          6d19h

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Bailey
Solution 2
Solution 3 AATHITH RAJENDRAN
Solution 4 Isuru Amarathunga
Solution 5
Solution 6