Custom resource definition(CRD)
Last updated
Was this helpful?
Last updated
Was this helpful?
CRD简单的例子
写一个controller处理CRD
sample-controller: 放在go/src/k8s.io目录下
关于subresource(status/scale)及category
如果要生成status相关的方法,删除CRD数据结构上的下面tag即可:
如果要生成scale
相关的方法,需要在CRD数据结构上加上一些tag:
使用scale
subresource可以很方便地实现自动扩缩容。
可以通过 对custom resource(CR)进行校验,否则如果用户创建的CR包含有不合规则的字段,就会导致CRD相应的controller没法正常工作,比如:不能正常list这些CR(解析时会出错)。
如果CRD里要校验的字段太多,可以考虑下面两种方式:
使用进行校验
利用工具自动生成OpenAPI schema
CRD registry
相关的代码在staging/src/k8s.io/apiextensions-apiserver/pkg/registry下。
ETCD存储
CRD存储在etcd上的路径与deployment、pod等核心资源的存储位置不一样,它的路径为:root / resource.Group + "/" + resource.Resource
(见staging/src/k8s.io/apiextensions-apiserver/pkg/apisever/customresource_handler.go
#574,注:在project里搜索“ResourcePrefix”可以搜到其他resource的存储路径)。
注: tapp为/registry/gaia/tapps/default/example-tapp。为了平滑升级,兼容以前的版本,我们需要修改tapp的存储路径。
Note: kubebuilder does not exist as an example to copy-paste, but instead provides powerful libraries and tools to simplify building and publishing Kubernetes APIs from scratch.
This project is a component of the , an open source toolkit to manage Kubernetes native applications, called Operators, in an effective, automated, and scalable way.
使用operator-sdk时可参考。
Kubebuilder is a framework for building Kubernetes APIs using .
Metacontroller is an add-on for Kubernetes that makes it easy to write and deploy in the form of .
Awesome Operators in the Wild:
Operator hub:
Extend the Kubernetes API with CustomResourceDefinitions:
Building an operator for Kubernetes with the sample-controller:
Building an operator for Kubernetes with kubebuilder:
Building an operator for Kubernetes with operator-sdk:
Kubernetes Operators Best Practices: