Basics on Kubernetes: What exactly is a ReplicaSet

In our previous article in series, basics on Kubernetes, we talked about what exactly are deployments, you can find the link of the article below. In this article, we are going to talk about what is ReplicaSet and how they help in scaling your application.

What are ReplicaSet and its objective?

ReplicaSet is an object in Kubernetes. The primary objective of replicaset is to define the number of replicas that will run for any type of pod. It is used to guarantee a stable number of pods always running.

What component uses ReplicaSet?

The deployment object of Kubernetes uses replicaset to maintain the number of pods defined in the spec. ReplicaSet makes sure the number of pods defined in deployment is running all the time.

Look at the below diagram of how deployment works with replicaset.

Basics on Kubernetes: What is deployment?Basics on Kubernetes: What is deployment?

How ReplicaSet marks the pod that it is maintaining?

ReplicaSet uses a selector to identify a new pod as a part of it. If the pod has no OwnerReference or Owner Reference is not a Controller and it matches repliaset’s selector, it will be identified as part of replicaset.

Should we use ReplicaSet?

Deployment provides an abstraction above replicaset, it is advisable to use deployments only. Deployment provides updates to pod, rolling updates, etc by managing the replicaset. You don’t need to touch replicaset objects, you only interact with deployments.

You may need to use replicaset only in case you are writing some custom update orchestration.

How you can create replicaset in Kubernetes?

Look at the yaml below. It will create a replicaset and launch pods for you.

## taken  from  kubernetes.io
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3

Submit the above yaml and run the below command to see the replicaset

kubectl get rs

NAME       DESIRED   CURRENT   READY   AGE
frontend   3         3         3       6s

You can read more about replicaset here on official documentation.

This is it for this article, if you like the article please share and subscribe.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.