Open In App

Scaling Applications With Kubernetes ReplicaSets

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Kubernetes is an orchestration tool used to scale , deploy and manage the containerized application. It provides ReplicaSet which is used to run a desired number of pods on any given time . In this guide, I will first discuss what Kubernetes is. Then I will discuss about ReplicaSets. After this I will walk you through the different steps to scale applications using Kubernetes ReplicaSet.

What Is Kubernetes?

Kubernetes is a container orchestration tool that helps to scale, deploy, and manage any containerized applications. It follows a master-slave architecture. The master node is the control plane in a Kubernetes cluster which manages all the cluster operation. The master node consists of four main components, API server which helps in interacting with the cluster, scheduler which helps in scheduling the pods to run, etcd to store cluster data and control manager which helps to manage the different controllers like deployment, replicaset, and many more. The worker node is the place in a Kubernetes cluster where the containers are deployed and executed. Worker nodes have some important components like kubelet which is used to manage the pod lifecycle on the node, container runtime which helps in running the containers, and kube-proxy which manages all the networking rules among the pods.

Kubernetes provides many important features such as:

  • Automatically scales up or down on the basis of traffic it receives.
  • Automatically self heals, this means it automatically starts a new pod on any pod failure or delete.
  • Automates the deployment process , by enabling roll out updates to application without any downtime.
  • Monitoring tool such as Grafana and Prometheus can be used to monitor entire Kubernetes cluster.

What Is ReplicaSets ?

ReplicaSet is used to maintain a certain number of pods always running all the time. ReplicaSet always monitors the current state and desired state of pods. ReplicalSet ensure a stable set of healthy pods running within a cluster at any given time. In the ReplicaSet configuration file we mainly mention two important features, number of replicas to maintain desired replica count and a pod template for creating new pods. On any pod failure or deletion of pod , replicaset automatically creates another healthy pod in the cluster to maintain the desired number of replicas. This feature of ReplicaSet ensures high availability.

The ReplicaSet configuration file consists of the following fields :

  • ApiVersion : It describes the Kubernetes API version that supports ReplicaSet .
  • Kind : It describe what type of resource . For example RepliceSet
  • Replicas : It describes the number of desired replicas .
  • Selector : It describes the group of pod .
  • Template : It describes which image and container port is used to create a pod .

Scaling Applications Using Kubernetes ReplicaSets: A Step-By-Step Guide

Step 1 : Start the minikube software tool.

 minikube start

minikube-start

Step 2 : Now create a ReplicaSet configuration file .

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: gfg-nginx-replica
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80



Kubernetes Replica Set

Step 3 : Create service configuration file . This file will help us to access the application from our browser.

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80


Kubernetes service Yaml

Step 4 : Apply all the YAML configuration files.

kubectl apply -f replica-set.yml
kubectl apply -f service.yml


kubectl  updating services

  • Here if you are using docker to run minikube then you have to write this additional command to access the service from your browser.
minikube service nginx-service

minikube nginx service

Step 5: Check number of pods running . It is same as the number of replicas mentioned in the ReplicaSet .

kubectl get pods


Listing kubernetes pods

Step 6 : Delete a pod and then observe the pods running in the cluster . You will notice the moment you delete a pod , another pod is created to maintain the desired state.

kubectl delete pod gfg-nginx-replica-2tl4c (here mention the pod name in your cluster )
kubectl get pods -w

deleting pods

Step 7 : You can also change the desired number of replicas using the command below .

kubectl scale replicaset gfg-nginx-replica --replicas=5

Kubernetes Scaling pods

  • After this you observe the number of pods running in the cluster.
kubectl get pods

Listing pods

Step 8 : Access the application from the browser now .

access-nginx application

Conclusion

Here in this guide you have first learned what is Kubernetes. Then you have learned about what is ReplicaSets. After this you have created a ReplicaSet and a Service YAML configuration file . Finally you have observed the behavior of pods running in the cluster by deleting a pod and also scaling up the number of replicas.

Scaling Applications With Kubernetes ReplicaSets – FAQ’s

What Is The Difference Between ReplicaSet And Deployment?

ReplicaSet provides low level of abstraction with basic scaling features but kubernetes deployment provides higher level of abstraction having features like versioning, rollingupdate and many more .

What Action Taken By ReplicaSet If You Delete A Pod Running In The Cluster ?

If you delete a pod running in the cluster then the ReplicaSet will automatically schedule another pod to run, so that it can maintain the desired state.

What Is The Purpose Of Using ReplicaSet?

ReplicaSet is used to maintain a desired number of pods running on the Kubernetes cluster. Using ReplicaSet ensures high availability as it automatically replaces any failed pods .

What Will Happen If I Delete The ReplicaSet?

If you delete the ReplicaSet then all the running pods managed by ReplicaSet will also deleted.

What Is The Command Used To Scale Up Or Down In A ReplicaSet ?

You can use the following command to scale up or down:

 kubectl scale replicaset <replicaset_name> --replicas=<desired_number_of_replicas> 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads