基于kubebuilder的operator开发实践
作者:互联网
1.准备工作
1.1依赖条件
dependent | version |
---|---|
go | v1.13+ |
docker | v17.03+ |
kubectl | v1.11.3+ |
kubernetes | v1.11.3+ |
1.2安装kubebuilder
$ os=$(go env GOOS)
$ arch=$(go env GOARCH)
$ curl -L https://go.kubebuilder.io/dl/2.3.1/${os}/${arch} | tar -xz -C /tmp/
$ mv /tmp/kubebuilder_2.3.1_${os}_${arch} /usr/local/kubebuilder
$ export PATH=$PATH:/usr/local/kubebuilder/bin
//chack kubebuilder
$ kubebuilder version
1.3概要设计
1.4 案例介绍
2.基于kubebuilder的operator开发
2.1项目初始化
$ mkdir kubebuilder-demo
$ cd kubebuilder-demo
$ go mod init welcome_demo.domain
$ kubebuilder init --domain demo.welcome.domain
初始化项目结束后,kubebuilder会自动生成一系列配置文件和代码框架:
kubebuilder-demo
├── Dockerfile
├── Makefile
├── PROJECT
├── config
│ ├── default
│ │ ├── kustomization.yaml
│ │ ├── manager_auth_proxy_patch.yaml
│ │ └── manager_config_patch.yaml
│ ├── manager
│ │ ├── controller_manager_config.yaml
│ │ ├── kustomization.yaml
│ │ └── manager.yaml
│ ├── prometheus
│ │ ├── kustomization.yaml
│ │ └── monitor.yaml
│ └── rbac
│ ├── auth_proxy_client_clusterrole.yaml
│ ├── auth_proxy_role.yaml
│ ├── auth_proxy_role_binding.yaml
│ ├── auth_proxy_service.yaml
│ ├── kustomization.yaml
│ ├── leader_election_role.yaml
│ ├── leader_election_role_binding.yaml
│ ├── role_binding.yaml
│ └── service_account.yaml
├── go.mod
├── go.sum
├── hack
│ └── boilerplate.go.txt
└── main.go
创建"Welcome" Kind 及其对应的控制器
$ kubebuilder create api --group webapp --kind Welcome --version v1
create Resource[y/n]
y
create Controller[y/n]
y
输入两次y,kubebuilder分别创建了资源和控制器的模版,此处的"group"、"version"、"kind" 三个属性组合起来标识一个k8s的CRD,代码清单:
kubebuilder-demo
├── Dockerfile
├── Makefile
├── PROJECT
├── api
│ └── v1
│ ├── groupversion_info.go
│ ├── welcome_types.go
│ └── zz_generated.deepcopy.go
├── bin
│ └── controller-gen
├── config
│ ├── crd
│ │ ├── kustomization.yaml
│ │ ├── kustomizeconfig.yaml
│ │ └── patches
│ │ ├── cainjection_in_welcomes.yaml
│ │ └── webhook_in_welcomes.yaml
│ ├── default
│ │ ├── kustomization.yaml
│ │ ├── manager_auth_proxy_patch.yaml
│ │ └── manager_config_patch.yaml
│ ├── manager
│ │ ├── controller_manager_config.yaml
│ │ ├── kustomization.yaml
│ │ └── manager.yaml
│ ├── prometheus
│ │ ├── kustomization.yaml
│ │ └── monitor.yaml
│ ├── rbac
│ │ ├── auth_proxy_client_clusterrole.yaml
│ │ ├── auth_proxy_role.yaml
│ │ ├── auth_proxy_role_binding.yaml
│ │ ├── auth_proxy_service.yaml
│ │ ├── kustomization.yaml
│ │ ├── leader_election_role.yaml
│ │ ├── leader_election_role_binding.yaml
│ │ ├── role_binding.yaml
│ │ ├── service_account.yaml
│ │ ├── welcome_editor_role.yaml
│ │ └── welcome_viewer_role.yaml
│ └── samples
│ └── webapp_v1_welcome.yaml
├── controllers
│ ├── suite_test.go
│ └── welcome_controller.go
├── go.mod
├── go.sum
├── hack
│ └── boilerplate.go.txt
└── main.go
2.2初始化校验
$ make install
//出现下面内容,恭喜你初始化成功了
/kubebuilder-demo/bin/controller-gen "crd:trivialVersions=true,preserveUnknownFields=false" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
go: creating new go.mod: module tmp
Downloading sigs.k8s.io/kustomize/kustomize/v3@v3.8.7
go: downloading github.com/emicklei/go-restful v1.1.3
go get: added sigs.k8s.io/kustomize/kustomize/v3 v3.8.7
/kubebuilder-demo/bin/kustomize build config/crd | kubectl apply -f -
customresourcedefinition.apiextensions.k8s.io/welcomes.webapp.demo.welcome.domain configured
这里要注意了可能会遇到go get "i/o timeout"等异常,这里需要切换下代理,我是在两个代理间来回切换才get 下来所有包。。。
export GOPROXY=https://mirrors.aliyun.com/goproxy/
export GOPROXY=https://goproxy.io,direct
2.2 controller编码(核心)
2.2.1修改Resource Type
2.2.2 修改controller逻辑
2.2.2.1 添加rbac权限
2.2.2.2 控制Deployment以及Service的创建
代码太多就不贴出来了哈,里面有详细的注解,请参考
https://github.com/broce-597/kubebuilder-demo
2.3功能测试
$ make install //会在你当前集群下创建CRD实例(./kube/config)
$ make run //本地run operator 实例
$ kubectl apply -f ./config/samples/webapp_v1_welcome.yaml
//查看本地是否创建Deployment/Service
$ kubec get deploy
$ kubec get svc
$ kubec get po
发现所有的resource都起来了
注释:博主自己写了一个welcome的测试服务,已经打好镜像放在了公有云(registry.cn-hangzhou.aliyuncs.com/broce_597/welcome:1.0.0)
请求服务http://ip:nodeport/sayhello
注释: 可以终端修改下welcome 的crd实例的name字段,在浏览器再次请求服务看看会发生什么呢!
标签:welcome,实践,yaml,manager,kubebuilder,role,go,operator 来源: https://www.cnblogs.com/broce-597/p/16242849.html