Controllers

Controllers are the brain behind kubernetes.
Controllers monitor the kubernetes objects and respond accordingly.

Replication Controller

When we have a single pod running our application, if for some reason our application crashes and pod fails, the 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.
In this case, if one fails, we still have our application running on the other one.

The replication controller helps us to run multiple instances of a single pod in the kubernetes cluster thus providing high availability.

Even we have a single pod, replication controller can help by automatically bringing up the new pod when the existing one fails.
Thus, the replication controller ensures that the specified number of pods are running at all time, even if it just 1 or 100.

Load Balancing & Scaling

Another reason to have replication controller is to create multiple pods to share the load across them.
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 run out of resources on the first node, we could deploy additional pods across the other nodes in the cluster.
The replication controller spans across multiple nodes in the cluster. It helps us to balance the load across multiple pods on different nodes as well as scale our application when the demand increases.

Replication Controller vs Replica set

  • Both have the same purpose but not the same.
  • Replication Controller is the older technology that is replaced by replica set.
  • Replica set is the new recommended way to set up replication.

Create a replication controller

rc-definition.yml

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: front-end
spec:
    # template section to provide a pod template to be used by the replication controller to create replicas.
  template:
    metadata:
      name: myapp-prod
      labels: 
        app: myapp
        type: front-end
    spec:
      containers:
        - name: nginx-container
          image: nginx

  replicas: 3

kubectl create -f <filename>
This creates the replication controller. When the replication controller is created, it first creates the pods as pod definition template as many as required(3 in this case).

kubectl get replicationcontroller
To view the list of created replication controllers.

kubectl get pods
To view the pods created by replication controller.

Replica Set

The role of the replica set is to monitor the pods and if any of them fails, it deploy new ones. The replicaset is the process that monitor the pods.

replicaset-definition.yml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: front-end
spec:
  template:
    metadata:
      name: myapp-prod
      labels: 
        app: myapp
        type: front-end
    spec:
      containers:
        - name: nginx-container
          image: nginx

  replicas: 3
  # selector section helps replicaset identify what pods fall under it.
  # We need to mention this (though we have provided pod template) because, replicaset can also manage pods which are not created as part of the replicaset creation.
  selector: 
    matchLabels:
      type: front-end

Selector is not required in the case of replication controller(still available, if not provided it assumes it to be the labels provided in the pod definition file), whereas a user input is required of this property in the case of replica set.

kubectl create -f <filename>
To create a replica set
kubectl create -f replicaset-definition.yml

kubectl get replicaset
To view the created replicasets

If we already have three pods created with matching labels on the kubernetes cluster and we need to select them as a replica set, still we need to provide the template property with the pod definition, as this pod definition will be used to build a new pod when any of the existing pods fails (to maintain the desired number of pods).

Scale

We started with 3 replicas and in future we decide to scale to 6. There are multiple ways to do this

  1. update the number of replicas in the definition file to 6. kubectl replace -f replicaset-definition.yml
    This will update the replicaset to have 6 replicas
  2. kubectl scale --replicas=6 -f replicaset-definition.yml or
    kubectl scale --replicas=6 replicaset myapp-replicaset (type, name format, this won't change the number of replicas in the file).

Commands

kubectl create -f replicaset-definition.yml
To create replicaset using replicaset-definition.yml file

kubectl get replicaset
To view the list replicasets created

kubectl delete replicaset myapp-replicaset
To delete the replicaset named myapp-replicaset.
This also deletes all underlying PODs.

kubectl replace -f replicaset-definition.yml
To replace or update the replicaset

kubectl scale --replicas=6 -f replicaset-definition.yml or
kubectl scale replicaset myapp-replicaset --replicas=6
To scale replicaset simply by command line without modifying the file.

kubectl edit replicaset myapp-replicaset
To view the running configuration of replicaset in a text format. This is not the actual file we created, this is a temporary file created by kubernetes to allow us to edit the configuration.
Changes made to this file are directly applied on the running configuration of the cluster as soon as we change the file.
This is mainly used to scale up or scale down.