摘要
欢迎来访我的GitHub,里面有我心血的结晶。Java、Docker、Kubernetes、DevOPS等应有尽有,还有系列产品文章,让你轻松掌握kubebuilder实战。快来看看吧!
正文
热烈欢迎浏览我的GitHub
https://github.com/zq2599/blog_demos
內容:全部原创文章内容筛选及配套设施源代码,涉及到Java、Docker、Kubernetes、DevOPS等;
系列产品文章内容连接
-
kubebuilder实战演练之一:准备工作
-
kubebuilder实战演练之二:第一次感受kubebuilder
-
kubebuilder实战演练之三:基本知识快评
-
kubebuilder实战演练之四:operator要求表明和设计方案
-
kubebuilder实战演练之五:operator编号
-
kubebuilder实战演练之六:搭建布署运作
-
kubebuilder实战演练之七:webhook
-
kubebuilder实战演练之八:知识要点随记
这篇概述
文中是《kubebuilder实战》系列产品的第二篇,前文将kubebuilder自然环境提前准备结束,今日我们在这里自然环境建立CRD和Controller,再部署到kubernetes自然环境而且认证是不是起效,全篇文章由下面这些内容构成:
-
建立API(CRD和Controller)
-
搭建和布署CRD
-
编译程序和运作controller
-
建立CRD相匹配的案例
-
删掉案例并终止controller
-
将controller制做成docker镜像系统
-
卸载掉和清除
建立helloworld新项目
-
实行下列指令,建立helloworld新项目:
mkdir -p $GOPATH/src/helloworld cd $GOPATH/src/helloworld kubebuilder init --domain com.bolingcavalry
-
控制面板輸出相近以下几点:
[root@kubebuilder helloworld]# kubebuilder init --domain com.bolingcavalry Writing scaffold for you to edit... Get controller runtime: $ go get sigs.k8s.io/controller-runtime@v0.5.0 Update go.mod: $ go mod tidy Running make: $ make /root/gopath/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... go build -o bin/manager main.go Next: define a resource with: $ kubebuilder create api
-
等候数分钟后建立进行,在$GOPATH/src/helloworld文件目录下增加以下几点,由此可见这也是个规范的go module工程项目:
[root@kubebuilder ~]# tree $GOPATH/src/helloworld /root/gopath/src/helloworld ├── bin │ └── manager ├── config │ ├── certmanager │ │ ├── certificate.yaml │ │ ├── kustomization.yaml │ │ └── kustomizeconfig.yaml │ ├── default │ │ ├── kustomization.yaml │ │ ├── manager_auth_proxy_patch.yaml │ │ ├── manager_webhook_patch.yaml │ │ └── webhookcainjection_patch.yaml │ ├── manager │ │ ├── kustomization.yaml │ │ └── manager.yaml │ ├── prometheus │ │ ├── kustomization.yaml │ │ └── monitor.yaml │ ├── rbac │ │ ├── auth_proxy_client_clusterrole.yaml │ │ ├── auth_proxy_role_binding.yaml │ │ ├── auth_proxy_role.yaml │ │ ├── auth_proxy_service.yaml │ │ ├── kustomization.yaml │ │ ├── leader_election_role_binding.yaml │ │ ├── leader_election_role.yaml │ │ └── role_binding.yaml │ └── webhook │ ├── kustomization.yaml │ ├── kustomizeconfig.yaml │ └── service.yaml ├── Dockerfile ├── go.mod ├── go.sum ├── hack │ └── boilerplate.go.txt ├── main.go ├── Makefile └── PROJECT 9 directories, 30 files
建立API(CRD和Controller)
-
下面要建立資源有关的具体内容了,group/version/kind这三一部分能够 明确資源的唯一真实身份,指令以下:
cd $GOPATH/src/helloworld kubebuilder create api \ --group webapp \ --version v1 \ --kind Guestbook
-
控制面板会提示是不是建立資源(Create Resource [y/n]),键入y
-
下面控制面板会提示是不是建立控制板(Create Controller [y/n]),键入y
-
kubebuilder会依据上述指令增加好几个文档,如下图白框所显示:
搭建和布署CRD
-
kubebuilder给予的Makefile将搭建和部署安排大幅简单化,实行下列指令会将全新搭建的CRD布署在kubernetes上:
cd $GOPATH/src/helloworld make install
-
控制面板輸出以下內容,提醒布署取得成功:
[root@kubebuilder helloworld]# make install /root/gopath/bin/controller-gen "crd:trivialVersions=true" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases kustomize build config/crd | kubectl apply -f - Warning: apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated in v1.16 , unavailable in v1.22 ; use apiextensions.k8s.io/v1 CustomResourceDefinition customresourcedefinition.apiextensions.k8s.io/guestbooks.webapp.com.bolingcavalry created
编译程序和运作controller
-
kubebuilder自动生成的controller源代码详细地址是:$GOPATH/src/helloworld/controllers/guestbook_controller.go , 內容以下:
package controllers import ( "context" "github.com/go-logr/logr" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" webappv1 "helloworld/api/v1" ) // GuestbookReconciler reconciles a Guestbook object type GuestbookReconciler struct { client.Client Log logr.Logger Scheme *runtime.Scheme } // kubebuilder:rbac:groups=webapp.com.bolingcavalry,resources=guestbooks,verbs=get;list;watch;create;update;patch;delete // kubebuilder:rbac:groups=webapp.com.bolingcavalry,resources=guestbooks/status,verbs=get;update;patch func (r *GuestbookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { _ = context.Background() _ = r.Log.WithValues("guestbook", req.NamespacedName) // your logic here return ctrl.Result{}, nil } func (r *GuestbookReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&webappv1.Guestbook{}). Complete(r) }
-
文中以感受基本上步骤为主导,不深入分析源代码,因此 对以上的编码仅做少许改动,用以认证是不是能起效,修改如下图白框所显示:
-
实行下列指令,会编译程序并运行刚刚改动的controller:
cd $GOPATH/src/helloworld make run
-
这时控制面板輸出以下几点,这儿要留意,controller是在kubebuilder电脑运作的,一旦应用Ctrl c终断控制面板,便会造成 controller终止:
[root@kubebuilder helloworld]# make run /root/gopath/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... /root/gopath/bin/controller-gen "crd:trivialVersions=true" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases go run ./main.go 2021-01-23T20:58:35.107 0800 INFO controller-runtime.metrics metrics server is starting to listen {"addr": ":8080"} 2021-01-23T20:58:35.108 0800 INFO setup starting manager 2021-01-23T20:58:35.108 0800 INFO controller-runtime.manager starting metrics server {"path": "/metrics"} 2021-01-23T20:58:35.108 0800 INFO controller-runtime.controller Starting EventSource {"controller": "guestbook", "source": "kind source: /, Kind="} 2021-01-23T20:58:35.208 0800 INFO controller-runtime.controller Starting Controller {"controller": "guestbook"} 2021-01-23T20:58:35.209 0800 INFO controller-runtime.controller Starting workers {"controller": "guestbook", "worker count": 1}
建立Guestbook资源的案例
-
如今kubernetes早已实施了Guestbook种类的CRD,并且相应的controller也已已经运作中,能够 试着建立Guestbook种类的案例了(等同于拥有pod的界定后,才能够建立pod);
-
kubebuilder早已全自动建立了一个种类的布署文档:$GOPATH/src/helloworld/config/samples/webapp_v1_guestbook.yaml ,內容以下,非常简单,下面我们就用这一文档来建立Guestbook案例:
apiVersion: webapp.com.bolingcavalry/v1 kind: Guestbook metadata: name: guestbook-sample spec: # Add fields here foo: bar
-
再次开启一个控制面板,登陆kubebuilder电脑上,实行下列指令就可以建立Guestbook种类的案例:
cd $GOPATH/src/helloworld kubectl apply -f config/samples/
-
以下所显示,控制面板提醒資源建立取得成功:
[root@kubebuilder helloworld]# kubectl apply -f config/samples/ guestbook.webapp.com.bolingcavalry/guestbook-sample created
-
用kubectl get指令能够 见到案例早已建立:
[root@kubebuilder helloworld]# kubectl get Guestbook NAME AGE guestbook-sample 112s
-
用指令kubectl edit Guestbook guestbook-sample编写该案例,改动的內容如下图白框所显示:
7. 这时去controller所属控制面板,能够 见到增加和更改的操作方法都是有日志輸出,我们增加的日志都是在里边,编码启用栈一目了然:
2021-01-24T09:51:50.418 0800 INFO controllers.Guestbook 1. default/guestbook-sample 2021-01-24T09:51:50.418 0800 INFO controllers.Guestbook 2. goroutine 188 [running]: runtime/debug.Stack(0xc0002a1808, 0xc0002fc600, 0x1b) /root/go/src/runtime/debug/stack.go:24 0x9f helloworld/controllers.(*GuestbookReconciler).Reconcile(0xc0003c9dd0, 0xc0002d02f9, 0x7, 0xc0002d02e0, 0x10, 0x12f449647b, 0xc000456f30, 0xc000456ea8, 0xc000456ea0) /root/gopath/src/helloworld/controllers/guestbook_controller.go:49 0x1a9 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc00022a480, 0x1430e00, 0xc0003e7560, 0x0) /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:256 0x166 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc00022a480, 0xc000469600) /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:232 0xb0 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).worker(0xc00022a480) /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:211 0x2b k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1(0xc000292980) /root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:152 0x5f k8s.io/apimachinery/pkg/util/wait.JitterUntil(0xc000292980, 0x3b9aca00, 0x0, 0x1609101, 0xc000102480) /root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:153 0x105 k8s.io/apimachinery/pkg/util/wait.Until(0xc000292980, 0x3b9aca00, 0xc000102480) /root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:88 0x4d created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1 /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:193 0x32d 2021-01-24T09:51:50.418 0800 DEBUG controller-runtime.controller Successfully Reconciled {"controller": "guestbook", "request": "default/guestbook-sample"} 2021-01-24T09:52:33.632 0800 INFO controllers.Guestbook 1. default/guestbook-sample 2021-01-24T09:52:33.633 0800 INFO controllers.Guestbook 2. goroutine 188 [running]: runtime/debug.Stack(0xc0002a1808, 0xc0003fa5e0, 0x1b) /root/go/src/runtime/debug/stack.go:24 0x9f helloworld/controllers.(*GuestbookReconciler).Reconcile(0xc0003c9dd0, 0xc0002d02f9, 0x7, 0xc0002d02e0, 0x10, 0x1d0410fe42, 0xc000456f30, 0xc000456ea8, 0xc000456ea0) /root/gopath/src/helloworld/controllers/guestbook_controller.go:49 0x1a9 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc00022a480, 0x1430e00, 0xc0003d24c0, 0x0) /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:256 0x166 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc00022a480, 0xc000469600) /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:232 0xb0 sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).worker(0xc00022a480) /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:211 0x2b k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1(0xc000292980) /root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:152 0x5f k8s.io/apimachinery/pkg/util/wait.JitterUntil(0xc000292980, 0x3b9aca00, 0x0, 0x1609101, 0xc000102480) /root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:153 0x105 k8s.io/apimachinery/pkg/util/wait.Until(0xc000292980, 0x3b9aca00, 0xc000102480) /root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:88 0x4d created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1 /root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:193 0x32d 2021-01-24T09:52:33.633 0800 DEBUG controller-runtime.controller Successfully Reconciled {"controller": "guestbook", "request": "default/guestbook-sample"}
删掉案例并终止controller
-
不会再必须Guestbook案例的情况下,实行下列指令就可以删掉:
cd $GOPATH/src/helloworld kubectl delete -f config/samples/
-
不会再必须controller的情况下,去它的控制面板应用Ctrl c终断就可以;
将controller制做成docker镜像系统
-
到此,我们早已体会过去了kubebuilder的基本要素,但是具体工作环境中controller一般都是会运作在kubernetes自然环境内,像上边这类运作在kubernetes以外的形式就有问题了,我们来试一下将其制成docker镜像系统随后在kubernetes自然环境运作;
-
这儿有一个规定,便是您要有一个kubernetes能够 浏览的镜像系统库房,比如局域网络内的Harbor,或是公共性的hub.docker.com,我这为了更好地实际操作便捷挑选了hub.docker.com,应用它的先决条件是有着hub.docker.com的注册新账号;
-
在kubebuilder电脑,开启一个控制面板,实行docker login指令登陆,依据提醒键入hub.docker.com的账号和登陆密码,那样就可以在当今操纵台子上实行docker push指令将镜像系统消息推送到hub.docker.com上(这一平台的互联网很差,很有可能要登陆好几回才能够取得成功);
-
实行下列指令搭建docker镜像系统并推荐到hub.docker.com,镜像系统名叫bolingcavalry/guestbook:002:
cd $GOPATH/src/helloworld make docker-build docker-push IMG=bolingcavalry/guestbook:002
-
hub.docker.com的互联网情况并不是一般的差,kubebuilder电脑的docker一定要设定镜像系统加快,以上指令假如遭受请求超时不成功,请再试几回,除此之外,搭建全过程中还会继续免费下载众多go控制模块的依靠,也必须您耐心等待,也比较容易碰到网络问题,必须数次再试,因此 ,最好应用局域网络内构建的Habor服务项目;
-
最后,指令运行取得成功后輸出以下:
[root@kubebuilder helloworld]# make docker-build docker-push IMG=bolingcavalry/guestbook:002 /root/gopath/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..." go fmt ./... go vet ./... /root/gopath/bin/controller-gen "crd:trivialVersions=true" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases go test ./... -coverprofile cover.out ? helloworld [no test files] ? helloworld/api/v1 [no test files] ok helloworld/controllers 8.604s coverage: 0.0% of statements docker build . -t bolingcavalry/guestbook:002 Sending build context to Docker daemon 40.27MB Step 1/14 : FROM golang:1.13 as builder ---> d6f3656320fe Step 2/14 : WORKDIR /workspace ---> Using cache ---> 83d05ead1041 Step 3/14 : COPY go.mod go.mod ---> Using cache ---> ae3e15a529f4 Step 4/14 : COPY go.sum go.sum ---> Using cache ---> 082223532ccc Step 5/14 : RUN go mod download ---> Using cache ---> bcdcfa1d65ca Step 6/14 : COPY main.go main.go ---> Using cache ---> 81d6a629ca98 Step 7/14 : COPY api/ api/ ---> Using cache ---> 75f99b174e97 Step 8/14 : COPY controllers/ controllers/ ---> b130d9f47903 Step 9/14 : RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go ---> Running in 768880aca19f Removing intermediate container 768880aca19f ---> bb4a494d3b43 Step 10/14 : FROM gcr.io/distroless/static:nonroot ---> 947e6f3ed7c1 Step 11/14 : WORKDIR / ---> Using cache ---> 22cc43cef8fb Step 12/14 : COPY --from=builder /workspace/manager . ---> 2137778f22c0 Step 13/14 : USER nonroot:nonroot ---> Running in 18295673073d Removing intermediate container 18295673073d ---> f7545379ab1f Step 14/14 : ENTRYPOINT ["/manager"] ---> Running in 550c47dd61dc Removing intermediate container 550c47dd61dc ---> 31cb31a6b03f Successfully built 31cb31a6b03f Successfully tagged bolingcavalry/guestbook:002 docker push bolingcavalry/guestbook:002 The push refers to repository [docker.io/bolingcavalry/guestbook] 99035107a955: Pushed 728501c5607d: Layer already exists 002: digest: sha256:54f8ec88511cce5b04c5d65cc15e0f7a7b4a8afb6b235904a638bff79e3c5784 size: 739
-
去hub.docker.com网站看一下,如下图,新镜像系统早已提交,那样只需一切设备只需能上网就能pull此镜像系统到本地运用了:
8. 镜像系统准备好以后,实行下列指令就可以在kubernetes自然环境布署controller:
cd $GOPATH/src/helloworld make deploy IMG=bolingcavalry/guestbook:002
-
控制面板会提醒各种資源被建立(rbac占多数):
[root@kubebuilder ~]# cd $GOPATH/src/helloworld [root@kubebuilder helloworld]# make deploy IMG=bolingcavalry/guestbook:002 /root/gopath/bin/controller-gen "crd:trivialVersions=true" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases cd config/manager && kustomize edit set image controller=bolingcavalry/guestbook:002 kustomize build config/default | kubectl apply -f - namespace/helloworld-system created Warning: apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated in v1.16 , unavailable in v1.22 ; use apiextensions.k8s.io/v1 CustomResourceDefinition customresourcedefinition.apiextensions.k8s.io/guestbooks.webapp.com.bolingcavalry configured role.rbac.authorization.k8s.io/helloworld-leader-election-role created clusterrole.rbac.authorization.k8s.io/helloworld-manager-role created clusterrole.rbac.authorization.k8s.io/helloworld-proxy-role created Warning: rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17 , unavailable in v1.22 ; use rbac.authorization.k8s.io/v1 ClusterRole clusterrole.rbac.authorization.k8s.io/helloworld-metrics-reader created rolebinding.rbac.authorization.k8s.io/helloworld-leader-election-rolebinding created clusterrolebinding.rbac.authorization.k8s.io/helloworld-manager-rolebinding created clusterrolebinding.rbac.authorization.k8s.io/helloworld-proxy-rolebinding created service/helloworld-controller-manager-metrics-service created deployment.apps/helloworld-controller-manager created
-
这时去看看kubernetes自然环境的pod,发觉的确早已增加了controller,如下图白框:
11.仔细的您应当会发觉图中黄框中表明这一pod事实上有两个器皿,用kubectl describe指令仔细观看,分别是kube-rbac-proxy和manager,如下图:
11. 因为有两个器皿,那麼查询日志时就需要特定在其中一个了,我们的controller相匹配的是manager器皿,因而查询日志的指令是:
kubectl logs -f \ helloworld-controller-manager-689d4b6f5b-h9pzg \ -n helloworld-system \ -c manager
-
再度建立Guestbook资源的案例,依然是kubectl apply -f config/samples/指令,再去看看manager器皿的日志,由此可见我们改动的信息早已打印出出来:
卸载掉和清除
-
感受结束后,假如想把前边建立的自然资源和CRD所有处理掉,能够 实行下列指令:
cd $GOPATH/src/helloworld make uninstall
-
到此,根据kubebuilder建立Operator有关网络资源的基本上步骤,我们早已感受过一遍了,这篇以了解专用工具和步骤为主导,仍未感受到Operator实际性的强悍作用,这种都已非后来的章节目录吧,我们逐渐加强学习实践活动;
你并不孤单,欣宸原創一路相伴
-
Java系列
-
Spring系列
-
Docker系列产品
-
kubernetes系列
-
数据库查询 分布式数据库系列产品
-
DevOps系列
关注不迷路
扫码下方二维码,关注宇凡盒子公众号,免费获取最新技术内幕!
评论0