Kubermatic branding element

Kubernetes ReplicaSet: An Introduction

Close-up view of a motherboard showcasing electronic components

In this part of our series, we are focusing on Kubernetes ReplicaSet. Just like the previous parts, there will be hand-on practice to allow you to get acquainted with the features and functionalities of a ReplicaSet which include using ReplicaSet to scale applications up or down. This Kubernetes object makes Kubernetes application management easier.

Kubernetes ReplicaSet

A ReplicaSet is a process that runs multiple instances of a Pod and keeps the specified number of Pods constant. Its purpose is to maintain the specified number of Pod instances running in a cluster at any given time to prevent users from losing access to their application when a Pod fails or is inaccessible.  

ReplicaSet helps bring up a new instance of a Pod when the existing one fails, scale it up when the running instances are not up to the specified number, and scale down or delete Pods if another instance with the same label is created. A ReplicaSet ensures that a specified number of Pod replicas are running continuously and helps with load-balancing in case of an increase in resource usage.

Creating a Kubernetes ReplicaSet

We will create an example ReplicaSet using the below configuration, just like we created a Pod in part 3 of this series. 

Before we begin, you should already have a running Kubernetes cluster and configured the kubectl command-line tool to communicate with the cluster. If you need guidance, you can find detailed instructions on getting a cluster running using KubeOne in this setup guide. Basic knowledge of Pod is suggested to follow the exercise.

Step 1: Create a YAML file using vim on the command line:

$vim replicaset-app.yaml

Step 2: Copy, paste, and save the configuration below into your YAML file. The indentation is crucial!

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx

 Step 3: Create the ReplicaSet with this command:

$kubectl create -f replicaset-app.yaml
replicaset.apps/my-replicaset created

Step 4: Once the ReplicatSet is running, you can check its status:

$kubectl get replicaset my-replicaset

NAME                 DESIRED    CURRENT    READY   	AGE
my-replicaset  	       2          2         2          17s

Name: ## This is the name of the ReplicaSet declared as the child of the metadata property 
Desired: ## This is the replica number specified in the YAML manifest file
Current: ## This equates to the current state of the ReplicaSet; that two replicas are up
Ready: ## The two replicas specified are ready and in the running state
Age: ## How long the replicas have been in the running state

Step 5: Next, you can check the status of the Pods running in the ReplicaSet:

$ kubectl get Pods

NAME                 	READY        STATUS    RESTARTS     AGE
my-replicaset-ccjfj   	 1/1         Running  	 0         5m18s
my-replicaset-m4bb7      1/1         Running  	 0         5m18s

Scaling Your Application:

An application can either be scaled up or down depending on the situation in two ways. Using the first method, you can edit the configuration file we created earlier in this exercise by changing the replicas property value to a higher number to scale up or a lower number to scale down.To scale up, edit the manifest file and change the replicas value from 2 to 5. Save and exit the vim terminal and then run the below command.

$ kubectl replace -f replicaset-app.yaml
replicaset.apps/my-replicaset replaced

To get the status:

$ kubectl get replicaset my-replicaset

NAME              DESIRED   CURRENT   READY   AGE
my-replicaset       5          5       5      12m

Next, you can check the status of the Pods running in the ReplicaSet:

$ kubectl get Pods

NAME                  	READY   STATUS    RESTARTS    AGE
my-replicaset-bq9wz      1/1    Running      0        15m
my-replicaset-c5h4x   	 1/1    Running      0        3m43s
my-replicaset-hj59z      1/1    Running      0        3m43s
my-replicaset-hpvxr      1/1    Running      0        15m
my-replicaset-xgwfx      1/1    Running      0        3m43s

As seen in the ReplicaSet and Pod statuses, there are 5 Replicas running and ready for use, as well as 5 Pods. To scale down, change the value of the replicas in the manifest file from 5 to 1, then run the command below again.

$ kubectl replace -f replicaset-app.yaml
replicaset.apps/my-replicaset replaced

Check the status of the ReplicaSet:

$ kubectl get replicaset my-replicaset

NAME            DESIRED   CURRENT   READY   AGE
my-replicaset     1         1        1      4m9s

Check the status of the Pod:

$ Kubectl get Pods

NAME                  	READY    STATUS    RESTARTS   AGE
my-replicaset-89dkw      1/1     Running      0       4m15s

You can see that the number of Replica and the running instance of a Pod are the same as the number specified in the manifest file replicas field.

An application can also be scaled up or down using the command line which is the second method.

$kubectl scale - -replicas=5 -f replicaset-app.yaml   ## Scale up
$kubectl scale - -replicas=1 -f replicaset-app.yaml  ## Scale down

Or

$kubectl scale - -replicas= 5 replicaset <replicaset name>   ## Scale up
$kubectl scale - -replicas= 1 replicaset <replicaset name>   ## Scale down

Summary

This guide has introduced you to how Kubernetes ReplicaSet can automate application lifecycle management for efficiency. Next in our series, we will look at Kubernetes Deployment, and its functionalities. We encourage you to contact us with any questions you have.    

Learn More

Seyi Ewegbemi

Seyi Ewegbemi

Student Worker