DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Kubernetes Controllers
Pratik Singh
Pratik Singh

Posted on

Kubernetes Controllers

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. 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 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

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)
}
Enter fullscreen mode Exit fullscreen mode

A code extract to explain about k8s controller. Reference

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. 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

References πŸ“š

I will add links to most of the topics in case you want to read more on them:

  • Youtube Video on Controllers : Here
  • Youtube Video on Kinds of Controllers: Here
  • Must read in case of confusion: Here
  • Build custom controllers: Here

Thanks for reading my article :)

P.S.: All these great images are built using Hackerdraw

Top comments (0)

12 Gorgeous UI Components for Your Design Inspiration

>> Check out this classic DEV post <<