天天看點

#yyds幹貨盤點#--k8s-擴充資源類型

在k8s上擴充資源類型的方式有三種:

  • 第一種是CRD,CRD是k8s内建的資源類型,該類型資源主要用來建立使用者自定義資源類型的資源;即通過CRD資源,可以将使用者自定義資源類型轉換為k8s上資源類型;
  • 第二種是自定義apiserver;這種方式要比第一種方式要複雜一點,需要使用者手動開發程式實作對應功能的apiserver,讓其使用者建立自定義類型資源能夠通過自定義apiserver實作;
  • 第三種方式就是修改現有k8sapiserver,讓其支援對應使用者自定義資源類型;

自定義資源類型我們可以使用CRD資源實作,也可以使用自定義apiserver或修改原有apiserver代碼實作.

  • 隻有資源類型是不能夠讓對應自定義類型資源執行個體化為一個自定義資源對象;
  • 隻有自定義資源類型,使用者建立對應資源類型的資源對象時,隻能把對應資源類型的定義資訊寫入到etcd中,它不能真正的跑起來.
  • 要想真正的跑起來,我們還需要一個自定義控制器,專門負責監聽對應的資源類型的資源變化,将對應資源執行個體化為對應k8s上的資源對象;
  • 當然不是所有的自定義類型的資源都需要自定義控制器,如果對應自定義類型資源調用了底層基礎控制器來管控對應自定義資源,那麼對應自定義類型資源就不需要使用自定義控制器

我們知道控制器是k8s上的一個重要元件,它的工作邏輯是注冊監聽在apiserver上對應類型的資源變動,如果對應資源狀态不滿足使用者期望狀态,它就會根據内部的和解循環來請求apiserver将對應類型資源的定義發送給它,然後根據資源定義來重建對應的資源,讓其狀态始終和使用者期望的狀态保持一緻;

自定義控制器也是同樣的邏輯,使用自定義控制器的目的也是讓對應自定義類型資源能夠被自定義控制器監聽,一旦對應資源發生變動,它能夠将其在k8s上建立出來,并一直保持和使用者期望的狀态吻合;

自定義控制器和自定義資源類型可以分開實作,也可以合并在一起實作,即自定義控制器程式能夠自動建立CRD資源,讓其對應自定義類型資源能夠被k8s識别并将其建立出來;具體是分開實作還是合并在一起實作,取決開發自定義控制器程式員;

[root@master01 ~]# kubectl explain crd
KIND:     CustomResourceDefinition
VERSION:  apiextensions.k8s.io/v1
 
DESCRIPTION:
     CustomResourceDefinition represents a resource that should be exposed on
     the API server. Its name MUST be in the format <.spec.name>.<.spec.group>.
 
FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
 
   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
 
   metadata     <Object>
 
   spec <Object> -required-
     spec describes how the user wants the resources to appear
 
   status       <Object>
     status indicates the actual state of the CustomResourceDefinition      
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  # 名稱必須與下面的spec字段比對,格式為: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # 用于REST API的組名稱: /apis/<group>/<version>
  group: stable.example.com
  # 此CustomResourceDefinition支援的版本清單
  versions:
    - name: v1
      # 每個版本都可以通過服務标志啟用/禁用。
      served: true
      # 必須将一個且隻有一個版本标記為存儲版本。
      storage: true
  # 指定crd資源作用範圍在命名空間或叢集
  scope: Namespaced
  names:
    # URL中使用的複數名稱: /apis/<group>/<version>/<plural>
    plural: crontabs
    # 在CLI(shell界面輸入的參數)上用作别名并用于顯示的單數名稱
    singular: crontab
    # kind字段使用駝峰命名規則. 資源清單使用如此
    kind: CronTab
    # 短名稱允許短字元串比對CLI上的資源,意識就是能通過kubectl 在檢視資源的時候使用該資源的簡名稱來擷取。
    shortNames:
    - ct
kubectl create -f resourcedefinition.yaml
kubectl get contab/ct      
#vim 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 create -f my-crontab.yaml      
  • 自定義資源配置設定屬主
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: crontabs.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: crontabs
    singular: crontab
    kind: CronTab
    shortNames:
    - ct
    # categories字段指定自定義資源所屬的組
    categories:
    - all      
  • 為自定義的資源添加狀态和伸縮配置
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: crontabs.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: crontabs
    singular: crontab
    kind: CronTab
    shortNames:
    - ct
  # 自定義資源的子資源的描述
  subresources:
    # 啟用狀态子資源。
    status: {}
    # 啟用scale子資源
    scale:
      specReplicasPath: .spec.replicas
      statusReplicasPath: .status.replicas
      labelSelectorPath: .status.labelSelector      
  • 為自定義資源添加額外的列印列
apiVersion: apiextensions.k8s.io/v1beta1
  kind: CustomResourceDefinition
  metadata:
    name: crontabs.stable.example.com
  spec:
    group: stable.example.com
    version: v1
    scope: Namespaced
    names:
      plural: crontabs
      singular: crontab
      kind: CronTab
      shortNames:
      - ct
    additionalPrinterColumns:
    - name: Spec
      type: string
      description: The cron spec defining the interval a CronJob is run
      JSONPath: .spec.cronSpec
    - name: Replicas
      type: integer
      description: The number of jobs launched by the CronJob
      JSONPath: .spec.replicas
    - name: Age
      type: date
      JSONPath: .metadata.creationTimestamp      
  • 為自定義資源添加驗證
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: crontabs.stable.example.com
spec:
  group: stable.example.com
  versions:
    - name: v1
      served: true
      storage: true
  version: v1
  scope: Namespaced
  names:
    plural: crontabs
    singular: crontab
    kind: CronTab
    shortNames:
    - ct
  validation:
   # openAPIV3Schema is the schema for validating custom objects.
    openAPIV3Schema:
      properties:
        spec:
          properties:
            cronSpec: #--必須是字元串,并且必須是正規表達式所描述的形式
              type: string
              pattern: '^(\d+|\*)(/\d+)?(\s+(\d+|\*)(/\d+)?){4}$'
            replicas: #----必須是整數,最小值必須為1,最大值必須為10
              type: integer
              minimum: 1
              maximum: 10      

繼續閱讀