Kubernetes Controllers
Tags: devops, kubernetes, go, computerscience
Published: 2022-10-29 06:00:00

This article will cover the introduction, basics, and usage of Kubernetes Cluster.

### Prerequisites ✅
- I am assuming that you understand the basics of a Kubernetes cluster like Pod, Deployments, ReplicaSets and more.
- You have worked with Kubernetes enough to understand the problem that controllers can helps us to solve.

Being said that. Let's get started!

# Introduction 💡
Kubernetes Controllers are quite simple. It is best understood by using the thermostat example from Kubernetes documentation [Here](https://kubernetes.io/docs/concepts/architecture/controller/#:~:text=When%20you%20set%20the%20temperature%2C%20that%27s%20telling%20the%20thermostat%20about%20your%20desired%20state.%20The%20actual%20room%20temperature%20is%20the%20current%20state.%20The%20thermostat%20acts%20to%20bring%20the%20current%20state%20closer%20to%20the%20desired%20state%2C%20by%20turning%20equipment%20on%20or%20off.). In a loose sense, we might say **Terraform** performs a similar function to the Kubernetes controller. It also detects drift and takes action to maintain the desired state. 

# Deep dive 🥽

- Each controller tracks an object(s) (one at the least).
- It monitors the current state of the object at all times.
- It takes action whenever it detects a difference between the desired state and the current state of the cluster. 
- Depending on the controller, it requests the Kubernetes API Server or the response system to make the required changes (to achieve the desired state).
- There are two main components of a controller: **SharedInformer** and **Workqueue**. 
- [SharedInformer](https://www.cncf.io/blog/2019/10/15/extend-kubernetes-via-a-shared-informer/) monitors events in the current state of Kubernetes objects. It sends events to Workqueue, where they are then put up by worker(s) to process.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/59uqk0mbv9he6c7vg64j.png)
> Image to illustrate the work of a controller.


In the easiest way, a controller observes the changes in a particular object(s) with the help of a Shared Informer. Since **Shared informers** send the updates on **Go channels**, the frequency and the weight of the metadata do not affect its speed. As soon as it detects drift from the desired state, it will command the required system (Kubernetes API server or other services) to do the required steps to get the desired state.
```
for {
  desired := getDesiredState()
  current := getCurrentState()
  makeChanges(desired, current)
}
```
> A code extract to explain about k8s controller. [Reference](https://docs.bitnami.com/tutorials/a-deep-dive-into-kubernetes-controllers/#controller-components:~:text=for%20%7B%0A%20%20desired%20%3A%3D%20getDesiredState()%0A%20%20current%20%3A%3D%20getCurrentState()%0A%20%20makeChanges(desired%2C%20current)%0A%7D)

# Example 👇🏻
I believe to understand any concept better an example is the best way to go. Although, Kubernetes provides a bunch of in-built controllers : [Here](https://github.com/kubernetes/kubernetes/tree/master/pkg/controller). We will focus our discussion to only one controller.

## Deployment controller
A **Deployment controller** is an in-built controller that is used to run any Pod with the desired number of replicas. There are multiple controllers in-built into Kubernetes.   All in-built controllers are managed under **Kube-Controller-Manager**. These are the main features of the Deployment 
controller:

- These Pods are entirely identical as they follow the PodTemplate. 
- It also allows specifying the deployment strategy (_RollingUpdate, Recreate_, and more). 
- You can upgrade the replicas with zero downtime or one after the other. 
- Rollback to the last state is easier with the controller. 
- It saves you from declaring three different manifests on **Pod Definition**, the number of **Replicas**, and the upgrade strategy.

# Custom Kubernetes Controllers 🔬

Whenever you talk about a developer tool, everyone loves to customise and build their own version. So yes it's possible to build your own controller if you want. I have not used many custom controllers yet.
You can look into this: [Here](https://youtu.be/sZt76yOpeTQ)

# References 📚

I will add links to most of the topics in case you want to read more on them: 
- Youtube Video on Controllers : [Here](https://youtu.be/gt_aMViZER8?list=LL)
- Youtube Video on Kinds of Controllers: [Here](https://youtu.be/N0jHRTADPLU)
- Must read in case of confusion: [Here](https://docs.bitnami.com/tutorials/a-deep-dive-into-kubernetes-controllers/#controller-components)
- Build custom controllers: [Here](https://youtu.be/sZt76yOpeTQ)

Thanks for reading my article :)
> P.S.: All these great images are built using [Hackerdraw](https://www.hackerdraw.com/)