A Kubernetes Operator tutorial based on Kubebuilder.
It will take you building to a full-featured cronjob controller step by step:
There are some prerequisites that need to be installed in advance.
Extract the archive we downloaded into /usr/local, creating a Go tree in /usr/local/go:
$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.15.9.linux-amd64.tar.gz
Add /usr/local/go/bin to the PATH environment variable in /etc/profile:
$ export PATH=$PATH:/usr/local/go/bin
$ export PATH=$PATH:$HOME/go/bin
$ export GO111MODULE=on
Verify that Go has installed successfully:
$ go version
go version go1.15.9 linux/amd64
Install the docker package:
$ sudo zypper install docker
To automatically start the Docker service at boot time:
$ sudo systemctl enable docker.service
Start the Docker service:
$ sudo systemctl start docker.service
To allow a certain user to connect to the local Docker daemon, use the following command:
$ sudo /usr/sbin/usermod -aG docker i516697
Download and install the latest minikube:
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
Start our cluster:
$ minikube start --driver=docker --kubernetes-version=v1.17.0
Create a symbolic link to minikube's binary named kubectl:
$ ln -s $(which minikube) /usr/local/bin/kubectl
The following script detects our OS and downloads the appropriate kustomize binary to our current working directory:
$ curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
This will install controller-gen binary in GOPATH/bin:
$ go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.2.5
Download kubebuilder and install locally:
$ os=$(go env GOOS)
$ arch=$(go env GOARCH)
$ curl -L https://go.kubebuilder.io/dl/2.3.2/${os}/${arch} | tar -xz -C /tmp/
$ sudo mv /tmp/kubebuilder_2.3.2_${os}_${arch}/* /usr/local/kubebuilder
$ export PATH=$PATH:/usr/local/kubebuilder/bin
It covers initialization, development, deployment and testing.
- Tell Go and kubebuilder the base import path of our module:
$ go mod init github.com/umarelx/Operator-101
- Scaffold out a new project:
$ kubebuilder init --domain github.com
- Add a new API:
$ kubebuilder create api --group batch --version v1 --kind CronJob
- Install CRDs and run the controller locally:
$ make install $ make run
- Install instances of CRs:
$ kubectl apply -f config/samples/batch_v1_cronjob.yaml
- Build/Push our image and deploy the controller to cluster:
$ make docker-build docker-push IMG=umarelx/cronjob:latest $ make deploy IMG=umarelx/cronjob:latest
Execute integration tests and compile:
$ make test
$ make
Check cronjob's running/updating status:
$ kubectl get cronjob.batch.github.com cronjob-sample -o yaml
$ kubectl get job
Delete instances of CRs:
$ kubectl delete cronjob.batch.github.com cronjob-sample
Check/Delete namespace after deployment:
$ kubectl get namespace
$ kubectl delete namespace operator-101-system
Delete CRDs from a cluster:
$ make uninstall
We love contributions! Before submitting a Pull Request, it's always good to start with a new issue first.
This repository is licensed under Apache 2.0. Full license text is available in LICENSE.