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.
There are two ways to scale the number of replicas in a cluster:
manually on a terminal
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:
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.
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:
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:
1/1
means that the pod is ready.