An example of Kubernetes Operator built with the kubebuilder.
This PodSet operator takes care of scaling up and down pods that run a sleep 3600 command in a busybox image. Nothing really fancy, but the point is to:
-
see how the CRD and operator controller can be defined, packaged and deployed
-
demonstrate the logic of reconcialation when a custom resource changes, or when pods are added or removed by a user
- kubebuilder version: v2.3.1+
- kubernetes: v1.14+
- go version: v1.13+
- docker version: 17.03+
执行make install即可部署CRD到kubernetes:
make install
部署成功后,用api-versions命令可以查到该GV:
kubectl api-versions|grep data.clond
kubectl apply -f config/samples/data.clond_v1_podset.yaml
删除pod
kubectl apply -f config/samples/pod_delete.yaml
pod扩容
kubectl apply -f config/samples/scale_up.yaml
pod缩容
kubectl apply -f config/samples/scale_down.yaml
清理pod
kubectl delete -f config/samples/data.clond_v1_podset.yaml
pod查看
kubectl get pod
operator查看
kubectl get podset
operator的状态查看
kubectl describe podset podset-sample
operator的状态也是在controller中设置的。
make docker-build docker-push IMG=shaloudocker/podset:v1
部署
make deploy IMG=shaloudocker/podset:v1
可以单独在Makefile
下添加构建输出yaml
的编排。
allYaml: manifests
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default > config/manifests/all.yaml
然后执行新增的命令即可。
make allYaml IMG=shaloudocker/podset:v1
开发完成后就可以使用这个模板直接到环境上了。
make uninstall