Kubernetes 中的自定义负载均衡

Custom load balancing in Kubernetes

我们正在开发一个模拟软件,它使用 kubernetes 在多个 pods 之间部署和扩展。 当用户发出模拟请求时,将选择一个开始执行作业并被视为忙碌的 pod。 当另一个用户发出模拟请求时,它应该被路由到下一个空闲的 pod。 目前,经常选择繁忙的 pod(即使有空闲的 pod),因为 kubernetes 不知道 pods 是 busy/free。

是否可以通过始终选择空闲 pod 的方式来平衡请求? (假设 pod 中的每个应用程序实例都公开一个 HTTP 端点,告诉它当前的 busy/free 状态)

我想你可以利用 readiness probes:

Sometimes, applications are temporarily unable to serve traffic. For example, an application might need to load large data or configuration files during startup, or depend on external services after startup. In such cases, you don't want to kill the application, but you don't want to send it requests either. Kubernetes provides readiness probes to detect and mitigate these situations. A pod with containers reporting that they are not ready does not receive traffic through Kubernetes Services.

您可以使应用程序响应非 200 return 代码的探测请求。它将被注意到,并且在就绪探测再次成功之前不会传递任何新请求。但也有缺点:

  • 当所有 pods 都忙时,您将收到 502 错误;
  • 用户将无法向他们的 pod 提交后续请求(因为 pod 会很忙);
  • 更改就绪状态需要一些时间,因此如果您在短时间间隔(探测间隔)内收到大量请求(超过 pods 的数量),一些 pods 可能需要超过一个请求。