TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
✓
Angular
0%
✓
Astro
0%
✓
Svelte
0%
✓
Vue.js
0%
✓
Other
0%
✓
I only use React
0%
✓
I don't use JavaScript
0%
Containers / Kubernetes

How To Build a Kubernetes Operator From Scratch

Operators streamline workflows, reduce manual intervention and support infrastructure management, key skills for anyone working in DevOps.
May 6th, 2025 4:00pm by
Featued image for: How To Build a Kubernetes Operator From Scratch
Featured image by Ken Suarez on Unsplash.

Building robust, scalable applications in Kubernetes often requires fine-tuned automation — and that’s where Kubernetes operators come into play. Operators extend Kubernetes’ capabilities by automating management of custom resources and stateful applications. Think of operators as software that knows how to operate your apps, ensuring everything runs smoothly, even in complex environments.

I will guide you through the steps of creating a Kubernetes operator from scratch, a crucial skill for anyone working in modern cloud native ecosystems. We’ll walk through building a “self-healing diagnostics” operator, designed to boost observability in Kubernetes clusters. With this operator, crash information is automatically annotated to pods, and remediation actions can be applied on demand.

Why Is This Important to You?

For anyone managing Kubernetes clusters, operators streamline workflows, reduce manual intervention and allow proactive infrastructure management.

This tutorial cuts through the theory and gives you practical, real-world experience, covering:

  1. Full local workflow: Learn how to run and test everything locally using Kind, a tool for testing Kubernetes locally, and container manager Docker.
  2. Real-world operator patterns: You’ll create Custom Resource Definitions (CRDs), implement reconciliation loops and leverage the Kubernetes client API.
  3. Immediate feedback: You’ll then test your operator and watch it automatically annotate crash information to pods in real time.

Mastering Kubernetes operators sets you up to manage complex systems with precision and scalability. Ready to level up? Let’s get started!

Prerequisites

Start by installing all the necessary tools to build and run your Kubernetes operator.

Installing prerequisites on macOS


Installing prerequisites on Linux


Verify your installations

Set Up a Cluster With Kind


If you need to reset your environment at any point:

Create a Test Application

Let’s begin by creating a deliberately fragile application in Golang that will help you test your operator.

Create a Go Application


Dockerfile

Build and Test Locally


Deploy to Kubernetes

Create a deployment manifest:

fragile-app-deployment.yaml:


Deploy to your Kind cluster:


Test the application in the cluster:


In another terminal, send requests to crash the app:


Watch Kubernetes restart the pod:

Understanding the Operator Pattern

Before building an operator, it’s good to understand what makes it special.

What Is a Kubernetes Operator?

An operator is a pattern that extends Kubernetes to handle application-specific operational tasks. It works by doing the following:

  • It defines custom resources (i.e., CRDs) that represent your application’s desired state.
  • It implements controllers that continuously reconcile the actual state with the desired state.

Think of operators as automated domain experts that can monitor your application’s health, react to changes or problems, and apply specialized knowledge to fix issues.

The Reconciliation Loop

The heart of the operator is the reconciliation loop! Think of it as Kubernetes’ way of constantly moving from the current state to the desired state.


The controller is triggered whenever a pod changes. Then it:

  1. Checks if the pod matches any PodDiagnoser’s target labels.
  2. Examines container restart information.
  3. Adds diagnostic annotations if crashes are detected.
  4. Applies any configured remediation actions.

This cycle repeats continuously, ensuring your pods always have up-to-date diagnostic information!

Building an Operator

Now let’s create an operator that automatically diagnoses pod crashes.

Initialize the Operator Project

Define the CRD

Edit the generated api/v1/poddiagnoser_types.go file:


Run the following command to update the generated code:

Implement the Controller

Edit internal/controller/poddiagnoser_controller.go:


Update the role-based access control (RBAC) manifests:

Set Up RBAC Permissions

Create a file named operator-permissions.yaml:

Modify the Deployment Manifest in Your Config Directory

  1. Locate your operator’s deployment YAML file, typically in config/manager/manager.yaml
  2. Add imagePullPolicy: Never to the container spec:

Build and Deploy the Operator


Apply the RBAC permissions:


Check that the operator is running:


If the pod is having issues pulling the image, you might need to restart it:


Security alert: While using cluster-admin is convenient for experimentation, it grants excessive privileges. For production, always follow the principle of least privilege by creating custom roles with only the necessary permissions.

If you still see RBAC errors, you can temporarily grant broader permissions for development:

Create a PodDiagnoser Instance

Create a file named config/samples/diagnostics_v1_poddiagnoser.yaml:


Apply it to your cluster:

View Operator Logs

Check the operator logs to see what’s happening:

Test the Operator

Trigger the fragile app to crash:


In another terminal, send requests to crash the app:


Wait for the pod to restart, then check if the operator added diagnostic annotations:


You should see annotations like:


Also, check for the event recorded by the operator:


You should see events like:

Clean Up

When you’re done with the tutorial, it’s time to clean up your resources:

Conclusion

Congratulations! You’ve successfully built a Kubernetes operator that diagnoses pod crashes, applies remediation strategies and integrates seamlessly with Kubernetes events for enhanced visibility. This achievement not only showcases your understanding of the operator pattern but also highlights your ability to extend Kubernetes with domain-specific automation and intelligence.

By leveraging the power of the operator, you’ve enriched Kubernetes’ self-healing capabilities, creating a smarter, more efficient system that proactively addresses application failures with diagnostic insights.

Key takeaways for future success:

  • CRDs: You now know how to define schemas tailored to your application’s needs!
  • Controllers and reconciliation: You’ve mastered how to build the control loop that drives your operator.
  • Robust error handling: You can now implement retry mechanisms, ensuring resilience in future operations.
  • RBAC and security: You’ve scoped permissions, making your operator production-ready.

This project is a testament to what’s possible when you pair Kubernetes’ power with precise, purpose-built automation. Keep building, iterating and pushing the boundaries of what your Kubernetes environment can do. The future is powered by innovation like this, so get out there and keep making things happen.

Learn how to build a scalable CI/CD pipeline for Kubernetes with Andela’s guide “Make a Scalable CI/CD Pipeline for Kubernetes with GitHub and ArgoCD.”

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker, Real.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.