简体   繁体   English

根据下游API结果自动缩放Kubernetes Pod

[英]Auto scale kubernetes pods based on downstream api results

I have seen HPA can be scaled based on CPU usage. 我已经看到HPA可以根据CPU使用率进行扩展。 That is super cool. 那太酷了。

However, the scenario I have is: the stateful app (container in pod) is one to one mapping based on the downstream API results. 但是,我遇到的情况是:有状态的应用程序(容器中的容器)是基于下游API结果的一对一映射。 For example, the downstream api results return maximum and expected capacity like {response: 10} . 例如,下游api结果返回最大容量和预期容量,例如{response: 10} I would like to see replicaSet or statefulSet or other kubernetes controller can obtain this value and auto scale the pods to 10. Unfortunately, the pod replicas is hardcoded in the yaml file. 我希望看到copysetSet或statefulSet或其他kubernetes控制器可以获取此值并将Pod自动缩放到10。不幸的是,pod副本硬编码在yaml文件中。

If I am doing it manually, I think I can do it via running start a scheduler. 如果我手动执行此操作,我认为可以通过运行启动调度程序来执行。 The job of the scheduler is to watch the api and run the kubectl scale command based on the downstream api results. 调度程序的工作是监视api并根据下游api结果运行kubectl scale命令。 This can be error prone and there is another system I need to maintain. 这可能容易出错,并且我需要维护另一个系统。 I guess this logic should belong to a kubernetes controller ? 我猜这个逻辑应该属于kubernetes控制器吗?

May I ask has someone done this stuff before and what is the way to configure it ? 请问有人做过这些东西吗?如何配置它?

Thanks in advance. 提前致谢。

Unfortunately, it is not possible to use an HPA in that mode, but your conception about how to scale is right. 不幸的是,无法在该模式下使用HPA,但是您对如何扩展的概念是正确的。

HPA is designed to analyze metrics and decide how many pods need to be spawned based on those metrics. HPA旨在分析指标并根据这些指标决定需要繁殖多少个吊舱。 It is using scaling rules and can only spawn pods one by one based on the result of its decision. 它使用缩放规则,并且只能根据其决定结果一一生成荚。

Moreover, it using standard Kubernetes API for scale pods. 而且,它使用标准的Kubernetes API来缩放秤舱。

Because a logic of HPA is already in your application, you can use the same API to scale your pods. 因为HPA逻辑已在您的应用程序中,所以您可以使用相同的API来扩展Pod。 Btw, kubectl scale is using the same way to interact with a cluster. 顺便说一句, kubectl scale正在使用相同的方式与集群进行交互。

So, you can use ie Cronjob, with a small application which will call API of your application every 5 minutes and call kubectl scale with proper name of deployment to scale your app. 因此,您可以将Cronjob与小型应用程序一起使用,该应用程序每5分钟调用一次应用程序的API,并使用适当的部署名称调用kubectl scale来扩展您的应用程序。

But, please keep in mind, you need to somehow control the frequency of up- and downscaling of pods, it will make your application more stable. 但是,请记住,您需要以某种方式控制Pod上下缩放的频率,这将使您的应用程序更稳定。 That's why I think that scaling not more often than once per 5 minutes is OK, but trying to do it every minute generally is not the best idea. 这就是为什么我认为不超过每5分钟一次的缩放比例是可以的,但是通常每分钟尝试一次缩放并不是最佳方法。

And of course, you can create a daemon and run it using Deployment, but I think Cronjob solution is more easy and faster to implement. 当然,您可以创建一个守护程序并使用Deployment来运行它,但是我认为Cronjob解决方案更容易实现。

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

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