简体   繁体   English

具有Pometheus的自定义指标的水平Pod Autoscaler和百分位数的CPU使用率

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

So I am trying to figure out how can I configure an Horizontal Pod Autoscaler from a custom metric reading from Prometheus that returns CPU usage with percentile 0.95 所以我试图找出如何从Prometheus的自定义指标读取中配置Horizo​​ntal Pod Autoscaler,该指标返回CPU使用率为0.95的百分比

I have everything set up to use custom metrics with prometheus-adapter, but I don't understand how to create the rule in Prometheus. 我已将一切设置为使用带有Prometheus-adapter的自定义指标,但我不了解如何在Prometheus中创建规则。 For example, if I go to Grafana to check some of the Graphs that comes by default I see this metric: 例如,如果我去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)

But how can I modify that to be percentile 95? 但是如何将其修改为百分位数95? I tried with histogram_quantile function but it says no datapoints found: 我尝试了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))

But even if that works, will the pod name and namespace be filled by prometheus-adapter or prometheus when using custom metrics? 但是,即使可行,使用自定义指标时,窗格名称和名称空间是否会由prometheus-adapter或prometheus填充?

And every example I find using custom metrics are not related with CPU. 我发现使用自定义指标的每个示例都与CPU无关。 So... other question I have is how people is using autoscaling metrics in production? 所以...我还有另一个问题是人们如何在生产中使用自动缩放指标? I'm used to scale based on percentiles but I don't understand how is this managed in Kubernetes. 我习惯于根据百分位数进行缩放,但我不了解如何在Kubernetes中进行管理。

If I understand you correctly you don't have to use custom metrics in order to horizontally autoscale your pods. 如果我对您的理解正确,则不必使用自定义指标即可水平自动缩放广告连播。 By default, you can automatically scale the number of Kubernetes pods based on the observed CPU utilization. 默认情况下,您可以根据观察到的CPU使用率自动缩放Kubernetes容器的数量。 Here is the official documentation with necessary details. 这是带有必要详细信息的官方文档

The Horizontal Pod Autoscaler automatically scales the number of pods in a replication controller, deployment or replica set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics). Horizo​​ntal Pod Autoscaler基于观察到的CPU使用率(或借助自定义指标支持,基于其他一些应用程序提供的指标)自动缩放复制控制器,部署或副本集中的Pod数量。

The Horizontal Pod Autoscaler is implemented as a Kubernetes API resource and a controller. Horizo​​ntal Pod Autoscaler被实现为Kubernetes API资源和控制器。 The resource determines the behavior of the controller. 该资源确定控制器的行为。 The controller periodically adjusts the number of replicas in a replication controller or deployment to match the observed average CPU utilization to the target specified by user. 控制器会定期调整复制控制器或部署中副本的数量,以使观察到的平均CPU利用率与用户指定的目标相匹配。

And here you can find the walkthrough of how to set it up. 在这里,您可以找到如何设置的演练

Also, here is the kubectl autoscale command documentation. 另外, kubectl autoscale命令文档。

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

Auto scale a replication controller "foo", with the number of pods between 1 and 5, target CPU utilization at 80% 自动缩放复制控制器“ foo”,其窗格数在1到5之间,目标CPU利用率为80%

I believe that it is the easiest way so no need to complicate it with some custom metrics. 我认为这是最简单的方法,因此无需将其与某些自定义指标复杂化。

Please let me know if that helped. 请让我知道是否有帮助。

If you want to add HPA based on custom metrics you can use Prometheus adapter. 如果要基于自定义指标添加HPA,则可以使用Prometheus适配器。

Prometheus adapter helps you in exposing custom metrics to HPA. Prometheus适配器可帮助您向HPA公开自定义指标。

Helm Chart - https://github.com/helm/charts/tree/master/stable/prometheus-adapter 舵图-https: //github.com/helm/charts/tree/master/stable/prometheus-adapter

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

Note - You have to enable 6443 port from public to cluster, because prometheus doesn't provide override option. 注意-您必须启用从公共端口到群集端口的6443端口,因为prometheus不提供覆盖选项。

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

Make sure that Prometheus is getting custom metrics data Install Prometheus adapter on the same kubernetes cluster where you want apply hpa 确保Prometheus正在获取自定义指标数据将Prometheus适配器安装在要应用hpa的同一kubernetes集群上

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

Pass the following config file to helm - 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))"

Above config will expose 上面的配置将暴露

cpu metrics as cpu_95 to HPA. cpu指标作为HPA的cpu_95。

To Verify, if data is exposed properly run following command - 要验证,是否正确公开了数据,请运行以下命令-

Fetch the data curl raw query command - kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1/namespaces/namespace_name/pods/\\*/cpu_95 | jq . 获取数据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 config - 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.

相关问题 OpenShift中水平Pod自动缩放器的自定义指标 - custom metrics for horizontal pod autoscaler in OpenShift 使用 Prometheus Adapter 的 Horizo​​ntal Pod Autoscaler (HPA) 自定义指标(如何定义单位?) - Horizontal Pod Autoscaler (HPA) custom metrics with Prometheus Adapter (How are units defined?) 水平Pod Autoscaler无法读取CPU使用率 - Horizontal Pod Autoscaler can't read CPU usage Prometheus是否如Kubernetes仪表板所示公开了Horizo​​ntal Pod Autoscaler的“当前CPU使用率”? - Does Prometheus expose the Horizontal Pod Autoscaler's “Current CPU Utilization” as shown in the Kubernetes dashboard? HPA(水平 pod 自动缩放器)`currentCPUUtilizationPercentage` 的 Kube.netes Prometheus 指标? - Kubernetes Prometheus metric for HPA (horizontal pod autoscaler) `currentCPUUtilizationPercentage`? 配置 prometheus 以从 dockerized nodejs pod 收集自定义指标 - Configure prometheus to collect custom metrics from dockerized nodejs pod Kubernetes Horizo​​ntal Pod Autoscaler如何计算CPU百分比? - How Kubernetes Horizontal Pod Autoscaler calculates CPU percentage? 使用Prometheus监控自定义kubernetes pod指标 - Monitor custom kubernetes pod metrics using Prometheus 水平 pod 自动缩放不起作用:`无法获取资源 cpu 的指标:没有从 heapster 返回指标` - Horizontal pod autoscaling not working: `unable to get metrics for resource cpu: no metrics returned from heapster` Cluster Autoscaler 和 Horizontal Pod Autoscaler 一起工作 - Cluster Autoscaler and Horizontal Pod Autoscaler working together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM