繁体   English   中英

Kubernetes HPA 使用来自另一个部署的指标

[英]Kubernetes HPA using metrics from another deployment

我目前正在尝试使用 prometheus 和 prometheus 适配器运行自动缩放演示,我想知道是否有一种方法可以根据 prometheus 从另一个部署中抓取的指标来自动缩放我的一个部署。

我现在拥有的是 2 个不同的部署,kafka-consumer-application(我想要扩展)和 kafka-exporter(它公开了我将用于扩展的 kafka 指标)。 我知道,如果我将它们都作为容器在同一个部署中,则自动缩放可以工作,但问题是 kafka-exporter 也会自动缩放并且它并不理想,所以我想将它们分开。 我尝试使用以下 HPA,但无法正常工作:

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
  name: consumer-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: kafka-consumer-application
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: object
    object:
      target: kafka-exporter
      metricName: "kafka_consumergroup_lag"
      targetValue: 5

我不确定我是否做错了什么,或者这不是我能做的,所以任何建议都值得赞赏。

谢谢!

注意:我使用此配置运行适配器:

rules:
  default: false
  resource: {}
  custom:
    - seriesQuery: 'kafka_consumergroup_lag'
      resources:
        overrides:
          kubernetes_namespace: {resource: "namespace"}
          kubernetes_pod_name: {resource: "pod"}
      name:
       matches: "kafka_consumergroup_lag"
       as: "kafka_consumergroup_lag"
      metricsQuery: 'avg_over_time(kafka_consumergroup_lag{topic="my-topic",consumergroup="we-consume"}[1m])'
``

kubernetes 文档中,您可以阅读:

Autoscaling on metrics not related to Kubernetes objects Applications running on Kubernetes may need to autoscale based on metrics that don't have an obvious relationship to any object in the Kubernetes cluster, such as metrics describing a hosted service with no direct correlation to Kubernetes namespaces. 在 Kubernetes 1.10 及更高版本中,您可以使用外部指标解决此用例

因此,使用外部指标,您的 HPA yaml 可能如下所示:

kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta2
metadata:
  name: consumer-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: kafka-consumer-application
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: External
    external:
      metric:
        name: kafka_consumergroup_lag
        #selector:
        #  matchLabels:
        #    topic: "my-topic"
      target:
        type: AverageValue
        averageValue: 5

如果您有多个 kafka-exporter,则可以使用selector对其进行过滤( 来源):

选择器是给定指标的标准 kubernetes label 选择器的字符串编码形式 设置后,它作为附加参数传递给指标服务器以获取更具体的指标范围。 未设置时,仅 metricName 将用于收集指标

也看看这个 Stack question

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM