天天看點

【從入門到放棄-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

,轉載請注明出處

繼續閱讀