Originally published on Cloud Native Now


As more businesses shift toward microservices, Kubernetes is turning into a go-to tool for handling the nitty-gritty of today’s IT world, and its API plays a pivotal role. Think of the K8s API as the control hub, making the management of the Kubernetes cluster a breeze and letting users spell out how they want their apps and infrastructure to look and act.

Kubernetes controllers are the unsung heroes, constantly working to ensure the system’s actual state matches the user’s needs. Here, the operator pattern is a real game changer, showing how flexible and user-focused Kubernetes can be.

The operator pattern was created to meet the varied needs of businesses and devs and enables Kubernetes to do even more, including managing custom resources it wasn’t originally built for. This means Kubernetes can be customized to handle all sorts of systems, proving it’s ready to adapt to whatever the tech world throws at it. Kubernetes really stands out when you consider the cool features and extensions the operator pattern brings to the table, offering solid answers to today’s tech hurdles.

Kubernetes-Native Controllers

By operating on the principle of desired state management, Kubernetes allows users to dictate their system’s configuration through a centralized control plane, which serves as the decision-making and monitoring hub. At the heart of this control plane are the Kubernetes-native controllers, purpose-built to manage specific resources within the ecosystem. These controllers continuously monitor their respective resources and ensure the system’s current state aligns seamlessly with the user-defined desired state; controllers automatically make necessary adjustments to maintain this balance.

For instance, consider the deployment controller. When you deploy an application in Kubernetes using a Deployment, this controller jumps into action. It ensures that the specified number of replicas of your application is maintained. If a pod crashes or becomes unresponsive, the deployment controller will recognize the discrepancy and initiate the creation of a new pod to maintain the desired state.

Similarly, the ReplicaSet controller maintains the correct number of pod replicas. It’s closely related to the deployment controller but operates at a slightly lower level, focusing specifically on pod replicas without the additional features that deployments offer.

These are just two examples, but Kubernetes boasts a plethora of native controllers, each tailored for specific tasks, like managing services, volumes or network policies. Together, they contribute to Kubernetes’ reliability and resilience, ensuring that your applications and infrastructure run smoothly and consistently.

Custom Controller Use Case: Tracking New Volumes

In my journey through the dynamic world of Kubernetes, I’ve found myself in situations where the built-in controllers couldn’t meet specific needs that cropped up. That’s when I realized the true power of custom controllers.

I worked in a large-scale organization where we were constantly deploying and scaling storage volumes. It became evident that we needed an efficient system to keep track of these deployments. I imagined how great it would be if I could receive a Slack notification every time a new storage volume was deployed. In addition, the volumes would be annotated automatically for monitoring systems without human intervention. While Kubernetes doesn’t offer these features natively, I figured out that a custom controller could be the perfect solution to bridge this gap.

So, I mapped out a workflow for the controller to handle this scenario, which looked something like this:

  • A new storage volume gets deployed in the Kubernetes cluster.
  • The custom controller, which I designed to keep an eye on storage volumes, spots this new deployment.
  • The controller reacts to this by triggering a predefined action—in this case, shooting off a notification to a Slack channel and annotating volumes for monitoring.
  • My team received the Slack notification, giving me a heads-up about the new volume deployment.
  • Armed with this info, I could quickly gauge whether the new storage was vital for a particular application or if we needed to make some tweaks. This hands-on experience highlighted the versatility and adaptability of custom controllers. I was able to tailor Kubernetes to my specific needs, ensuring seamless integration with the other tools and platforms I relied on while maintaining the system in its desired state. It turned out to be a practical solution and helped to streamline operations and keep everything running smoothly.

Kubernetes-Native Way: kubebuilder

Kubernetes boasts a rich ecosystem that not only allows for the creation of custom controllers but also offers tools to facilitate this process. A standout tool in this realm is kubebuilder, a scaffolding framework designed to construct Kubernetes APIs and controllers. This tool greatly simplifies the task of integrating custom resources and logic into Kubernetes.

The preference for kubebuilder over custom scripts stems from several of its advantages:

It provides a structured project layout, streamlining the development and maintenance of controllers and custom resources. It autogenerates much of the repetitive code essential for setting up controllers and APIs. It integrates seamlessly with Kustomize for configuration customization and is backed by thorough documentation to guide developers through its functionalities.

Walkthrough: Creating a Controller With kubebuilder

To get going on creating a controller with Kubebuiler, I highly recommend The Kubebuilder Book. It provides a comprehensive walkthrough of creating a container, covering all the steps and components. Even for the relative experts out there, this guide is worth looking into to further sharpen your skills.

Advantages of Controllers

Kubernetes controllers, both inherent and custom-made, serve as the foundational pillars of the Kubernetes ecosystem. Acting as silent custodians, they ensure the cluster’s current state consistently mirrors the user’s desired specifications. These controllers offer a multitude of benefits:

  • High availability: Controllers are integral to Kubernetes’ promise of high availability. For instance, in the context of tracking newly created volumes, having a controller that notifies the team immediately ensures that any issues can be addressed promptly, maintaining the high availability of the volumes. This self-recovery feature ensures applications remain robust against failures.
  • Versatility: Controllers in Kubernetes are designed to cater to diverse needs. Leveraging them in tracking volume creations showcases their versatility in adapting to different operational needs, including batch jobs, stateful services or daemon processes. This allows Kubernetes to manage varied workloads effectively.
  • Appropriate permissions: By prioritizing security, controllers operate on a least-privilege principle. They possess only the essential permissions needed for their tasks, reducing potential security threats and limiting the impact of any compromised component.
  • Resource optimization: Beyond state maintenance, controllers emphasize efficiency. In the scenario of tracking volumes, it aids in resource optimization by providing real-time updates, facilitating immediate actions to optimize resources based on the current state and guaranteeing cost efficiency.
  • Extensibility: Kubernetes’ flexibility is evident in its support for custom controllers, allowing users to address unique needs beyond the capabilities of native controllers. This adaptability ensures Kubernetes stays relevant to changing business needs. For example, in the case of tracking volumes, it has extended its functionality to integrate seamlessly with tools like Slack, enhancing operational efficiency and responsiveness.

Conclusion

Controllers aren’t just a component of Kubernetes; they’re its lifeblood, ensuring that applications remain available, resilient and efficient. They are essentially the Ops engineers’ method of introducing automation to K8s in an elegant and resilient way and extending their capabilities. In most scenarios, controllers prove to be the optimal way to interact with clusters, outshining scripts and manual interventions. Controllers’ automated, continuous monitoring and action loops mean the system can stay in its desired state without constant human oversight.

It’s also worth delving deeper into how controllers aid in extending the system. The term “operator” defines a set of controllers and custom resource definitions (CRDs), which are—in essence—custom resources. I’ve touched upon this concept briefly in this article. Still, it’s fundamental to understand that operators allow for creating custom, application-specific controllers, thereby enhancing the extensibility of Kubernetes.

So, as you navigate your Kubernetes journey, remember the pivotal role of controllers and consider crafting your own. With the extensibility features of Kubernetes—especially through the use of operators—you stand to gain even more from the cloud-native infrastructure and ecosystem.