Kubernetes – ReplicaSet object – simplifying concepts

KuberNetes - Sample YAML file for ReplicaSet
KuberNetes - Sample YAML file for ReplicaSet

Hi All,

Greetings for the day!!!

We are continuing simplifying Kubernetes concepts. Today we will do one more Kubernetes object – ReplicaSet

Takeaway from this article :

  • What is Replica
  • What is ReplicaSet
  • Why we need ReplicaSet
  • Yaml specification for ReplicaSet – Creating a Replica Set
  • kubectl ReplicaSet CMDLETs

What is Replica

  • Replicas are the number of instances of PODs running in Kubernetes cluster at a single time

What is ReplicaSet

  • ReplicaSet is one of the Kubernetes object. What are Kubernetes object – please refer our previous article – Kubernetes – Objects – simplifying concepts
  • Replica sets are the new way to maintain the replicas of POD
  • Replica sets are replacement of Replication Controller
  • Replication controller is the older technology
  • Replica Set is the new recommended way to set up replication
  • Replica Set and Replication Controller provide same set of functionality but replica set is more powerful

Why we need ReplicaSet

  • ReplicaSet is required to maintain the required number of PODs and in healthy state
  • In case any POD fails, ReplicaSet instantiate new POD and make sure number of replicas of PODs are maintained as per the specification
  • Even there is single POD which fails, ReplicaSet makes sure to new POD instantiated and application is accessible
  • To create new POD ReplicaSet uses POD template

YAML specification for ReplicaSet

apiVersion: apps/v1 # api version
kind: ReplicaSet    # Kubernetes object which we are creating
metadata:
  name: replica-set name
  labels:
    app: app-name
    tier: tier-name 
spec:
  # number of replicas
  replicas: 3
  selector:
    matchLabels:
      app: app-name
      tier: tier-name
  template: #POD specification
    metadata:
      labels:
        app: app-name
        tier: tier-name
    spec: #spce section should be similar to spec in a pod definition
      containers:
        - name: container-name
          image: image used for container

  • Following are the details for above ReplicaSet YAML specification
    • First spec is for ReplicaSet – how it should be
    • No of replicas will be optional. If we didn’t specify any number by default 1 pod will be deployed
    • Second spec is for containers
    • Labels specified in metadata section must match with labels specified in selector section. Otherwise Kubernetes wont allow to create it

kubectl ReplicaSet CMDLETs

  • Creating ReplicaSet object
    • Sample YAML specification for ReplicaSet

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: knowledgejunction-replicaset
  labels:
    app: knowledgejunction-app
    tier: knowledgejunction-tier
spec:
  replicas: 3
  selector:
    matchLabels:
      app: knowledgejunction-app
      tier: knowledgejunction-tier
  template:
    metadata:
      labels:
        app: knowledgejunction-app
        tier: knowledgejunction-tier
    spec:
      containers:
        - name: knowledgejunction
          image: nginx:1.14.2

KuberNetes - Sample YAML file for ReplicaSet
fig : KuberNetes – Sample YAML file for ReplicaSet

  • Creating Replica Set with given yaml specification file using – kubectl create CMDLET


C:\>kubectl create -f "C:\Prasham\Articles\Kubernetes\Imperative CMDLETS\YAML files\knowledgejunction_replicaset.yaml"

Output will be

replicaset.apps/knowledgejunction-replicaset created

KuberNetes - Crating ReplicaSet - using kubectl create -f ReplicaSet.yaml
fig : KuberNetes – Crating ReplicaSet
  • We could see our ReplicaSet using – kubectl get CMDLET
kubectl get ReplicaSets

Listing kubernetes object using - kubectl get ReplicaSets
fig : KuberNetes – ReplicaSet created successfully

Best Practice

  • Mostly ReplicaSets are not getting created directly
  • We create ReplicaSets generally through Deployment object

Thanks for reading the article !!! Please feel free to discuss in case any issues / suggestions / thoughts / questions

HAVE A GREAT TIME AHEAD !!!

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

Leave a Reply

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

%d bloggers like this: