Skip to content

How to build a custom Operator pattern in Kubernetes using Kubebuilder, a minimalist custom Controller(s) and CRD(s).

Notifications You must be signed in to change notification settings

abstractmachines/kubebuilder-controller

Repository files navigation

Kubebuilder: Controllers, CRDs, and Operators

How to build custom controllers, the easy way.

To begin, clone (quickstart or kube builder book): https://book.kubebuilder.io/

For the impatient

The substantive part of this tutorial is in these two files:

  1. DEPLOYMENT-CONTROLLERRUNTIME.md
  2. REPLICAS-UPDATE-DEPLOYMENT.md

Runbooks

Not a tutorial based on theory. Just some basic developer workflow runbooks.

Runbook (startup)

Developer workflow for custom controllers

  1. Install custom types and generate code make install.
  2. Run controller (make run).
  3. Make changes to code while running controller.
  4. in new terminal, apply changes to cluster and note changes. kubectl apply -f config/samples.

Note that the apply will cause the Reconciler to "reconcile." So applying a change to the cluster will, for example, log any print/log statements in the controller's reconcile() function.

Sample Controller workflows

Workflows for developing custom CRDs and controllers (custom operators).

Log statement:

  1. Make change in guestbook controller's Reconcile code (add fmt.Println("henlo")).
  2. Run controller (make run).
  3. See the controller print "henlo" in the make run terminal window/tmux window.

Add labels imperatively and apply to cluster:

  1. Run controller (make run).
  2. Add labels to ./config/samples/webapp_v1_guestbook.yaml under metadata
metadata:
name: guestbook-sample
labels:
  is-awesome: totes
  is-bad: nopes
  1. apply to cluster: apply -f ./config/samples.
  2. Use CLI label selector. k get guestbook -l is-awesome=totes. You'll see guestbook crd.

Get guestbook CRD, its labels, its namespace ...

  1. Use Reconciler function in Controller to Get object properties from NamespacedName struct.
  2. Run controller (make run).
  3. Note changes

Create a basic Deployment

  • Use this document DEPLOYMENT-CONTROLLERRUNTIME.md
  • Topics covered: controller runtime pkg, client pkg, appsv1 pkg, etc
  • You'll learn how to navigate the Kubernetes API at pkg.go.dev

Specify a desired state (scaled replicas), and update Deployment

Create a basic Service ...

About

How to build a custom Operator pattern in Kubernetes using Kubebuilder, a minimalist custom Controller(s) and CRD(s).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published