防止K8S HPA在负载降低后删除pod

Prevent K8S HPA from deleting pod after load is reduced

我有来自 prometheus 适配器的 sidekiq 自定义指标。使用普罗米修斯的队列指标,我设置了 HPA。当 sidekiq 队列中的作业超过 1000 个作业时,HPA 会触发 10 个新 pods。然后每个 pod 将执行队列中的 100 个作业。当工作减少到 400 时。HPA 将缩小。但是当发生缩小时,hpa kills pods 说 4 pods 被杀死。 Thoes 4 pods 仍然是 运行 个职位说每个 pod 是 运行 30-50 个职位。现在,当 hpa 删除这 4 个 pods 时,它们上的作业 运行 也会终止。这些作业在 sidekiq 中被标记为失败。

所以我想要实现的是阻止 hpa 删除正在执行作业的 pods。此外,我希望 hpa 即使在负载降至最低后也不会缩小,而是在 sidekiq 指标中队列中的作业为 0 时删除 pods。

有什么办法可以实现吗?

奇怪的用法,老实说:你在浪费资源,即使你的流量处于冷却阶段,但由于你没有提供进一步的细节,这里是。

实际上,不可能实现您想要的,因为常见的行为是支持不断增长的负载来抵消您的工作量。实现这一点的唯一等待(并且不推荐)是将 horizontal-pod-autoscaler-downscale-stabilization Kubernetes 控制器管理器的 flag 更改为更高的值。

JFI,文档警告您:

Note: When tuning these parameter values, a cluster operator should be aware of the possible consequences. If the delay (cooldown) value is set too long, there could be complaints that the Horizontal Pod Autoscaler is not responsive to workload changes. However, if the delay value is set too short, the scale of the replicas set may keep thrashing as usual.

根据#Hb_1993 的讨论和所做的工作,可以使用预停止挂钩来延迟驱逐,其中延迟基于操作时间或某些逻辑来了解是否游行是否完成。

pre-stop hook 是一个生命周期方法,在 pod 被逐出之前调用,然后我们可以附加到这个事件并执行一些逻辑,比如执行 ping 检查,以确保我们的 pod 已经完成处理当前请求。

PS- 将此解决方案与少许盐一起使用,因为这可能并非在所有情况下都有效或会产生意想不到的结果。

To do this, we introduce asleep in the preStop hook that delays the shutdown sequence.

可以在本文中找到更多详细信息。

https://blog.gruntwork.io/delaying-shutdown-to-wait-for-pod-deletion-propagation-445f779a8304