Kubernetes Notes - Controller

Kubernetes is designed to keep applications running even when failures occur. One of the key mechanisms that enables this is the Controller.

Controllers continuously monitor the cluster and ensure that the actual state matches the desired state. If something goes wrong, Kubernetes automatically corrects it.

This behavior is commonly referred to as self-healing.

It performs three main tasks:

  • Observe the current state
  • Compare it with the desired state
  • Take action if they differ

This loop runs continuously inside the Kubernetes control plane.

Table of Contents

Replicaset

A replicaset ensures that a specific number of pod replicas are always running.

Architecture

1ReplicaSet
23Pods

Deploy

To better demonstrate controller let us deploy a replicaset.

replica.yaml

 1apiVersion: apps/v1
 2kind: ReplicaSet
 3metadata:
 4  name: nginx-rs
 5spec:
 6  replicas: 3
 7  selector:
 8    matchLabels:
 9      app: nginx
10  template:
11    metadata:
12      labels:
13        app: nginx
14    spec:
15      containers:
16      - name: nginx
17        image: nginx

Deploy and verify if pods are running.

1kubectl apply -f replica.yaml -n demo
2
3kubectl get pods -n demo
4NAME               READY   STATUS    RESTARTS   AGE
5nginx-rs-4mbdt     1/1     Running   0          23s
6nginx-rs-cn55j     1/1     Running   0          23s
7nginx-rs-kdw67     1/1     Running   0          23s

We can see here that three replica pods are created.

Self-Healing

One of Kubernetes most powerful features is its ability to automatically recover from failures. From the example earlier, we will delete a pod.

1kubectl delete pod nginx-rs-4mbdt -n demo
2pod "nginx-rs-4mbdt" deleted from demo namespace
3➜  k8s-demo kubectl get pods -n demo
4NAME               READY   STATUS    RESTARTS   AGE
5nginx-rs-cn55j     1/1     Running   0          2m53s
6nginx-rs-kdw67     1/1     Running   0          2m53s
7nginx-rs-stwqh     1/1     Running   0          5s

If the pod is managed by a controller, Kubernetes will automatically recreate a new pod to replace it. Because the desired state we specify is that three replicas must always be running. The controller detects the mismatch and restores the system.

Scaling

Replicaset can be scaled by editing your YAML file replicas: 3 or by using kubectl.

1kubectl scale --replica=5 replicaset/nginx-rs -n demo
2replicaset.apps/nginx-rs scaled

Check latest update.

1kubectl get pods -n demo
2NAME             READY   STATUS    RESTARTS   AGE
3nginx-rs-657r5   1/1     Running   0          35s
4nginx-rs-cn55j   1/1     Running   0          13m
5nginx-rs-kdw67   1/1     Running   0          13m
6nginx-rs-stwqh   1/1     Running   0          10m
7nginx-rs-t974q   1/1     Running   0          35s

Now Kubernetes ensures five pods always exist.

Advance Controller

Replicaset has a lot of limitation, it cannot handle rolling update, rollout and revision history. That is why this is rarely being used in production environment. Deployment, Statefulset, Daemonset and Job solves these limitation. Check the next part discussing more about these controllers.