如何向HPA提供外部指标? [英] How to supply external metrics into HPA?

查看:158
本文介绍了如何向HPA提供外部指标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题设置..假设我有两个Pod,A和B.我希望能够根据来自任意来源的任意数量动态缩放PodA.假设Pod B是这样的来源:例如,它可以具有一个HTTP服务器,该服务器的端点在请求时以Pod A的所需副本数进行响应.又或者它是ES服务器还是SQL DB(无关紧要).

Problem setting. Suppose I have 2 pods, A and B. I want to be able to dynamically scale pod A based on some arbitrary number from some arbitrary source. Suppose that pod B is such a source: for example, it can have an HTTP server with an endpoint which responds with the number of desired replicas of pod A at the moment of request. Or maybe it is an ES server or a SQL DB (does not matter).

问题.除HPA外,我还需要定义哪些kubernetes对象? HPA应该知道应该使用哪种配置来查找B以获取当前指标? B的API应该是什么样子(或有任何约束?)?

Question. What kubernetes objects do I need to define to achieve this (apart from HPA)? What configuration should HPA have to know that it needs to look up B for current metric? How should API of B look like (or is there any constraints?)?

我进行的研究.不幸的是,除了声明存在这种可能性之外,官方文档并没有对此多说.还有两个存储库,一个,其中有一些我遇到麻烦的样板代码构建和另一没有任何使用说明(尽管据称确实满足了基于HTTP的外部指标"要求.

Research I have made. Unfortunately, the official documentation does not say much about it, apart from declaring that there is such a possibility. There are also two repositories, one with some go boilerplate code that I have trouble building and another one that has no usage instructions whatsoever (though allegedly does fulfil the "external metrics over HTTP" requirement).

通过查看这些存储库中的.yaml配置,我得出的结论是,除了DeploymentService之外,还需要定义一个APIService对象,该对象将外部或自定义指标注册到kubernetes API并将其与常规服务(您将在其上放置Pod)以及少数ClusterRoleClusterRoleBinding对象进行链接.但是对此没有任何解释.同样,我什至无法像其他对象一样在我的本地集群(1.15版)中列出带有kubectl的现有APIServices.

By having a look at the .yaml configs in those repositories, I have reached a conclusion that apart from Deployment and Service one needs to define an APIService object that registers the external or custom metric in the kubernetes API and links it with a normal service (where you would have your pod) and a handful of ClusterRole and ClusterRoleBinding objects. But there is no explanation about it. Also I could not even list existing APIServices with kubectl in my local cluster (of 1.15 version) like other objects.

推荐答案

最简单的方法是将指标输入Prometheus(这是一个普遍解决的问题),然后设置基于Prometheus的HPA(也是一个普遍解决的问题) ).

The easiest way will be to feed metrics into Prometheus (which is a commonly solved problem), and then setup a Prometheus-based HPA (also a commonly solved problem).

1.向Prometheus馈送自己的指标

  • Prometheus-Operator 开始获得集群本身受到监视,并可以访问ServiceMonitor对象. ServiceMonitor是指向群集中服务的指针.它们使您的Pod的/metrics端点被Prometheus服务器发现并抓取.
  • 编写一个pod,该pod从您的第三方API中读取指标,并将其显示在自己的/metrics端点中.这将是您的API和Prometheus格式之间的适配器.当然有客户: https://github.com/prometheus/client_python#exporting
  • 写一个类型为ClusterIPService来代表您的广告连播.
  • 写一个指向服务的ServiceMonitor.
  • 通过Prometheus仪表板查询您的自定义指标,以确保完成此阶段.
  • Start with Prometheus-Operator to get the cluster itself monitored, and get access to ServiceMonitor objects. ServiceMonitors are pointers to services in the cluster. They let your pod's /metrics endpoint be discovered and scraped by a prometheus server.
  • Write a pod that reads metrics from your 3rd party API and shows them in own /metrics endpoint. This will be the adapter between your API and Prometheus format. There are clients of course: https://github.com/prometheus/client_python#exporting
  • Write a Service of type ClusterIP that represents your pod.
  • Write a ServiceMonitor that points to a service.
  • Query your custom metrics thru Prometheus dashboard to ensure this stage is done.

2.设置基于Prometheus的HPA

  • Setup Prometheus-Adapter and follow the HPA walkthrough.
  • Or follow the guide https://github.com/stefanprodan/k8s-prom-hpa

要获得HPA,这似乎是一大堆工作.但是,在此,仅适配器盒是自定义部件.在大多数集群中,其他所有内容都是标准的堆栈设置,无论如何,您还将获得许多其他用例.

This looks like a huge pile of work to get the HPA. However, only the adapter pod is a custom part here. Everything else is a standard stack setup in most of the clusters, and you will get many other use cases for it anyways.

这篇关于如何向HPA提供外部指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆