06 | Replication Controller in Kubernetes

What is Replication Controller?

With Replication Controller, you specify how many numbers of specific Pod you want to keep running. And rest it will ensure that only those many numbers of Pods will run.

clone multiply GIF by Kandu

It constantly monitors the list of running pods and makes sure the actual number of pods of a “type” always matches the desired number. If too few such pods are running, it creates new replicas from a pod template. If too many such pods are running, it removes the excess replicas.

Note that Replication Controller is old and Replica Set is replacing it. Currently both are being supported. This chapter will cover Replication Controller and in next chapter we will talk about Replica Set.

Can I create Replication controller with value 1 for a POD?

Yes.

Yes GIF by Brittany Broski

Replication Controller ensure that single pod will keep running, and if it goes down, it will bring it up.

How to create ReplicationController?

It has three important parts:

  • A label selector, which determines what pods are in the ReplicationController’s scope
  • A replica count, which specifies the desired number of pods that should be running
  • A pod template, which is used when creating new pod replicas

Below is definition file. 

apiVersion: v1
 kind: ReplicationController
 metadata:
    name: thinkscholar-rc
 spec:
    replicas: 3
    selector:
        app: thinkscholar
 template:
    metadata:
        labels:
             app: thinkscholar
    spec:
         containers:
        - name: thinkscholar
           image: web/thinkscholar
           ports:
           - containerPort: 8080

Command:

kubectl create -f rc-definition.yml

Now check if this worked by checking Pods:

$ kubectl get pods
 NAME                 READY     STATUS              RESTARTS   AGE
 thinkscholar-53thy   0/1       ContainerCreating   0          2s
 thinkscholar-k0xz6   0/1       ContainerCreating   0          2s
 thinkscholar-q3vkg   0/1       ContainerCreating   0          2s

Once we create a replication controller using above definition file, the Replication Controller will ensure that three instances of Pod which matches the label (app=thinkscholar) are always running. If the number of Pods with these labels go up or down, ReplicationController will bring back the pod count to 3.

What if I manually delete a POD?

bored d&d GIF by Hyper RPG

If you delete the pod and run command to get pods again:

$ kubectl delete pod thinkscholar-53thy

Now check the pods running:

$ kubectl get pods
 NAME                 READY     STATUS              RESTARTS   AGE
 thinkscholar-53thy   1/1       Terminating         0          3m
 thinkscholar-oini2   0/1       ContainerCreating   0          2s
 thinkscholar-k0xz6   1/1       Running             0          3m
 thinkscholar-q3vkg   1/1       Running             0          3m

Listing the pods again shows four of them, because the one you deleted is terminating, and a new pod has already been created.

What will happen if the label selector is not mentioned in ReplicationController template?

Louis Litt Genius GIF by Suits

Not specifying the selector at all is also an option. In that case, it will be configured automatically from the labels in the pod template.

How does replication controller count number of specific Pod that are running which it is responsible for? 

Replication-Controllers checks pods that match a certain label selector.

A ReplicationController’s job is to make sure that an exact number of pods always match its label selector. 

If You Say So Ok GIF by Bounce

How to get Replication Controller resources created?

Command:

kubectl get rc

You can also use: kubectl get replicationcontroller or kubectl get replicationcontrollers

Output:

NAME                DESIRED   CURRENT   READY     AGE
thinkscholar-rc     3         3         2         3m

You see three columns showing the desired number of pods, the actual number of pods, and how many of them are ready.

How to get detailed information about a specific Replication Controller ?

Command:

kubectl describe rc <replication-controller-name>

Output:

 Name:           thinkscholar-rc
 Namespace:      default
 Selector:       app=thinkscholar
 Labels:         app=thinkscholar
 Annotations:    <none>
 Replicas:       3 current / 3 desired               
 Pods Status:    4 Running / 0 Waiting / 0 Succeeded / 0 Failed  
 Pod Template:
   Labels:       app=thsch
   Containers:   ...
 Events:                 
 From                    Type     Reason            Message
 ----                    -------  ------            -------
 replication-controller  Normal   SuccessfulCreate  Created pod: thinkscholar-53thy
 replication-controller  Normal   SuccessfulCreate  Created pod: thinkscholar-k0xz6
 replication-controller  Normal   SuccessfulCreate  Created pod: thinkscholar-q3vkg

What will happen if I change the Label of running pod?

Good Mythical Morning Question GIF by Rhett and Link

If you change the label of the running pod, and that old label is part of the label selector of any ReplicationController, then Pod will move out of that ReplicationController. And the replication controller will start a new pod, to match the replicas count.

If a new label being added, is configured for a different replication controller, then the other ReplicationController will also take necessary action to meet its replicas count.

How to change existing label value for a pod ?

Command:

kubectl label pod <pod-name> <new-label> --overwrite

Output:

$ kubectl label pod thinkscholar-dmdck app=thinks --overwrite

The –overwrite argument is necessary; otherwise, kubectl will only print out a warning and won’t change the label.

Can we change the label selector of running ReplicationController?

NO

castle no GIF

How to edit a specific replication controller?

Command:

kubectl edit rc <replication-controller-name>

This command will open the ReplicationController YAML definition in your default text editor. After you save your changes and exit the editor, kubectl will automatically update the ReplicationController

How to update the replicas value for a specific replication controller configured in Kubernetes ?

Command:

kubectl scale rc <replication-controller-name> --replicas=10

How to delete a specific replication controller ?

Command:

kubectl delete rc <replication-controller-name>

Note: on deleting ReplicationController, the pods are also deleted.

How to delete ReplicationController without deleting related pods ?

Command:

kubectl delete rc <replication-controller-name> --cascade=false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s