kubernetes CRD

CRD: CustomResourceDefinition

CRD是Kubernetes为提高可扩展性,让开发者去自定义资源(如Deployment,StatefulSet等)的一种方法。Operator = CRD + Controller

CRD仅仅是资源的定义,而Controller可以去监听CRD的CRUD事件来添加自定义业务逻辑。

 

网上很多老的教程关于crd的,都有点旧了,按照yaml走可能不成功的,看博文以前注意观察文章创建时间,尽量看官网原文。

官网链接:https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/

创建CRD用的yaml: resourcedefinition.yaml 

 

apiVersion: apiextensions.k8s.io/v1
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
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                cronSpec:
                  type: string
                image:
                  type: string
                replicas:
                  type: integer
  # 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

 

 

 

命令查看CRD对象

 

kubectl apply -f resourcedefinition.yaml

 

 

创建一个刚创建的CronTab crd的一个实例 my-crontab.yaml 

apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
  name: my-new-cron-object
spec:
  cronSpec: "* * * * */5"
  image: my-awesome-cron-image

 

# 创建
kubectl apply -f my-crontab.yaml

# 查看
kubectl get crontab

# 输出yaml定义
kubectl get ct -o yaml

# 删除crd
kubectl delete -f resourcedefinition.yaml

 

posted @ 2020-07-28 10:34  我喜欢旅行  阅读(699)  评论(0编辑  收藏  举报