【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

前言

上文【从入门到放弃-Kubernetes】Kubernetes进阶-pod水平自动伸缩(hpa),我们学习了如何对标准资源Deployment、replication controller、replica set等内置资源进行水平自动伸缩。

实际在生产中(一般较大的公司)不太会用到这些内置标准资源,都会使用CustomResources自定义资源进行灵活的扩展,来满足复杂多变的业务需求。

CustomResources是Kubernetes API的扩展,可以通过动态注册,控制资源在集群中的运行状态。

安装CustomResources后,就可以和内置资源如pod一样,通过kubectl创建和访问对象。

下面我们将CustomResources简称为CR。

定义CRD

添加CR通常有两种方式,通过CRD(CustomResourceDefinitions)的方式和使用聚合API的方式。

CRD的方式使用简单,聚合API的方式更灵活。

选型建议如下:

【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

更详细的对比可以在官方文档中查看详情。

这里我们使用CRD的方式创建,有两种版本的CRD定义方式分别如下

apiextensions.k8s.io/v1

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

apiextensions.k8s.io/v1beta1

# Deprecated in v1.16 in favor of apiextensions.k8s.io/v1
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文件
kubectl apply -f crd.yaml

# 开启api server http代理
kubectl proxy --port=8080 &

# 访问/apis/stable.example.com/v1/namespaces/*/crontabs查看
curl http://127.0.0.1:8080/apis/stable.example.com/v1/namespaces/*/crontabs

# 查看crd
kubectl get crd

可以看到类似以下输出

【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

创建CR对象

apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
  name: my-new-cron-object
spec:
  cronSpec: "* * * * */5"
  image: my-awesome-cron-image
# 创建CR对象
kubectl apply -f cr-object.yaml

# 查看crontab资源
kubectl get crontab

# 查看crontab yaml文件
kubectl get crontab -o yaml

【从入门到放弃-Kubernetes】Kubernetes进阶-CustomResources

删除CRD

# 删除CRD
kubectl delete -f crd.yaml

# 再次查看crontab资源,会看到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)
kubectl get crontabs

总结

本文学习了如何通过CRD来创建CR,实际上通常还需要通过Custom controllers监听资源的变化,在周期内的各个阶段做相应的处理。
接下里会开始学习Custom controllers的开发。

更多文章

见我的博客:https://nc2era.com

written by AloofJr,转载请注明出处

上一篇:C# WPF定时器


下一篇:【从入门到放弃-ZooKeeper】ZooKeeper实战-分布式队列