How to scale a number of replicas in Kubernetes

Scaling a number of replicas in Kubernetes

Pods are the most basic unit that is deployable in Kubernetes, and replica is simply a duplication of the pod. On a cluster where Kubernetes is deployed, increasing or decreasing the number of similar pods (or replicas) is known as scaling. The following diagram shows an example of scaling:

In the diagram above, the Kubernetes cluster has 1 master node (depicted by a red hexagon) and 2 worker nodes (represented by 2 blue hexagons). The pods are displayed using green squares. Initially, only 2 pods are deployed on the cluster, one on each worker node. If we scale up the pods by 2, then one more replica of the pod is started on each worker node. If we scale down the pods by 1, then only 1 pod remains on the left worker node.

How to scale the number of replicas

There are two ways to scale the number of replicas in a cluster:

  1. manually on a terminal

  2. auto-scaling.

Note: The scope of this shot is limited to manual scaling.

The following text assumes that we have deployed a Kubernetes cluster with one pod:

First, we’ll find out the different types of deployments (or pods) that are present on the cluster via the following command:

kubectl get deployments

This will give us a table with the following columns:

NAME READY UP-TO-DATE AVAILABLE AGE
my-deployment 1/1 1 1 5m

Note: This table will be printed out on the terminal

The fields of the table are described below:

  • NAME: Name of the deployment in the cluster.
  • READY: Ratio of CURRENT/DESIRED replicas.
  • UP-TO-DATE: The number of replicas that have been updated to reach the DESIRED replica count.
  • AVAILABLE: Number of replicas available to the users.
  • AGE: Uptime of the application.

Next, to scale the number of replicas, we can use the following command:

kubectl scale deployments/<deployment_name> --replicas=<num_replicas>

Here, <deployment_name> is the name of the deployment we want to scale, and <num_replicas> is the number of replicas we wish to have for said deployment. If we want to scale up my-deployment to 4 replicas, we’ll use the following command:

kubectl scale deployments/my-deployment --replicas=4

Finally, to verify whether our deployment is scaled or not, we can run the kubectl get deployments command and check the READY parameter. Alternatively, we can also check ReplicaSets status or pods status to verify the scaling of our deployment.

ReplicaSets status

The following command gives the status of the replica sets:

kubectl get rs

This will give us a table with the following columns:

NAME DESIRED CURRENT READY AGE
my-deployment-74fa83b46b 4 4 4 12s

Note: This table will be printed out on the terminal.

The fields of the table are described below:

  • NAME: Name of Replica Set in the cluster.
  • DESIRED: The desired number of replicas for the application.
  • CURRENT: Number of replicas of the application that are currently running.
  • READY: Number of replicas that are available to the users.
  • AGE: Uptime of the application.

Pods status

The following command gives the status of pods in the cluster:

kubectl get pods --show-labels

This will give us a table with the following columns:

NAME READY STATUS RESTART AGE LABELS
my-deployment-74fa83b46b-ab3c7 1/1 Running 0 12s app=my-app,pod-template-hash=1234561234
my-deployment-74fa83b46b-azser 1/1 Running 0 12s app=my-app,pod-template-hash=1234561234
my-deployment-74fa83b46b-cf6fd 1/1 Running 0 12s app=my-app,pod-template-hash=1234561234
my-deployment-74fa83b46b-9hd4f 1/1 Running 0 12s app=my-app,pod-template-hash=1234561234

Note: This table will be printed out on the terminal.

The fields of the table are described below:

  • NAME: Name of pod in the cluster.
  • READY: 1/1 means that the pod is ready.
  • STATUS: Status of the pod.
  • AGE: Uptime of the pod.
  • LABELS: Have an app label and a pod-template-hash value.
Copyright ©2024 Educative, Inc. All rights reserved