Kubernetes Notes
  • README
  • 序:Kubernetes之道
  • 搭建开发环境
    • 搭建本地开发测试环境
  • 基本概念
    • Workloads
      • StatefulSet
    • 认证鉴权
      • 理解证书
      • Kubelet配置
    • 垃圾回收
    • Service
  • 基础组件
    • Scheduler
    • 自动扩缩容(HPA)
  • 容器运行时(Container Runtime)
    • CRI
    • Docker
      • 安装配置Docker
      • Docker FAQ
    • Containerd
  • 资源隔离与限制
    • 基础知识
      • Cgroup
      • Namespaces
    • CPU
    • Memory
    • 网络出/入带宽
    • GPU
    • Container
    • FAQ
  • 网络
    • 基础知识
      • Linux network interfaces
      • Iptables
      • Overlay network
    • CNI
    • Flannel
      • Flannel原理
      • host-gw
      • vxlan
      • ipip
    • Calico
    • Cilium
  • Service Mesh
    • Istio
  • 扩展Kubernetes
    • Admission controller
    • Custom resource definition(CRD)
    • Scheduler extender
    • 扩展资源维度
    • Kubectl plugin
    • Aggregator
    • Device plugin
  • 现网问题
    • Kubernetes
    • ETCD
    • Docker
  • 最佳实践
    • 各组件参数配置调优
    • 各大公司生产环境实践
    • 如何打造一个Kubernetes平台
  • 生产力小工具
    • 生成特定权限和配额的kubeconfig
  • 社区贡献
  • 学习资料
  • 附录:RTFSC
    • Informer
    • Pod deletion
Powered by GitBook
On this page
  • 1. 简介
  • 2. 实现
  • 3. 自动化工具
  • 4. 一些有趣的CRD/Operator
  • 5. 参考资料

Was this helpful?

  1. 扩展Kubernetes

Custom resource definition(CRD)

PreviousAdmission controllerNextScheduler extender

Last updated 5 years ago

Was this helpful?

1. 简介

  • CRD简单的例子

  • 写一个controller处理CRD

    sample-controller: 放在go/src/k8s.io目录下

  • 关于subresource(status/scale)及category

    如果要生成status相关的方法,删除CRD数据结构上的下面tag即可:

    // +genclient:noStatus

    如果要生成scale相关的方法,需要在CRD数据结构上加上一些tag:

    Similar to how an UpdateStatus() method exists for the status subresource, we can generate the GetScale() and UpdateScale() methods for the scale subresource by adding the following tags on the Database type.
    
    // +genclient:method=GetScale,verb=get,subresource=scale,result=k8s.io/api/autoscaling/v1.Scale
    // +genclient:method=UpdateScale,verb=update,subresource=scale,input=k8s.io/api/autoscaling/v1.Scale,result=k8s.io/api/autoscaling/v1.Scale

    使用scale subresource可以很方便地实现自动扩缩容。

  • 可以通过 对custom resource(CR)进行校验,否则如果用户创建的CR包含有不合规则的字段,就会导致CRD相应的controller没法正常工作,比如:不能正常list这些CR(解析时会出错)。

    如果CRD里要校验的字段太多,可以考虑下面两种方式:

    • 使用进行校验

    • 利用工具自动生成OpenAPI schema

2. 实现

  • 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的存储路径。

3. 自动化工具

  • 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.

4. 一些有趣的CRD/Operator

5. 参考资料

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:

https://kubernetes.io/docs/tasks/access-kubernetes-api/extend-api-custom-resource-definitions/
https://github.com/kubernetes/sample-controller
https://github.com/kubernetes/community/blob/master/contributors/devel/controllers.md
https://blog.openshift.com/kubernetes-custom-resources-grow-up-in-v1-10/
CRD validation
OpenAPI v3 schema
validatingadmissionwebhook
https://github.com/kubernetes-sigs/controller-tools/tree/master/cmd/crd
https://github.com/ant31/crd-validation
operator-sdk
Operator Framework
operator-sdk get started
Kubebuilder
custom resource definitions (CRDs)
metacontroller
custom controllers
simple scripts
https://github.com/operator-framework/awesome-operators
https://operatorhub.io
https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/
https://itnext.io/building-an-operator-for-kubernetes-with-the-sample-controller-b4204be9ad56
https://itnext.io/building-an-operator-for-kubernetes-with-kubebuilder-17cbd3f07761
https://itnext.io/building-an-operator-for-kubernetes-with-operator-sdk-40a029ea056
https://blog.openshift.com/kubernetes-operators-best-practices/