Kubernetes(k8s) - What are ReplicaSets?

A ReplicaSet (RS) is a Kubernetes object used to maintain a stable set of replicated pods running within a cluster at any given time.


kubernetes-k8s-what-are-replicasets

The main goal of kubernetes controllers is to define a desired state, by ensuring at any given moment, N pods of the same type are constantly running at all times. As such, RS are often used to guarantee the availability of a service. Kubernetes will automatically deploy extra pods to replace those that fail or become inaccessible, preventing users from losing access to an application. If we didn’t use ReplicaSets, we would have to build as many manifests as the number of pods we require, which would be a lot of work to do for each application.


A ReplicaSet has two main features: a pod template for creating new pods whenever existing ones fail, and a replica count for maintaining the desired number of replicas that the controller is supposed to keep running. A ReplicaSet also works to ensure additional pods are scaled down or deleted whenever an instance with the same label is created.


kubernetes-k8s-what-are-replicasets

As a result, it ensures that a specified number of replica pods are running continuously. A Kubernetes RS helps with load-balancing, reliability, and scaling, as follows:


Load balancing — Replication allows Kubernetes to have multiple instances of a pod. This means that traffic is sent to different instances, which prevents a single instance from being overloaded.


Reliability — Replication ensures that we have multiple instances of an application, which means that it won’t fail just because one of the containers fails.


Scaling — With Kubernetes, you can quickly scale your application up or down by adding or removing instances.



Creating ReplicaSets in Kubernetes

We can define ReplicaSets in Kubernetes using YAML files. Using these YAML files, we can create objects that interact with the Kubernetes API (Pods, Namespace, Deployments etc.). Under the hood, kubectl converts the information that we have defined in our YAML file to JSON, that makes the request to the Kubernetes API.


Let’s take a look at an example YAML definition for a Kubernetes ReplicaSets:

              
                ---
                                apiVersion: apps/v1
                            kind: ReplcaSet
                      metadata:
                               name: nginx-rs
                        labels:
                               app: nginx
                  spec:
                            replicas: 3
                          selector:
                                  matchLabels:
                                    app: nginx
                          template:
                              metadata:
                                labels:
                                                      app: nginx               
                          spec:
                                    containers:
                                      - name: nginx
                                         image: nginx
                                   ports:
                                                - containerPort: 80
              
              

Let’s take a look at following fields:


selector — Used to identify which pods this ReplicaSet is responsible for.


replicas — Indicates the number of pods the ReplicaSet should maintain.


template — Defines the pod template that the ReplicaSet uses when creating pods and adding them to meet the required number of replicas.


apiVersion — Defines the Kubernetes API that supports the resource ReplicaSet. Every API supports certain resources and together with Kind we can unequivocally define them.


kind — Defines the resource as a ReplicaSet for the Kubernetes API



kubectl - deploy and interact with Replicasets

kubectl, (kube-control, or as some people call it, kube-cuddle) is the Kubernetes command-line tool. It allows us to run commands against Kubernetes clusters.


With kubectl, we can create a Pod using our YAML definition file like so:


kubectl apply -f myyamlfile.yaml

We can list all of our Replicasets like so:


kubectl get rs



Summary and Conclusions


Kubernetes ReplicaSets to automate application lifecycle management to increase productivity. In order to make full use of this knowledge in your Kubernetes clusters, you must evaluate the needs of your pods so that you can select the optimum resource with the best functionality for your use case.



Author


sagar-mehta

Sagar Mehta is Atgen Software Solutions Founder and a recognised expert in the field of Intelligent Automation, including Robotic Process Automation, Workload Automation, DevOps, SRE and Advanced Analytics. Sagar advocates a pragmatic approach to Automation, encouraging a policy of using ‘the best tool for the job’.


Prior to co-founding Atgen Software Solutions, Sagar worked in Senior Automation roles, architecting and delivering robust, scalable solutions for many of the world’s biggest banks and working with leading Automation vendors. He developed his first automated solution in 2006 and has continued to deliver robust, scalable and sophisticated Automation ever since.


Sagar is a regular guest speaker and panellist at Automation seminars, conferences and user group events.

Contact

Have a similar problem to solve, let's work together.

Our Address

#107, Tower B, Escon Arena, Zirakpur, Punjab, India - 140603

Email Us

info@atgensoft.com

Call Us

+91-8806666141