当前位置:网站首页>使用CRD扩展Kubernetes API
使用CRD扩展Kubernetes API
2022-06-21 22:14:00 【Mr.Cylon】
Intorduction CRD
什么是CRD
在 Kubernetes API 中,resources 是存储 API 对象集合的endpoint。例如,内置 Pod resource 包含 Pod 对象的集合。当我们想扩展API,原生的Kubernetes就不能满足我们的需求了,这时 CRD (CustomResourceDefinition) 就出现了。在 Kubernetes 中创建了 CRD 后,就可以像使用任何其他原生 Kubernetes 对象一样使用它,从而利用 Kubernetes 的所有功能、如安全性、API 服务、RBAC 等。
Kubernetes 1.7 之后增加了对 CRD 自定义资源二次开发能力来扩展 Kubernetes API,通过 CRD 我们可以向 Kubernetes API 中增加新资源类型,而不需要修改 Kubernetes 源码来创建自定义的 API server,该功能大大提高了 Kubernetes 的扩展能力。
创建 CRD
前提条件: Kubernetes 服务器版本必须不低于版本 1.16
再创建新的 CustomResourceDefinition(CRD)时,Kubernetes API 服务器会为指定的每一个版本生成一个 RESTful 的资源路径。(即定义一个Restful API)。CRD 可以是namespace作用域的,也可以是cluster作用域的,取决于 CRD 的 scope 字段设置。和其他现有的内置对象一样,删除一个namespace时,该namespace下的所有定制对象也会被删除。CustomResourceDefinition 本身是不受名字空间限制的,对所有名字空间可用。
例如,编写一个firewall port 规则:
# 1.16版本后固定格式
apiVersion: apiextensions.k8s.io/v1
# 类型crd
kind: CustomResourceDefinition
metadata:
# 必须为name=spec.names.plural + spec.group
name: ports.firewalld.fedoraproject.org
spec:
# api中的group
# /apis/<group>/<version>/<plural>
group: firewalld.fedoraproject.org
# 此crd作用于 可选Namespaced|Cluster
scope: Namespaced
names:
# 名字的复数形式,用于api
plural: ports
# 名称的单数形式。用于命令行
singular: port
# 种类,资源清单类型
kind: PortRule
# 名字简写,类似允许 CLI 上较短的字符串匹配的资源
shortNames:
- fp
versions:
# 定义版本的类型
- name: v1
# 通过 served 标志来启用或禁止
served: true
# 其中一个且只有一个版本必需被标记为存储版本
storage: true
# 自定义资源的默认认证的模式
schema:
# 使用的版本
openAPIV3Schema:
# 定义一个参数为对象类型
type: object
# 这个参数的类型
properties:
# 参数属性spec
spec:
# spec属性的类型为对象
type: object
# 对象属性
properties:
# spec属性name
name:
# 类型为string
type: string
port:
type: integer
host:
type: string
isPermanent:
type: boolean
需要注意的是v1.16版本以后已经 GA了,使用的是v1版本,之前都是vlbeta1,定义规范有部分变化,所以要注意版本变化。
这个地方的定义和我们定义普通的资源对象比较类似,我们说我们可以随意定义一个自定义的资源对象,但是在创建资源的时候,肯定不是任由我们随意去编写YAML文件的,当我们把上面的CRD文件提交给Kubernetes之后,Kubernetes会对我们提交的声明文件进行校验,从定义可以看出CRD是基于 OpenAPIv3 schem 进行规范的。当然这种校验只是对于字段的类型进行校验,比较初级,如果想要更加复杂的校验,这个时候就需要通过Kubernetes的admission webhook来实现了。关于校验的更多用法,可以前往官方文档查看。
创建一个crd类型资源
apiVersion: "firewalld.fedoraproject.org/v1"
kind: PortRule
metadata:
name: http-port
spec:
name: "nginx"
port: 80
host: "10.0.0.3"
isPermanent: false
查看创建的crd
# kubectl get t
NAME CREATED AT
firewallds.port.fedoraproject.org 2022-06-19T09:27:09Z
Reference
边栏推荐
- redis主从复制(九)
- Redis master-slave replication (9)
- 关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
- Mono 的创建
- 211高校神级硕士论文刷屏!75行字错了20行!学校回应:导师停招...
- Nearly 90% of servers can be saved, but the anti fraud efficiency has greatly increased. Why is PayPal's plan to break the "Ai memory wall" so cost-effective?
- IPD chip shipments exceeded 1billion, and chips and semiconductors appeared in ims2022
- Notes on the development of raspberry pie (16): Raspberry pie 4b+ install MariaDB database (MySQL open source branch) and test basic operations
- 211 thèse de maîtrise en divinité à l'Université! 75 lignes et 20 mauvaises lignes! Réponse de l'école: le tuteur a arrêté d'inscrire...
- Go语言学习教程(十二)
猜你喜欢

所谓的0拷贝不就是为了让CPU休息吗?深入理解mmap

211高校神级硕士论文刷屏!75行字错了20行!学校回应:导师停招...

硬件开发笔记(三):硬件开发基本流程,制作一个USB转RS232的模块(二):设计原理图库

基于Arduino框架下VSCode PlatformIO一个项目配置两种不同开发板的兼容模式
![class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist](/img/1a/294eb0128285686ede415991f69be7.png)
class path resource [classpath*:mapper/*.xml] cannot be opened because it does not exist

CVPR2022 | 弱监督多标签分类中的损失问题

目标检测、视觉弱监督学习、大脑多模态成像技术等CV综述来了!图像图形学发展年度报告综述专刊!

Unity-网络开发(二)

How to modify the judgment time of the equipment in the offline state when the national standard equipment is registered on the easycvr platform?

洞見數據價值,啟迪數字未來,《數字化的力量》問世
随机推荐
硬件开发笔记(三):硬件开发基本流程,制作一个USB转RS232的模块(二):设计原理图库
Redis master-slave replication (9)
Win11 hotspot connection successful but no network? Solution of win11 mobile hotspot and network conflict
Component value transfer: props are used for parent component and child component value transfer
Creation of mono
组件传值:子组件向父组件传递数据
Today's sleep quality record 81 points
二叉排序树
Why applets and the industrial Internet can complement each other
6月编程语言排行榜已出,这门语言要“封神”
What if the program input point cannot be located in the dynamic link library
關於 麒麟系統開發錯誤“fatal error: GL/gl.h: No such file or directory“ 的解决方法
洞见数据价值,启迪数字未来,《数字化的力量》问世
基于Arduino框架下VSCode PlatformIO一个项目配置两种不同开发板的兼容模式
Google AI big model lamda can one day replace the search engine? Google researcher: search can be re imagined as a two-way dialogue between users and languages
被八股文害惨了。。。。
Kirin System Development Notes (V): making and installing the startup USB flash disk of the Kirin system, installing the Kirin system on the physical machine, and building a Qt development environment
Hardware development notes (V): basic process of hardware development, making a USB to RS232 module (IV): creating con connection device package and associating principle element devices
211高校神级硕士论文刷屏!75行字错了20行!学校回应:导师停招...
Unity network development (I)