What is Kubebuilder?
Kubebuilder is a development framework for building Kubernetes operators using the Go programming language. It provides project scaffolding, resource definitions, and controller logic to simplify operator creation. In short, Kubebuilder allows developers to create CRDs (Custom Resource Definitions) and controllers, which watch Kubernetes resources and act upon changes.Setting Up a Project with Kubebuilder
The typical process starts by setting up a basic project structure using thekubebuilder init
command. This scaffolds the folders and files necessary for an operator project. The following commands initialize a project and create a custom API for a simple application:
WebApp
, a custom resource that will deploy and manage a Kubernetes Deployment and Service.
Adding Metrics to the Operator
For observability, we can integrate Prometheus metrics into the operator. In Kubernetes, Prometheus can scrape data from custom endpoints to provide insights into application behavior. By using theprometheus.NewCounter
method from the Prometheus Go client, we set up counters to track resource actions, such as the creation and deletion of WebApp
instances.
In the controller’s main loop, you might add code like this to register counters:
Deploying Prometheus to Scrape Metrics
You must configure Prometheus to scrape the operator’s metrics endpoint. To achieve this, add a scrape configuration to the Prometheus setup, pointing to the operator service endpoint:/metrics
path, which Prometheus regularly scrapes to collect data.
Running the Operator and Observing Metrics
After deploying the operator to a Kubernetes cluster (e.g., a local KIND cluster), we can create customWebApp
resources that initiate deployments. By applying YAML files with the custom WebApp
definitions, developers can observe metrics that Prometheus scrapes and view them in a dashboard like Grafana.
For instance, creating or updating a WebApp
resource increases the counter for created deployments, visible in both the Prometheus and Grafana dashboards: