Kubernetes Operators: Pioneering Modern Architectural Automation and Management

Beyond Automation: Unleashing Software Autonomy with Kubernetes Operators

Ashok Gudise
CloudX at Fidelity
9 min readMar 12, 2024

--

Discover the revolutionary operator pattern and its transformative impact on software autonomy, explored through the lens of Kubernetes Operators. Your guide to a self-managed software landscape.

Picture yourself in a bustling software amusement park where applications whiz by on roller coaster, databases play a merry tune on the Ferris wheel, and containers are like the colorful balloons floating above. It’s a “Carnival of Code,” and you’re in charge of making sure the show goes on smoothly.

Understanding the Kubernetes Operator Pattern: Empowering Software to Run Itself

In the vast realm of software, the Kubernetes (K8s) Operator Pattern stands as a beacon of transformation. Imagine software that isn’t just a passive tool, but an active partner. The K8s pattern equips applications with a remarkable ability; it enables them to understand their own needs and make adjustments without human intervention. Think of it as a software butler, always at the ready to ensure everything runs smoothly.

K8s Pattern Overview

The K8s pattern sets the stage for a future where software takes care of its own maintenance and fine-tuning, reducing the manual workload and making your digital world smarter and more self-reliant. As we delve into the intriguing world of K8s Operators, you’ll discover how to put this pattern concept into practice, revolutionizing the way you manage and orchestrate applications.

K8s Operator Components

K8s Operators consist of three key components, each playing a crucial role in their functionality. Let’s take a closer look at each of these components.

Key Components of K8s Operator

Custom Resource Definitions

Custom Resource Definitions (CRDs) are custom resource types that Operators use to extend a Kubernetes-native API. These definitions allow you to define custom objects specific to your application or workload. CRDs define the desired state, behavior, and parameter for your application, making it easier to manage complex applications in a Kubernetes-native way.

Controller

At the heart of the K8s Operator is a controller which continuously monitors and reconciles the state of CRDs within a Kubernetes cluster. Its primary responsibility is to watch for the changes in CRDs and take appropriate actions to ensure that the desired state, as defined in the CRDs, is maintained.

Operator Logic

The Operator Logic is the set of automated procedures, workflows, and decision-making processes that the Operator follows to ensure that the application aligns with its desired state as defined in the CRDs. It encapsulates the domain-specific knowledge and operational expertise required to manage a particular application. This is where the “magic” of automation happens, enabling complex application management with ease and reliability in Kubernetes.

What exactly is the K8s Operator?

Kubernetes Operators are a powerful pattern for managing applications and their components in a more automated and declarative manner. They extend Kubernetes capabilities by capturing domain-specific knowledge and operational procedures into custom resources which can be managed by Kubernetes itself. This means you can automate complex, Day 2 operations for your applications such as scaling, backup, and updates, in a Kubernetes-native way.

Operator Functionality

Where did it all start?

Kubernetes Operators were initially conceived and developed by CoreOS, a container-focused technology company. CoreOS introduced the concept of Operators to the Kubernetes community as an open-source project, and it gained significant traction due to its potential to address the challenges of managing complex, stateful applications. Today, Kubernetes Operators are a pivotal component of the Kubernetes ecosystem for their ability to automate and streamline application management in Kubernetes environments.

Why do we need it?

Operators are particularly beneficial for stateful applications like databases, message queues, and monitoring systems where manual intervention and ad-hoc scripts are often needed to handle operations effectively. With Operators, you can encode best practices and operational expertise into your Kubernetes deployments which leads to increased consistency, reliability, and reduced operational overhead.

What does the overall flow look like?

External or internal systems trigger changes in an Operator by sending events or requests that pertain to the application or workload managed by the Operator. These triggers can be anything from user-initiated actions to monitoring alerts to changes in external data sources. Once the Operator receives a trigger, it interacts with the Kubernetes API server to create, update, or delete resources based on the CRD specific to the application.

Operator Flow of Events

The Operator’s intelligence lies in its ability to interpret these events, to apply domain-specific logic, and to communicate with the API server to ensure that the application aligns with the desired state defined in the CRDs. The operator flow enables the automation and management of complex applications within a Kubernetes environment, resulting in enhanced efficiency and reliability.

Understanding the Process of State Reconciliation

Reconciliation of a CRD state is the continuous process by which the K8s Operator ensures that the current state of a CRD resource aligns with its desired state as defined in the CRD specification. The Operator’s controller monitors for any discrepancies or changes and, if necessary, takes corrective actions to bring the resource back in line with the desired state. This automatic, self-healing mechanism is fundamental to maintaining the stability and integrity of applications managed by K8s Operators.

Reconciliation of CRD State

Capabilities of K8s Operators

Leveraging K8s Operators delivers a multitude of benefits that can significantly enhance your infrastructure management. Like an autopilot for your operations, K8s Operators bring automation to your infrastructure which results in reducing errors and providing valuable analytics. In addition, Operators help you achieve consistency in deployment, operational knowledge, and maintaining a reliable state.

K8s Operators Capabilities

They bring scalability based on predefined metrics and events, allowing for elasticity in your system. And finally, they simplify day-2 operations including updates, migrations, automated backups, and handling operational complexities with ease. Whether you are looking to enhance efficiency, maintain a reliable state, or achieve seamless scalability, K8s Operators are a valuable tool for modern infrastructure management.

How to Create an Operator

Building a K8s Operator involves several essential components and steps as follows:

  1. To streamline development, choose from various frameworks such as Operator SDK or kubebuilder.
  2. Define the CRD that represents your application’s desired state.
  3. Implement the Operator logic which includes event handling, reconciliation, and any custom automation required for your application.
  4. Containerize it and create a K8s deployment to deploy it within your cluster.
  5. Finally, deploy the CRD instance to interact with your Operator, enabling the automation of application management.

Let’s delve deeper into the process of creating Operators

Implementing K8s Operators involves considering various options, such as selecting the appropriate method based on available Software Development Kits (SDKs), Ansible, or Golang. Each approach offers distinct advantages.

First, choosing a supported library based on available SDKs can simplify development, particularly if you’re already familiar with a specific programming language or ecosystem. These libraries often provide pre-built components for common Operator tasks, making it easier to get started. However, their scope may be somewhat limited compared to Golang-based Operators.

On the other hand, Golang-based Operators provide unparalleled control and flexibility, making them suitable for complex and customized scenarios. They empower developers to create robust and efficient Operators from scratch. This approach is ideal when your needs go beyond what existing SDKs can offer.

Ansible-based Operators are a good choice when you want to leverage existing Ansible playbooks and roles. They enable the reuse of Ansible automation across Kubernetes, simplifying Operator development if you have a substantial investment in Ansible automation.

Coverage by Operator Type

Ultimately, the choice between these methods depends on your specific use case and requirements. If you need quick and easy Operator development with limited customization, SDK-based libraries may suffice. If you require fine-grained control and extensive customization, Golang-based Operators are the way to go. Ansible-based Operators are suitable when you want to extend your existing Ansible workflows to Kubernetes. In summary, it is essential to evaluate your project’s needs and your team’s expertise to make the most informed decision.

Below are alternative approaches that emphasize specific aspects of application customization depending on requirements.

Operator Implementations Examples

“Helm-based” K8s Operator Demo

In the world of modern business, expansion is the key to success. Imagine you’ve kick-started a highly popular food chain in your local area, and now you’re eager to take it global. You want to offer franchises to clients worldwide, each with their unique website customizations and databases. However, the thought of manually setting up each client’s environment (complete with database and configuration) seems a daunting and time-consuming task especially when potentially dealing with thousands of requests. This is where K8s Operators come to the rescue.

In this example, we explore how we can harness the power of K8s Operators to automate the process of building customized websites for franchise clients, turning a complex and time-consuming task into an efficient and scalable solution. For this use case, we opt for Helm Chart Operators as they are among the simplest options available.

Steps to Create Helm-based K8s Operator

Pre-requisites

If you are using MacOS and have installed brew, you can install the Operator SDK by following the instructions below:

Step 1: Install the Operator SDK

#mac os
brew install operator-sdk

Step 2: Setup Kubernetes using Docker

Follow this Step-by-Step Guide for Setting-up Kubernetes Cluster in Rancher Desktop

Step 3: Setup Nginx in your K8s

Using the below commands, get helm charts for nginx.

helm install nginx-ingress ingress-nginx/ingress-nginx

Verify your nginx config is working as follows:

kubectl describe ingress -n ingress-nginx

Step 4: Navigate to the desired location and create a folder for Operators workspace.

Now you are in the helm charts root folder (if you are referring to helm charts via local path).

Step 5: Create Helm Operator

#Create Operator Helm Chart 
 operator-sdk init - plugins=helm.sdk.operatorframework.io/v1 \
- domain=ashokgudise.docker.io \
- helm-chart=~./orchestration-demo-api/deployment/orchestration-demo-api

Step 6: Locate the Docker file and verify all configuration once before you install

Step 7: Deploy the operator

make deploy

Step 8: Verify the deployment

kubectl get all -n orchestration-demo-operator-system

Step 9: Create your app using Operator

kubectl apply -f config/samples/ashokgudise_v1alpha1_orchestrationdemoapi.yaml

Step 10: Verify ingress for your app

kubectl describe ingress

Step 11: Forward the traffic to your local host

Create a proxy to forward traffic to your local host as follows:

kubectl create ingress orchestration-demo.local-localhost - class=nginx \
- rule="orchestration-demo.local/*=orchestration-demo-apil:8080"

Step 12: Finally, test your app as follows:

curl - resolve orchestrationdemoapi-sample-orchestration-demo-api:8080:127.0.0.1 http://localhost:57699/swagger-ui/index.html

Summary

In this blog, we explored the transformative power of Kubernetes Operators in simplifying the management of complex and stateful applications. We delved into the concept of Operators (with a special focus on the Kubernetes Helm Operator) and showcased their ability to automate deployments seamlessly. By understanding the core components with a hands-on demo, we unveiled the potential of Operators for streamlining application operations in Kubernetes. Whether you’re a seasoned Kubernetes pro or just starting your container journey, this blog provides insights into how Operators can revolutionize application management and deployment practices.

That’s all for now. Happy learning!

--

--

Ashok Gudise
CloudX at Fidelity

I am an enthusiastic learner who is curious to learn new market trends and new technologies that make software engineering much more robust and easy to learn.