KEDA能夠對事件驅動的Kubernetes工做負載進行細粒度的自動縮放(包括從零到零的自動縮放)。 KEDA充當Kubernetes Metrics服務器,容許用戶使用專用的Kubernetes自定義資源定義來定義自動縮放規則。git
KEDA能夠在雲和邊緣上運行,能夠與Kubernetes組件(例如Horizontal Pod Autoscaler)進行本地集成,而且沒有外部依賴性。github
KEDA在Kubernetes中扮演着兩個關鍵角色。首先,它充當代理來激活和停用部署,以在無事件的狀況下從零擴展到零。其次,它充當Kubernetes指標服務器,將豐富的事件數據(例如隊列長度或流滯後)暴露給水平Pod自動縮放器以推進橫向擴展。而後由部署決定是否直接從源中使用事件。這樣能夠保留豐富的事件集成,並使完成或放棄隊列消息之類的手勢能夠當即使用。api
KEDA有許多「scalers」,它們既能夠檢測是否應激活或停用部署,也能夠提供特定事件源的自定義指標。今天,對如下內容提供了縮放器支持:服務器
固然其餘事件源正在增長中,以下:架構
規劃中app
待規劃ide
ScaledObject自定義資源定義ui
爲了使部署與事件源同步,須要部署ScaledObject自定義資源。 ScaledObjects包含有關要擴展的部署的信息,事件源的元數據(例如,鏈接字符串密鑰,隊列名稱),輪詢間隔和冷卻時間。 ScaledObject將產生相應的自動擴展資源(HPA定義)以擴展部署。刪除ScaledObjects時,將清除相應的HPA定義。this
例如:spa
apiVersion: keda.k8s.io/v1alpha1 kind: ScaledObject metadata: name: kafka-scaledobject namespace: default labels: deploymentName: azure-functions-deployment spec: scaleTargetRef: deploymentName: azure-functions-deployment pollingInterval: 30 triggers: - type: kafka metadata: # Required brokerList: localhost:9092 consumerGroup: my-group # Make sure that this consumer group name is the same one as the one that is consuming topics topic: test-topic lagThreshold: "50"
可使用helm部署,也能夠yaml部署。利用yaml部署能夠執行以下操做:
kubectl apply -f KedaScaleController.yaml
KedaScaleController.yaml 以下:
apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: name: scaledobjects.keda.k8s.io spec: group: keda.k8s.io version: v1alpha1 names: kind: ScaledObject singular: scaledobject plural: scaledobjects shortNames: - sco categories: - keda scope: Namespaced additionalPrinterColumns: - name: Deployment type: string JSONPath: .spec.scaleTargetRef.deploymentName - name: Triggers type: string JSONPath: .spec.triggers[*].type - name: Age type: date JSONPath: .metadata.creationTimestamp validation: openAPIV3Schema: properties: spec: required: [triggers] type: object properties: scaleType: type: string enum: [deployment, job] pollingInterval: type: integer cooldownPeriod: type: integer minReplicaCount: type: integer maxReplicaCount: type: integer scaleTargetRef: required: [deploymentName] type: object properties: deploymentName: type: string containerName: type: string triggers: type: array items: type: object required: [type, metadata] properties: type: type: string authenticationRef: type: object properties: name: type: string metadata: type: object additionalProperties: type: string --- apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: name: triggerauthentications.keda.k8s.io spec: group: keda.k8s.io version: v1alpha1 names: kind: TriggerAuthentication singular: triggerauthentication plural: triggerauthentications shortNames: - ta - triggerauth categories: - keda scope: Namespaced --- apiVersion: v1 kind: Namespace metadata: name: keda --- kind: ServiceAccount apiVersion: v1 metadata: name: keda-operator namespace: keda --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: keda-operator-service-account-role-binding roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: keda-operator namespace: keda --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: keda:system:auth-delegator roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: system:auth-delegator subjects: - kind: ServiceAccount name: keda-operator namespace: keda --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: keda-auth-reader namespace: kube-system roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: extension-apiserver-authentication-reader subjects: - kind: ServiceAccount name: keda-operator namespace: keda --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: keda-operator name: keda-operator namespace: keda spec: replicas: 1 selector: matchLabels: app: keda-operator template: metadata: labels: app: keda-operator name: keda-operator spec: serviceAccountName: keda-operator containers: - name: keda-operator image: kedacore/keda:latest args: - /adapter - --secure-port=6443 - --logtostderr=true - --v=2 ports: - containerPort: 6443 name: https - containerPort: 8080 name: http volumeMounts: - mountPath: /tmp name: temp-vol volumes: - name: temp-vol emptyDir: {} --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: custom-metrics-resource-reader roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: custom-metrics-resource-reader subjects: - kind: ServiceAccount name: keda-operator namespace: keda --- apiVersion: v1 kind: Service metadata: name: keda-operator namespace: keda spec: ports: - name: https port: 443 targetPort: 6443 - name: http port: 80 targetPort: 8080 selector: app: keda-operator --- apiVersion: apiregistration.k8s.io/v1beta1 kind: APIService metadata: name: v1beta1.external.metrics.k8s.io spec: service: name: keda-operator namespace: keda group: external.metrics.k8s.io version: v1beta1 insecureSkipTLSVerify: true groupPriorityMinimum: 100 versionPriority: 100 --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: custom-metrics-resource-reader rules: - apiGroups: - "" resources: - namespaces - pods - services - external verbs: - get - list --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: keda-hpa-controller-custom-metrics roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: horizontal-pod-autoscaler namespace: kube-system
關鍵代碼在pkg文件夾下,以下圖:
舉例說明一下,當使用客戶端--kubectl 或是client-go部署一個針對deployment A 的ScaledObject crd。想根據kafaka的消息積壓數目進行hpa。那麼controller會監聽到建立了crd,將會對新增動做作出操做。具體就是,根據crd的具體內容建立一個hpa對象,crd 的spec 內容會轉換成hpa 。此時官方k8s的hpa就會經過scalers中的kafka scaler 讀取kafka指定topic的消息數目,而後最終由hpa controller 作出是否擴縮的決定。
KEDA 目前處於Experimental Phase 階段。微軟和紅帽但願社區共同參與。
KEDA 並無實現了本身的HPA,其實最終起做用的依舊是社區中的HPA,他只是根據crd 內容生成了HPA 對象,只不過這個metrics 是外部metrics。KEDA 主要是集成了各類事件源。