【從入門到放棄-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的方式更靈活。

選型建議如下:

更詳細的對比可以在官方文檔中查看詳情。

這裏我們使用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

可以看到類似以下輸出

創建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

刪除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,轉載請註明出處

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章