How to build custom controllers, the easy way.
To begin, clone (quickstart or kube builder book): https://book.kubebuilder.io/
- Inspired by https://github.com/sethp-nr/guestbook-workshop
- See also: https://maelvls.dev/learning-kubernetes-controllers/
- See also: Kubernetes community document on controllers
The substantive part of this tutorial is in these two files:
Not a tutorial based on theory. Just some basic developer workflow runbooks.
- After running Kubebuilder init and startup basics for project,
- including a
make install,make run, and probably anapply, - follow the
Developer Workflowloop below.
- Install custom types and generate code
make install. - Run controller (
make run). - Make changes to code while running controller.
- in new terminal,
applychanges 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.
Workflows for developing custom CRDs and controllers (custom operators).
Log statement:
- Make change in guestbook controller's
Reconcilecode (addfmt.Println("henlo")). - Run controller (
make run). - See the controller print "henlo" in the
make runterminal window/tmux window.
Add labels imperatively and apply to cluster:
- Run controller (
make run). - Add labels to
./config/samples/webapp_v1_guestbook.yamlundermetadata
metadata:
name: guestbook-sample
labels:
is-awesome: totes
is-bad: nopes
applyto cluster:apply -f ./config/samples.- Use CLI label selector.
k get guestbook -l is-awesome=totes. You'll seeguestbookcrd.
Get guestbook CRD, its labels, its namespace ...
- Use Reconciler function in Controller to
Getobject properties fromNamespacedNamestruct. - Run controller (
make run). - 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
- Use this document REPLICAS-UPDATE-DEPLOYMENT.md
- Topics covered:
types,schemas, more details onDeployments
Create a basic Service ...