前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >kubernetes学习记录(14)——使用CustomResourceDefinitions(CRD)扩展Kubernetes API

kubernetes学习记录(14)——使用CustomResourceDefinitions(CRD)扩展Kubernetes API

作者头像
胡了了
发布2020-02-13 10:41:41
1.6K0
发布2020-02-13 10:41:41
举报

工作中即将开始写operator,先提前学习一下相关的知识

目前我们的kubernetes集群版本为1.15.0,故参考文档为官方文档《Extend the Kubernetes API with CustomResourceDefinitions》

创建一个CustomResourceDefinition

  • 官方例子
代码语言:javascript
复制
# resourcedefinition.yaml
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct
  preserveUnknownFields: false
  validation:
    openAPIV3Schema:
      type: object
      properties:
        spec:
          type: object
          properties:
            cronSpec:
              type: string
            image:
              type: string
            replicas:
              type: integer
  • 根据yaml创建crd
代码语言:javascript
复制
# kubectl apply -f resourcedefinition.yaml
customresourcedefinition.apiextensions.k8s.io/crontabs.stable.example.com created
# kubectl get crd | grep crontabs
crontabs.stable.example.com             2020-01-08T07:49:53Z

创建自定义对象

  • 根据上文定义的CRD,创建CRD对象
代码语言:javascript
复制
# my-crontab.yaml
apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
  name: my-new-cron-object
spec:
  cronSpec: "* * * * */5"
  image: my-awesome-cron-image
代码语言:javascript
复制
# kubectl apply -f my-crontab.yaml
# kubectl get crontab
NAME                 AGE
my-new-cron-object   28s

删除自定义对象

删除CustomResourceDefinition时,服务器将卸载RESTful API端点并删除其中存储的所有自定义对象

代码语言:javascript
复制
# kubectl delete -f resourcedefinition.yaml
# kubectl get crontabs
Error from server (NotFound): Unable to list "stable.example.com/v1, Resource=crontabs": the server could not find the requested resource (get crontabs.stable.example.com)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建一个CustomResourceDefinition
    • 创建自定义对象
      • 删除自定义对象
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档