Part 3 of Day 02 Replication Controller & Replica Set

Part 3 of Day 02 Replication Controller & Replica Set

Welcome to Part3 of Day 02 of the #5DaysOfKubernetes! Today, we dive into the world of Replication Controller and its more advanced counterpart, Replica Set. These powerful tools are essential for ensuring scalability and high availability in Kubernetes.

Before Kubernetes, other tools did not provide important and customized features like scaling and replication.

When Kubernetes was introduced, replication and scaling were the premium features that increased the popularity of this container orchestration tool.

Replication means that if the pod's desired state is set to 3 and whenever any pod fails, then with the help of replication, the new pod will be created as soon as possible. This will lead to a reduction in the downtime of the application.

Scaling means if the load becomes increases on the application, then Kubernetes increases the number of pods according to the load on the application.

Replication Controller is an object in Kubernetes that was introduced in v1 of Kubernetes which helps to meet the desired state of the Kubernetes cluster from the current state. Replication Controller works on equality-based controllers only.

ReplicaSet is an object in Kubernetes, and it is an advanced version of ReplicationController. ReplicaSet works on both equality-based controllers and set-based controllers.

Let’s do some hands-on to get a better understanding of ReplicationController & ReplicaSet.

YML file

apiVersion: v1
kind: ReplicationController
metadata:
  name: myreplica
spec:
  replicas: 2
  selector:
    Location: India
  template:
    metadata:
      name: testpod6
      labels:
        Location: India
    spec:
     containers:
       - name: c00
         image: ubuntu
         command: ["/bin/bash", "-c", "while true; do echo ReplicationController Example; sleep 5 ; done"]

Create the replication controller by running the command

kubectl apply -f myrc.yml

Now, you can see the replication controller that we created earlier and observe the desired state, current state, ready, and age.

If you list all the pods, you will see that my replica created two pods that are running.

If you try to delete the pods, you will see that the new pod will be created quickly. You can observe through the AGE of both pods.

If you want to modify the replicas, you can do that by running the command

kubectl scale — replicas=5 rc -l Location=India

If you try to delete pods, you will see again that the new pod is creating quickly.

Now, if you want to delete all the pods. You can do it by just deleting the replicationController by the given command.

kubectl delete -f myrc.yml

ReplicaSet HandsOn

YML file

apiVersion: apps/v1                            
kind: ReplicaSet    
metadata:
  name: myrs
spec:
  replicas: 2  
  selector:                  
    matchExpressions:                             
      - {key: Location, operator: In, values: [India, US, Russia]}
      - {key: env, operator: NotIn, values: [testing]}
  template:      
    metadata:
      name: testpod7
      labels:              
        Location: Russia
    spec:
     containers:
       - name: container1
         image: ubuntu
         command: ["/bin/bash", "-c", "while true; do echo ReplicaSet Example; sleep 5 ; done"]

Create the replicaSet by running the command

kubectl apply -f myrs.yml

Now, you can see the pods that have been created through replicaSet with labels

If you try to delete the pods, you will see that the new pod is created with the same configuration.

If you want to increase or decrease the number of replicas, you can do it by the given command.

kubectl scale — replicas=5 rs myrs

If you want to delete all the pods, you can do it by deleting the replicaSet with the help of the given command.

kubectl delete -f myrs.yml

Conclusion

ReplicationController and ReplicaSet are essential tools in Kubernetes for achieving high availability and scalability. They provide the foundation for ensuring your applications run smoothly, even in the face of failures and increased demand.

In our hands-on exercises, you’ve witnessed their power to maintain the desired state of your pods and adapt to changing requirements seamlessly.