简体   繁体   English

Kubernetes:将 HPA 与来自其他 pod 的指标一起使用

[英]Kubernetes: using HPA with metrics from other pods

I have:我有:

  1. deployments of services A and B in k8s在 k8s 中部署服务 A 和 B
  2. Prometheus stack普罗米修斯堆栈

I wanna scale service A when metric m1 of service B is changed.我想在服务 B 的度量 m1 更改时扩展服务 A。 Solutions which I found and not suitable more or less:我发现但或多或少不适合的解决方案:

  1. I can define HPA for service A with the following part of spec:我可以使用规范的以下部分为服务 A 定义 HPA:
  - type: Object
      object:
        metric:
          name: m1
        describedObject:
          apiVersion: api/v1
          kind: Pod
          name: certain-pod-of-service-B
        current:
          value: 10k

Technically, it will work.从技术上讲,它会起作用。 But it's not suitable for dynamic nature of k8s.但它不适合 k8s 的动态特性。 Also I can't use pods metric (metrics: - type: Pods pods:) in HPA cause it will request m1 metric for pods of service A (which obviously doesn't have this)我也不能在 HPA 中使用 pods metric(metrics: - type: Pods pods:) 因为它会为服务 A 的 pod 请求 m1 metric(显然没有这个)

  1. Define custom metric in prometheus-adapter which query m1 metric from pods of service B. It's more suitable, but looks like workaround cause I already have a metric m1在 prometheus-adapter 中定义自定义指标,从服务 B 的 pod 查询 m1 指标。它更合适,但看起来像解决方法,因为我已经有一个指标 m1

  2. The same for external metrics外部指标也是如此

I feel that I miss something cause it doesn't seem like a non realistic case:) So, advise me please how to scale one service by metric of another in k8s?我觉得我错过了一些东西,因为它看起来不像是一个不现实的案例:) 所以,请告诉我如何在 k8s 中按另一个服务的度量来扩展一个服务?

I decided to provide a Community Wiki answer that may help other people facing a similar issue.我决定提供一个社区 Wiki 答案,以帮助面临类似问题的其他人。

The Horizontal Pod Autoscaler is a Kubernetes feature that allows to scale applications based on one or more monitored metrics. Horizontal Pod Autoscaler是一项 Kubernetes 功能,允许基于一个或多个监控指标扩展应用程序。
As we can find in the Horizontal Pod Autoscaler documentation :我们可以在Horizontal Pod Autoscaler 文档中找到:

The Horizontal Pod Autoscaler automatically scales the number of Pods in a replication controller, deployment, replica set or stateful set based on observed CPU utilization (or, with custom metrics support, on some other application-provided metrics). Horizontal Pod Autoscaler 会根据观察到的 CPU 利用率(或者,在自定义指标支持下,根据一些其他应用程序提供的指标)自动扩展复制 controller、部署、副本集或有状态集中的 Pod 数量。

There are three groups of metrics that we can use with the Horizontal Pod Autoscaler:我们可以在 Horizontal Pod Autoscaler 中使用三组指标

  • resource metrics : predefined resource usage metrics (CPU and memory) of pods and nodes. 资源指标:Pod 和节点的预定义资源使用指标(CPU 和内存)。
  • custom metrics : custom metrics associated with a Kubernetes object. 自定义指标:与 Kubernetes object 关联的自定义指标。
  • external metrics : custom metrics not associated with a Kubernetes object. 外部指标:与 Kubernetes object 无关的自定义指标。

Any HPA target can be scaled based on the resource usage of the pods (or containers ) in the scaling target.任何 HPA 目标都可以根据扩展目标中pod (或容器)的资源使用情况进行扩展。 The CPU utilization metric is a resource metric , you can specify other resource metrics besides CPU (eg memory). CPU 利用率指标是一个resource metric ,您可以指定除 CPU 之外的其他资源指标(例如内存)。 This seems to be the easiest and most basic method of scaling, but we can use more specific metrics by using custom metrics or external metrics .这似乎是最简单、最基本的扩展方法,但我们可以通过使用custom metricsexternal metrics

There is one major difference between custom metrics and external metrics (see: Custom and external metrics for autoscaling workloads ): custom metricsexternal metrics之间有一个主要区别(请参阅: 用于自动缩放工作负载的自定义和外部指标):

Custom metrics and external metrics differ from each other:自定义指标和外部指标彼此不同:

A custom metric is reported from your application running in Kubernetes.在 Kubernetes 中运行的应用程序会报告自定义指标。 An external metric is reported from an application or service not running on your cluster, but whose performance impacts your Kubernetes application.来自未在集群上运行的应用程序或服务报告的外部指标,但其性能会影响您的 Kubernetes 应用程序。

All in all, in my opinion it is okay to use custom metrics in the case above, I did not find any other suitable way to accomplish this task.总而言之,在我看来,在上述情况下使用custom metrics是可以的,我没有找到任何其他合适的方法来完成这项任务。

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

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