You build a Kubernetes Operator in Go by scaffolding a project with the Operator SDK, defining a Custom Resource Definition (CRD), and implementing a reconciliation loop that watches for changes to your custom resources.
- Install the Operator SDK and create a new project named
my-operatorwith the--domainflag set toexample.comand--typeset togo.operator-sdk init --domain example.com --type go - Create a new API group named
v1alpha1with a kind namedMyResourceusing thecreate apicommand.operator-sdk create api --group mygroup --version v1alpha1 --kind MyResource - Generate the code for your CRD and controller by running the make target
generate.make generate - Implement your business logic in the
Reconcilemethod withincontrollers/myresource_controller.goto manage the lifecycle of your custom resource.vim controllers/myresource_controller.go - Build the operator image and push it to a container registry using the
maketargetdocker-buildfollowed bydocker-push.make docker-build docker-push - Apply the CRD and deploy the operator to your Kubernetes cluster using the
maketargetdeploy.make deploy