# Create, View and Destroy a Replica Set

In 
Published 2022-12-03

This tutorial explains to you how to create, view and destroy a Replica Set in a Kubernetes cluster.

# Replica Set - explanation

When you deploy pods there is no mechanism to monitor that pod. If the pod fails it will not be created again.

A Replica Set is a layer of abstraction over pods. When a Replica Set is created a Controller monitors the pods to ensure that we have the desired number of pods running on Kubernetes cluster.

A Replica Set has a selector which identifies the pods it takes care of. If you have a Pod with label matching the ReplicaSet label, ReplicaSet will take control over the pod.

For instance, if you deploy Replica Set with 3 replicas and Pod with labels matching the Replica Set labels was deployed before that, then RS will spawn only 2 Pods with the matching labels.

ReplicaSet helps perform load balancing between multiple instances of the pod.

# Create a Replica Set in a Kubernetes cluster

In order to create a Replica Set, we need to create a file as my-first-replica-set.yml which describes the Replica Set we want to create:

my-first-replica-set.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app1
  template:
    metadata:
      labels:
        app: app1
    spec:
      containers:
      - name: my-container
        image: nginx:1.14.2

The following command will create a new Replica Set:

kubectl apply -f my-first-replica-set.yml

# View Replica Set information

We can run the following command to see the result:

kubectl get rs

NAME       DESIRED   CURRENT   READY   AGE
nginx-rs   2         2         2       2m47s

Get more information on a specific Replica Set:

kubectl describe rs nginx-rs

In my case the result is:

Name:         nginx-rs
Namespace:    default
Selector:     app=app1
Labels:       <none>
Annotations:  <none>
Replicas:     2 current / 2 desired
Pods Status:  2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=app1
  Containers:
   my-container:
    Image:        nginx:1.14.2
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  3m14s  replicaset-controller  Created pod: nginx-rs-v2gd2
  Normal  SuccessfulCreate  3m14s  replicaset-controller  Created pod: nginx-rs-fsf48

# Destroy a Replica Set in a Kubernetes cluster

Delete the Replica Set created above:

kubectl delete rs nginx-rs