基於自定義Istio指標的Pod水平自動縮放

作者:Sandor Magyari

譯者:張成

審校:羅廣明

原文:https://banzaicloud.com/blog/k8s-hpa-prom-istio/

本文來自Banzai Cloud,通過一個spring-boot應用示例演示如何通過Istio operator來實現pod水平擴展。​

基於自定義Istio指標的Pod水平自動縮放

Pipeline的核心功能之一,Banzai Cloud的應用程序和devops容器管理平臺,是多維的並可以基於默認和自定義指標進行自動調節。 在我們引入自定義指標後,我們選擇了通過Prometheus適配器Prometheus收集指標。 從那時起,我們的許多客戶開始使用Hoizontal Pod Autoscaling,他們中的大多數人只對基本的CPU和內存指標感到滿意。

我們一直都知道這不是一個理想的解決方案,我們一直在努力尋求更靈活的解決方案,以便:

  • 基於自定義Prometheus指標的擴展
  • 爲更復雜的Prometheus查詢提供擴展支持

隨着我們的開源Istio operator的發佈以及在Pipeline平臺上廣泛引入基於Istio的服務網格,我們也提供了根據自定義的Istio指標的自動縮放功能。 Prometheus現在提供網絡指標,延遲,故障率以及更多指標(默認情況下從Istio中刪除)。 我們的HPA operator根據這些指標決定是否擴展。

但是,在使用Prometheus Adapter時,我們的方法已不再可行,因此我們決定探索替代方案。

如果您錯過了我們關於Pipeline平臺所支持和自動化不同類型的自動縮放功能的系列博客,請查看這些早先的帖子:

1. Autoscaling Kubernetes集羣

1. Vertical pod autoscaler

1. 自定義指標上的水平自動縮放Kubernetes部署

1. Horizontal Pod Autoscaler Kubernetes Operator

1. 重新加載水平Pod自動調節器操作員

TL;DR

  • Pipeline及其HPA Operator中對重新設計的自定義指標支持的介紹。
  • 使用來自Prometheus的Istio指標自動調整Spring Boot部署的示例
  • 深入瞭解添加自定義指標後發生了什麼
  • 切換到另一個自定義指標適配器kube-metrics-adapter的介紹

重新支持自定義指標

由於上面列出的原因,我們決定使用另一個自定義指標適配器kube-metrics-adapter

kube-metrics-adapter 是一種通用指標適配器,可以從多個來源收集和提供指標。對於每個源,都有一個Collector實現;目前,我們對其Prometheus收集器最感興趣。

Prometheus Collector是一個通用收集器。它將Prometheus 查詢映射到可由HPA 控制器用於部署自動擴展的度量標準。它的方法不同於Prometheus Adapter ,它具有預定義的規則集 - 包含針對Prometheus 運行的查詢,用於將指標轉換爲自定義指標 - 定期執行。獲取所有匹配的指標標準(因爲這些查詢必須是通用的並且定位所有pod和部署),並作爲自定義指標進行轉換和公開。相比之下,“Prometheus collector”僅收集自定義查詢返回的指標結果,其定義因部署而異,並且僅針對單個部署/ pod或服務,從而減少了存儲的指標總數。該解決方案的一個缺點是,目前,用戶應該避免執行性能不佳的查詢。

使用ISTIO自動伸縮Spring Boot應用

HPA Operator

我選擇了流行的spotguides基於MySQL的Sprint Boot 應用,以便在MySQL服務器旁,快速啓動一個輕量級Web應用程序容器。 我使用Banzai Pipeline Beta,它支持在六個雲提供商中或在本地快速啓動Kubernetes集羣。 Prometheus & Grafana Monitoring以及Istio是默認功能,可以通過UI開關輕鬆打開(Pipeline處理其餘部分)。 在這個例子中,我將在Google Cloud上啓動一個Kubernetes集羣,並打開Monitoring and Service Mesh,並在其上部署Spring Boot Spotguide。 羣集創建是我們的spotguide嚮導的一部分,但是,因爲Service MeshPipeline中的一個相對較新的功能,我們不能(迄今爲止)從spotguide的流程中啓用它, 所以我們必須事先創建集羣。

我在這個視頻中記錄了這些步驟:

一旦你的Spring Boot容器MySQL服務器啓動並運行,你就可以從Spotguid 總覽打開演示程序。這是一個演示應用程序,是典型的Spring Web應用程序。我們已經創建了一個GitHub代碼庫,你可以在裏面找到示意代碼(就像我們的其他spotguides一樣)。默認情況下,應用程序URL將僅返回運行狀況檢查狀態,該狀態連接到Kubernetes活動和準備情況檢查。然後,JVM指標將暴露給Prometheus,並且repo將集成到我們的CI/CD流中,因此,一旦您提交業務邏輯,它將自動部署到集羣。在這裏閱讀更多關於我們的spotguides。

您可以從羣集列表或詳細信息頁面下載Kubernetes配置,以便稍後從本地計算機獲取對羣集的kubectl訪問權限。

我正在使用hey工具(go get -u github.com/rakyll/hey)來生成針對Spring應用程序的負載;下面的命令將每秒發出50個請求,總計10000個請求:

hey -n 10000 -q 50 https://spring-boot-custom-metrics-demo.sancyx5g25.sancyx.beta.banzaicloud.io/actuator/health/kubernetes

現在,打開監控(我們的Spotguide摘要或羣集詳細信息頁面中提供的鏈接)以查看可用的指標並確定您的Prometheus 查詢。 因爲我們啓用了Service Mesh,所有網絡通信都將通過Envoy代理,將指標發送到Istio遙測服務,該服務由Prometheus抓取。

我將基於我的示例,查詢基於istiorequeststotal指標,該指標與Spring容器相關:

sum(rate(istio_requests_total{destination_service="spring-boot-custom-metrics-demo-spotguide-spring-boot.default.svc.cluster.local",destination_service_name="spring-boot-custom-metrics-demo-spotguide-spring-boot",destination_service_namespace="default",destination_workload="spring-boot-custom-metrics-demo-spotguide-spring-boot"}[1m]))

請務必添加相應的標籤過濾器,以便專門選擇與您的pod/部署相關的指標。

從部署列表頁面上的Horizontal Pod Autoscaler菜單中選擇spring-boot-custom-metrics-demo-spotguide-spring-boot部署,以訪問HPA Edit頁面。 在那裏,您可以通過輸入自定義指標或查詢的名稱來設置cpu,內存和自定義指標。

現在,如果您在更長的時間內生成另一個負載(50個請求/秒),則應在屬於部署的HPA中增加副本計數:

hey -n 50000 -q 50 https://spring-boot-custom-metrics-demo.sancyx5g25.sancyx.beta.banzaicloud.io/actuator/health/kubernetes

...

kubectl get hpa

NAME                                                   REFERENCE                                                 TARGETS      MINPODS   MAXPODS   REPLICAS   AGE
spring-boot-custom-metrics-demo-spotguide-spring-boot   Deployment/spring-boot-custom-metrics-demo-spotguide-spring-boot   245866m/40   1         10        7          80s

加載結束後,副本數將慢慢減少到默認的最小值:

kubectl get hpa

NAME                                                   REFERENCE                                                 TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
spring-boot-custom-metrics-demo-spotguide-spring-boot   Deployment/spring-boot-custom-metrics-demo-spotguide-spring-boot   266m/40   1         10        1          32m

背後的場景

讓我們深入瞭解使用Pipeline UI向部署添加自定義指標時幕後發生的情況。 這個流程非常類似於資源指標,您可能會從之前的一系列帖子中熟悉這些指標,但是現在,讓我們關注與自定義指標相關的API。

Pipeline UI使用HPA API創建/更新指標:

PUT {{pipeline_url}}/api/v1/orgs/:orgId/clusters/:clusterId/hpa

{
  "scaleTarget": "example-deployment-name",
  "minReplicas": 1,
  "maxReplicas": 10,
  "customMetrics": {
    "customMetricName": {
      "query": "sum({kubernetes_pod_name=~\"^example-deployment-name-pod.*\",__name__=~\"example-metric-name\"})",
      "targetAverageValue": "100"
    }
  }
}

如果要指定絕對值,可以使用targetValue而不是targetAverageValue。 不同之處在於,targetAverageValue使用當前pod副本計數對獲取的度量值進行平均。

Pipeline實際上做的是爲您的部署添加以下注釋:

    hpa.autoscaling.banzaicloud.io/maxReplicas=10
    hpa.autoscaling.banzaicloud.io/minReplicas=1
    prometheus.customMetricName.hpa.autoscaling.banzaicloud.io/query=sum({kubernetes_pod_name=~\"^example-deployment-name-pod.*\",__name__=~\"example-metric-name\"})

所有這些都是通過HPA operator實現的,它根據部署註釋管理HPA資源。 Pipeline在羣集創建完成後,通過一個post hook,部署HPA operator,然後,如果metrics.api尚未註冊,則部署kube-metrics-adaptermetrics-server。 此過程可能會有所不同,具體取決於您的雲提供商和K8s版本,這些版本可能已安裝了某些功能。在我們的例子中,新創建的HPA對象將如下所示:

apiVersion: v1
items:
- apiVersion: autoscaling/v1
  kind: HorizontalPodAutoscaler
  metadata:
    annotations:
    ...
      autoscaling.alpha.kubernetes.io/current-metrics: '[{"type":"Object","object":{"target":{"kind":"Pod","name":"example-deployment-name-customMetricName","apiVersion":"v1"},"metricName":"customMetricName","currentValue":"222m"}}]'
      autoscaling.alpha.kubernetes.io/metrics: '[{"type":"Object","object":{"target":{"kind":"Pod","name":"example-deployment-name-customMetricName","apiVersion":"v1"},"metricName":"customMetricName","targetValue":"40"}}]'
      metric-config.object.customMetricName.prometheus/per-replica: "true"
      metric-config.object.customMetricName.prometheus/query: sum({kubernetes_pod_name=~\"^example-deployment-name-pod.*\",__name__=~\"example-metric-name\"})
    ...
  spec:
    maxReplicas: 10
    minReplicas: 1
    scaleTargetRef:
      apiVersion: apps/v1
      kind: Deployment
      name: example-deployment-name
  status:
    currentReplicas: 1
    desiredReplicas: 1
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

請注意,自定義度量標準是Object類型,並綁定到名爲example-deployment-name-customMetricName的Pod資源。 Kube-metrics-adapter也使用註釋查詢來獲取度量值,該值在以下端點公開/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/example-deployment-name -customMetricName/customMetricName,並且它正是HPA控制器將要查找的位置:

$ kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/customMetricName" | jq .
{
  "kind": "MetricValueList",
  "apiVersion": "custom.metrics.k8s.io/v1beta1",
  "metadata": {
    "selfLink": "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/customMetricName"
  },
  "items": [
    {
      "describedObject": {
        "kind": "Pod",
        "namespace": "default",
        "name": "example-deployment-name-customMetricName",
        "apiVersion": "v1"
      },
      "metricName": "customMetricName",
      "timestamp": "2019-03-13T20:23:32Z",
      "value": "222m"
    }
  ]
}

很高興你閱讀本文,希望你有一個關於自動縮放愉快的實踐體驗!

Service Mesh Meetup 成都站

關於 ServiceMesher 社區

ServiceMesher 社區是由一羣擁有相同價值觀和理念的志願者們共同發起,於 2018 年 4 月正式成立。

社區關注領域有:容器、微服務、Service Mesh、Serverless,擁抱開源和雲原生,致力於推動 Service Mesh 在中國的蓬勃發展。

社區官網:https://www.servicemesher.com

ServiceMesher 社區

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