簡體   English   中英

具有Pometheus的自定義指標的水平Pod Autoscaler和百分位數的CPU使用率

[英]Horizontal Pod Autoscaler with custom metrics from Prometheus with percentiles for CPU usage

所以我試圖找出如何從Prometheus的自定義指標讀取中配置Horizo​​ntal Pod Autoscaler,該指標返回CPU使用率為0.95的百分比

我已將一切設置為使用帶有Prometheus-adapter的自定義指標,但我不了解如何在Prometheus中創建規則。 例如,如果我去Grafana檢查默認情況下出現的某些圖表,則會看到以下指標:

sum(namespace_pod_name_container_name:container_cpu_usage_seconds_total:sum_rate{namespace="api", pod_name="api-xxxxx9b-bdxx", container_name!="POD", cluster=""}) by (container_name)

但是如何將其修改為百分位數95? 我嘗試了histogram_quantile函數,但沒有找到數據點:

histogram_quantile(0.95, sum(namespace_pod_name_container_name:container_cpu_usage_seconds_total:sum_rate{namespace="api", pod_name="api-xxxxx9b-bdxx", container_name!="POD", cluster=""}) by (container_name))

但是,即使可行,使用自定義指標時,窗格名稱和名稱空間是否會由prometheus-adapter或prometheus填充?

我發現使用自定義指標的每個示例都與CPU無關。 所以...我還有另一個問題是人們如何在生產中使用自動縮放指標? 我習慣於根據百分位數進行縮放,但我不了解如何在Kubernetes中進行管理。

如果我對您的理解正確,則不必使用自定義指標即可水平自動縮放廣告連播。 默認情況下,您可以根據觀察到的CPU使用率自動縮放Kubernetes容器的數量。 這是帶有必要詳細信息的官方文檔

Horizo​​ntal Pod Autoscaler基於觀察到的CPU使用率(或借助自定義指標支持,基於其他一些應用程序提供的指標)自動縮放復制控制器,部署或副本集中的Pod數量。

Horizo​​ntal Pod Autoscaler被實現為Kubernetes API資源和控制器。 該資源確定控制器的行為。 控制器會定期調整復制控制器或部署中副本的數量,以使觀察到的平均CPU利用率與用戶指定的目標相匹配。

在這里,您可以找到如何設置的演練

另外, kubectl autoscale命令文檔。

例如: kubectl autoscale rc foo --max=5 --cpu-percent=80

自動縮放復制控制器“ foo”,其窗格數在1到5之間,目標CPU利用率為80%

我認為這是最簡單的方法,因此無需將其與某些自定義指標復雜化。

請讓我知道是否有幫助。

如果要基於自定義指標添加HPA,則可以使用Prometheus適配器。

Prometheus適配器可幫助您向HPA公開自定義指標。

舵圖-https: //github.com/helm/charts/tree/master/stable/prometheus-adapter

Prometheus適配器-https: //github.com/DirectXMan12/k8s-prometheus-adapter

注意-您必須啟用從公共端口到群集端口的6443端口,因為prometheus不提供覆蓋選項。

https://github.com/helm/charts/blob/master/stable/prometheus-adapter/templates/custom-metrics-apiserver-deployment.yaml#L34

確保Prometheus正在獲取自定義指標數據將Prometheus適配器安裝在要應用hpa的同一kubernetes集群上

helm install --name my-release stable/prometheus-adapter -f values.yaml

將以下配置文件傳遞給helm-values.yaml

prometheus-adapter:
  enabled: true
  prometheus:
    url: http://prometheus.namespace.svc.cluster.local
  rules:
    default: true
    custom:
    - seriesQuery: '{__name__="cpu",namespace!="",pod!="",service="svc_name"}'
      seriesFilters: []
      resources:
        overrides:
          namespace: {resource: "namespace"}
          pod: {resource: "pod"}
      name:
        matches: "cpu"
        as: "cpu_95"
      metricsQuery: "histogram_quantile(0.95, sum(irate(<<.Series>>{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>,le))"

上面的配置將暴露

cpu指標作為HPA的cpu_95。

要驗證,是否正確公開了數據,請運行以下命令-

獲取數據curl原始查詢命令kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/namespaces/namespace_name/pods/\\*/cpu_95 | jq . kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/namespaces/namespace_name/pods/\\*/cpu_95 | jq .

HPA配置-

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: test-cpu-manual
  labels:
    app: app_name
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta2
    kind: Deployment
    name: app_name
  minReplicas: 1
  maxReplicas: 15
  metrics:
  - type: Pods
    pods:
      metricName: cpu_95
      targetAverageValue: 75

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM