zhaoqin@zhaoqindeMBP-2 elasticweb % pwd
/Users/zhaoqin/github/blog_demos/kubebuilder/elasticweb
zhaoqin@zhaoqindeMBP-2 elasticweb % make run
/Users/zhaoqin/go/bin/controller-gen object:headerFile=“hack/boilerplate.go.txt” paths="./…"
go fmt ./…
go vet ./…
/Users/zhaoqin/go/bin/controller-gen “crd:trivialVersions=true” rbac:roleName=manager-role webhook paths="./…" output:crd:artifacts:config=config/crd/bases
go run ./main.go
2021-02-20T20:46:16.774+0800 INFO controller-runtime.metrics metrics server is starting to listen {“addr”: “:8080”}
2021-02-20T20:46:16.774+0800 INFO setup starting manager
2021-02-20T20:46:16.775+0800 INFO controller-runtime.controller Starting EventSource {“controller”: “elasticweb”, “source”: “kind source: /, Kind=”}
2021-02-20T20:46:16.776+0800 INFO controller-runtime.manager starting metrics server {“path”: “/metrics”}
2021-02-20T20:46:16.881+0800 INFO controller-runtime.controller Starting Controller {“controller”: “elasticweb”}
2021-02-20T20:46:16.881+0800 INFO controller-runtime.controller Starting workers {“controller”: “elasticweb”, “worker count”: 1}
新建elasticweb资源对象
-
负责处理elasticweb的Controller已经运行起来了,接下来就开始创建elasticweb资源对象吧,用yaml文件来创建;
-
在config/samples目录下,kubebuilder为咱们创建了demo文件elasticweb_v1_elasticweb.yaml,不过这里面spec的内容不是咱们定义的那四个字段,需要改成以下内容:
apiVersion: v1
kind: Namespace
metadata:
name: dev
labels:
name: dev
apiVersion: elasticweb.com.bolingcavalry/v1
kind: ElasticWeb
metadata:
namespace: dev
name: elasticweb-sample
spec:
Add fields here
image: tomcat:8.0.18-jre8
port: 30003
singlePodQPS: 500
totalQPS: 600
- 对上述配置的几个参数做如下说明:
-
使用的namespace为dev
-
本次测试部署的应用为tomcat
-
service使用宿主机的30003端口暴露tomcat的服务
-
假设单个pod能支撑500QPS,外部请求的QPS为600
- 执行命令kubectl apply -f config/samples/elasticweb_v1_elasticweb.yaml,即可在kubernetes创建elasticweb实例:
zhaoqin@zhaoqindeMBP-2 elasticweb % kubectl apply -f config/samples/elasticweb_v1_elasticweb.yaml
namespace/dev created
elasticweb.elasticweb.com.bolingcavalry/elasticweb-sample created
- 去controller的窗口发现打印了不少日志,通过分析日志发现Reconcile方法执行了两次,第一执行时创建了deployment和service等资源:
2021-02-21T10:03:57.108+0800 INFO controllers.ElasticWeb 1. start reconcile logic {“elasticweb”: “dev/elasticweb-sample”}
2021-02-21T10:03:57.108+0800 INFO controllers.ElasticWeb 3. instance : Image [tomcat:8.0.18-jre8], Port [30003], SinglePodQPS [500], TotalQPS [600], RealQPS [nil] {“elasticweb”: “dev/elasticweb-sample”}
2021-02-21T10:03:57.210+0800 INFO controllers.ElasticWeb 4. deployment not exists {“elasticweb”: “dev/elasticweb-sample”}
2021-02-21T10:03:57.313+0800 INFO controllers.ElasticWeb set reference {“func”: “createService”}
2021-02-21T10:03:57.313+0800 INFO controllers.ElasticWeb start create service {“func”: “createService”}
2021-02-21T10:03:57.364+0800 INFO controllers.ElasticWeb create service success {“func”: “createService”}
2021-02-21T10:03:57.365+0800 INFO controlle