Kubernetes hands on series: ReplicaSets and Replication Controllers

Rakesh Jain
4 min readAug 26, 2020

--

What is a Replica and Why do we need a replication controller?

Replica Set ensures how many replica of pod should be running.

To understand this in an easier manner lets take an example ->
Lets assume we are running our application on a single pod. What if for some reason our application crashes and the pod fails. Users will no longer be able to access our application.

To prevent users from losing access to our application we would like to have more than one instance or pod running at the same time. That way if one failed we still have our application running on the other one.

The replication controller helps us run multiple instances of a single pod in the Kubernetes cluster.

Thus providing high availability.
so does that mean you can’t use a replication controller if you plan to have a single pod!

No, You can use it. Even if you have a single pod the replication controller can help by automatically bringing up a new pod when the existing one fails.

Thus the replication controller ensures that the specified number of pods are running at all times.

Another reason we need a replication controller is to create multiple pods to share the load across them. Simple scenario is if we have a single pod serving a set of users, when the number of users increase we deploy additional pod to balance the load across the two pods if the demand further increases and if we were to run out of resources on the first node we could deploy additional pods across the other nodes in the cluster.

As you can see the replication controller spans across multiple nodes in the cluster.

It helps us balance the load across multiple pods on different nodes as well as scale our application when the demand increases.

It’s important to note that there are two similar terms replication controller and ReplicaSet.

Difference between ReplicaSet and Replication Controller

  • Replication Controller is the older technology that is being replaced by a ReplicaSet.
  • ReplicaSet is the new way to setup replication.

Creating a Replication Controller

Replication Controller Definition File

https://gist.github.com/TroubleshooteRJ/04d030d01417ce320616752539426639

Creating a ReplicaSet

ReplicaSet Definition File

https://gist.github.com/TroubleshooteRJ/3196245fd10b4e86fa6973d9468a16cb

ReplicaSet requires a selector definition when compare to Replication Controller.

There is one major difference between replication controller and replica set. ReplicaSet requires a selector definition. The selector section helps the replica set identify what pods fall under it.

But why would you have to specify what pods fall under it if we have provided the contents of the pod definition file itself in the template ?
It’s because replicas set can also manage pods that were not created as part of the replica at creation.

The matchLabels selector simply matches the labels specified under it to the labels on the pods.

There could be hundreds of other pods in the cluster running different applications.

This is where labeling our pods during creation comes in handy we could now provide these labels as a filter for replica set under the selector section we use to match labels filter and provide the same label that we used while creating the pods.

This way the replica set knows which pods to monitor. The same concept of labels and selectors is used in many other places throughout Kubernetes.

How to scale ReplicaSet

There are multiple ways to scale ReplicaSet

  • First way is to update the number of replicas in the
    replicaset-definition.yaml file. E.g replicas: 6 and then run
  • Second way is to use kubectl scale command with type and name.
  • Third way is to use kubectl scale command.

That’s all for this particular topic.

Hope you like the tutorial. Please let me know your feedback in the response section.

Happy Learning!

--

--

Rakesh Jain
Rakesh Jain

Written by Rakesh Jain

DevOps Professional | Technical writer

No responses yet