当前位置:网站首页>Operator-1初识Operator
Operator-1初识Operator
2022-07-01 12:40:00 【51CTO】
背景:
接触kubernetes也好多年了,开始就各种听说Operator的,但是从来没有深入了解动手写过Operator
。开始体验一下简单的Operator
Operator初体验
什么是Operator?
参照:红帽官方文档 什么是 Kubernetes Operator?
**coreos2016年引入,**是一种封装、部署和管理 Kubernetes 应用的方法
- crd webhook controller
开发工具:
what is crd
**CRD **全称是 Custom Resource Definition, CRD是一种无需编码就可以扩展原生kubenetes API接口的方式。适合扩展kubernetes的自定义接口和功能。如果想更为灵活的添加逻辑就需要API Aggregation方式.
开始准备
常用的开发工具有一下几种:
我的开发工具 包括goland kubebuilder kustomize,kubernetes1.23.6,工作环境rocky linux 8.5 go 1.17
注意:一定看一下go 版本 与开发工具对应版本,以及与kubernetes的版本
kubebuilder kustomize install
https://github.com/kubernetes-sigs/kubebuilder/releases



创建并初始化项目
goland创建名为kube-oprator1的项目:

终端执行一下命令:

貌似提示我go版本过低?(go版本 1.17.6我的是)
升级一下go版本
注意:非必须,后面是降低了kubebuilder的版本。go版本就保持1.17版本了,
浏览器打开 https://golang.google.cn/dl/ go下载页面,选择1.17最新版本下载并替换本地GO版本!
。。。。貌似还是报错,仔细看了一眼 https://github.com/kubernetes-sigs/kubebuilder/releases我还是换个kubebuilder版本吧…
kubebuilder 版本3.4.1

生成目录结构如下:
重点关注一下config/default/ kustomization.yaml文件:
现在能看懂的配置,命名空间!我这里就不修改了采用默认的配置!

目录结构如下
注意:关于 domain group version kind对应 :

简单创建一个crd
api/v1/redis_type.go

随手演示删除Foo字段,添加一个Port字段,设置port字段为int类型!
以test目录下yaml文件定制crd
test/redis.yaml
make install创建crd

关于reconcile
controllers/redis_controller.go
关于reconcile就先不求甚解了

本地调试 make run
终端一运行
终端2运行
观察终端1 得到如下输出:
初步发布到kubernetes集群
注:我的环境安装了podman,关于podman自行百度,镜像仓库使用了腾讯云镜像仓库个人版
关于Podman
先修改docker构建命令为podman!
podman login将密码记住…基本跟docker的使用方法差不多
dockerhub加速
的特别强调一下dockerhub加速
文件末尾添加了加速地址!
重启podman服务
构建发布镜像
Dockerfile文件中添加GOPROXY

顺便发现腾讯云个人仓库一个不显示OCI-Image大小的bug…

注:过程很曲折。中间有镜像下不动的科学上网了,自行脑部。如“gcr.io/distroless/static:nonroot镜像我的操作环境为rocky linux 8.5下载不动的时候我直接科学上网了…
发布方式:
make又失败了最终根据Makefile中deploy手动执行如下命令:
注意:两条命令都是在kube-oprator1项目根目录下执行的!

默认命名空间没有修改查看kube-oprator1-system namespace命名空间下pod状态!
理论上pod是没有部署成功的,原因如下:gcr.io/kubebuilder/kube-rbac-proxy:v0.11.0无法下载…我是用的苯方法,rocky开发机科学上网下载镜像上传到腾讯与镜像仓库,然后pull镜像到kuberntes机器work节点。当然了work节点我只有一个测试环境还好~
等待pod running
CRD自定义资源简单验证
以test/redis.yaml为例
参照 https://book.kubebuilder.io/reference/markers/crd-validation.html 就设置一下port的范围!

make install 依然失败!还是手动命令了
注:这个地方我做错了好几次,原因是我以为make install =kustomize build config/crd | kubectl apply -f -,仔细看了一眼Makefile:
包含manifests的步骤,尝试了一下果然如此!


得到如下输出:端口小于1024无法创建成功
修改 test/redis.yaml port: 1024

webhook简单测试
简单准入控制器webhook create

kube-oprator1 api/v1目录下增加了webhook的相关文件,做了一个简单的验证** name=zhangpeng**
证书管理cert-manager:
访问 https://github.com/cert-manager/cert-manager/releases下载页面,1.19.0是alpha版本 我用了1.18.2的版本!


注意:镜像依然要科学上网下载
修改文件
config/default/kustomization.yaml下箭头标注部分解开注释

**config/manager/manager.yaml **
删除crd
make uninstall可以不过我的make总是失败…直接删除了!

打包镜像发布
打包发布镜像,其实最好应该修改一个镜像标签tag,这里就演示 就先这样了!make install 也不能用不知道那里有问题了 直接复制Makefile中的命令了!构建镜像并发布镜像!




恩修改成zhangpeng1
创建成功了?看一下make run,but make run无法运行了?
本地调试模式注释掉main.go SetupWebhookWithManager


总结:
1.注意开发工具之间版本的匹配
2.make 失败时候看一下Makefile中相关命令可以手动运行一下
3.资源的清理,本地调试模式
4.接下来准备设计一个简单的Operator?
边栏推荐
- 阿霍的三个阶段
- MySQL common functions
- Three stages of aho
- [Suanli network] technological innovation of Suanli Network -- key technology of operation service
- 使用nvm管理nodejs(把高版本降级为低版本)
- ASP.NET Core 6 从入门到企业级实战开发应用技术汇总
- Which securities company has a low, safe and reliable account opening commission
- Circular linked list--
- Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
- List of QT players [easy to understand]
猜你喜欢

基因检测,如何帮助患者对抗疾病?

Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.

Double linked list related operations

Chapter 14 signals (IV) - examples of multi process tasks

logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable
![[today in history] July 1: the father of time sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world](/img/41/76687ea13e1722654b235f2cfa66ce.png)
[today in history] July 1: the father of time sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]

IOS interview

MySQL common functions
![leetcode:226. Flip binary tree [DFS flip]](/img/b8/6c5596ac30de59f0f347bb0bddf574.png)
leetcode:226. Flip binary tree [DFS flip]
随机推荐
Topic 1004: the story of cows (recursion)
codeforces -- 4B. Before an Exam
使用nvm管理nodejs(把高版本降级为低版本)
Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
How to use opcache, an optimization acceleration component of PHP
系统测试UI测试总结与问题(面试)
Fatal error: execution: there is no such file or directory
Quickly understand what the compressed list in redis is
[brain opening] west tide and going to the world series
编译调试Net6源码
Good luck brought by years of persistence
硬阈值(Hard Thresholding)函数解读[通俗易懂]
数据库之MHA高可用集群部署及故障切换
BIM and safety in road maintenance-buildSmart Spain
How can genetic testing help patients fight disease?
华为面试题: 招聘
Tencent Li Wei: deeply cultivate "regulatory technology" to escort the steady and long-term development of the digital economy
How to play with the reading and writing operations of blocking sockets?
Share several tools for designing exquisite circuit diagrams