This tutorial is to develop Kubernetes Operators with RedHat Operator SDK.
What is Operator?
RedHat Operator Framework
Please refer to GitHub - Operator Framework
Operator SDK
Create a new project
1
2
3$ export GO111MODULE=on
$ operator-sdk new sample-operator --repo=github.com/stevenchiu30801/sample-operator
$ cd sample-operatorAdd a new Custom Resource Definition
1
$ operator-sdk add api --api-version=sample.io/v1alpha1 --kind=SampleCr
Define the spec and status
1
2
3
4
5// pkg/apis/sample/v1alpha1/samplecr_types.go
type SampleCrSpec struct {
TestList []int `json:"testList"`
Count int `json:"count"`
}1
2
3
4
5# update the generated code for that resource type
$ operator-sdk generate k8s
# automatically generate the CRDs
$ operator-sdk generate crds
# changes would be applied in deploy/crds/sample.io_samplecr_crd.yamlAdd a new Controller
1
$ operator-sdk add controller --api-version=sample.io/v1alpha1 --kind=SampleCr
Design the controller
pkg/controller/samplecr/samplecr_controller.go
Register CRD
1
$ kubectl create -f deploy/crds/sample.io_samplecr_crd.yaml
Run the operator
Two ways
Run as a Deployment inside the cluster
1
2
3
4
5
6# build image and push it to your registry first!
$ sed -i 's|REPLACE_IMAGE|<docker/repo:tag>|g'deploy/operator.yaml
$ kubectl create -f deploy/service_account.yaml
$ kubectl create -f deploy/role.yaml
$ kubectl create -f deploy/role_binding.yaml
$ kubectl create -f deploy/operator.yamlRun locally outside the cluster
1
2# during development cycle to deploy and test faster
$ operator-sdk run --local --namespace=default
Create CR
1
2# modify CR spec first
$ kubectl create -f deploy/crds/sample.io_v1alpha1_samplecr_cr.yaml
Some design logic of controller
- Design control logic in the view of maintaining states of reconciled objects (custom resources)
- Don’t think in the way of handling requests on orchestrator or manager
- Since reconciled object would be requeue due to changes of status
- Try to not maintain soft state in operator
- Store data in database or volume
- Use Status to keep object states
- Use finalizer to handle cleanup of external resources (resources created from Helm) or customize cleanup logic
- For resources allocated in Go objects, one could use Controller Reference to enable automated cleanup
Reference
Go API Package
- Sample
memcached_controller.go
- kubernetes-sigs/controller-runtime
- Main API to design operators
In my design case, I use Go Operator but have to deploy resources with Helm (Too many resources to deploy with Go objects)
Here are some API references to do so and also refer to the code
- kubernetes/cli-runtime
- For Helm client to get Kubernetes client config
- genericclioptions.AddFlags()
- Field explanation for genericclioptions.ConfigFlags
- kubernetes/client-go#rest/config.go-rest.InClusterConfig()
- Config intended for Kubernetes clients that expect to be running inside a pod
- helm/helm#pkg
Operator
- operator-framework/getting-started
- operator-sdk#user-guide.md - for Go operator
- operator-framework/operator-sdk-samples
Finalizer
- Extend the Kubernetes API with CustomResourceDefinitions#Finalizers
- kubernetes-sigs/kuberbuilder#using_finalizers.md
- kubernetes-sigs/kubebuilder#finalizer_example.go
- Kubernetes 实战-Operator Finalizers 实现
Others
Update
Operator SDK has its own website and documentation.