Understanding Kubernetes CRDs, Operators, and Building an Operator with Kubebuilder
This article explains how Kubernetes CustomResourceDefinitions (CRDs) let users create custom resources, how Operators add behavior to those resources, and provides a step‑by‑step Kubebuilder tutorial for scaffolding, generating, and running a simple Foo operator.
Kubernetes includes built‑in resources such as pod , deployment , configmap , service , etc., which are managed by internal components. In addition, users can define arbitrary resources via CustomResourceDefinitions (CRD), gaining the same CRUD capabilities as built‑in objects.
By registering a CRD, custom resources like myPod or CronTab are stored in the control‑plane etcd and can be manipulated with kubectl . The article shows a concrete CRD manifest for a CronTab resource and how applying it creates the custom type.
Because a CRD alone does not implement behavior, an Operator (a custom controller) is needed to react to changes. An Operator watches custom resources and can, for example, create a Pod to run user‑specified logic.
The tutorial then introduces Kubebuilder, a framework that scaffolds CRDs and Operators. It walks through installing Kubebuilder, creating a test project, initializing it, generating the API, and producing the CRD YAML with make manifests .
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# name must match
.
name: crontabs.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
status:
type: object
properties:
podName:
type: string
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ctAfter applying the CRD, a custom CronTab object can be created with kubectl apply -f my-crontab.yaml and listed with kubectl get crontab .
To give the custom resource executable behavior, the article shows how to write an Operator using Kubebuilder: adding reconciliation logic that logs the Foo spec, generating manifests, installing them, and running the controller.
func (r *FooReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx)
// fetch Foo
foo := &mygroupv1.Foo{}
if err := r.Get(ctx, req.NamespacedName, foo); err != nil {
l.Error(err, "unable to fetch Foo")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// log Foo attributes
l.Info("Received Foo", "Image", foo.Spec.Image, "Msg", foo.Spec.Msg)
return ctrl.Result{}, nil
}Typical Makefile targets ( make install , make run ) are shown, and sample controller output demonstrates the Foo object being reconciled.
In summary, Kubernetes CRDs provide extensibility by allowing users to define new resource types, while Operators implement custom control logic, together delivering a powerful, flexible cloud‑native extension mechanism.
System Architect Go
Programming, architecture, application development, message queues, middleware, databases, containerization, big data, image processing, machine learning, AI, personal growth.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.