具有基于 URL 亲和力的服务代理

Service proxy with affinity based on URL

我正在寻找具有基于 URL 亲和力的服务代理(或负载均衡器)。

这是为了在集群内部的 Kubernetes 中使用:我正在寻找一个 "internal" 负载平衡器,我不需要在外部公开服务。

默认情况下,Kubernetes 中的服务使用 "round robin" 算法。

我想要一些基于 HTTP URL 部分的亲和力:第一个请求将转到随机 pod,使用相同 URL 的后续请求将(最好)转到到同一个吊舱。

我已经阅读了一些关于基于 sourceIP 的亲和力的文档,这是否存在基于 URLs?

我已经快速阅读了有关 Envoy 的内容,也许使用 "Ring hash" 负载平衡算法可以,但我不知道是否可以基于 URL 进行散列。

也许可以使用 kube-proxy (https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-ipvs) 的 "ipvs" 代理模式,但我只看到 "destination hashing" 和 "source hashing" 作为负载平衡算法,我也不知道怎么配置。

正如您已经提到的,IPVS 代理算法定义了源和目标 IP 地址,以便为负载平衡生成唯一的哈希键。但是,它在 L4 传输层运行,为 TCP 或 UDP 服务拦截网络流量。因此,可能很难与 HTTP 请求交互并根据 URL 路径做出路由决策。

Envoy proxy represents consistent hashing via HTTP header values, specified inside HTTP router filter along with Ring hash 负载均衡策略。因此,您可以在Hash policy中指定合适的header名称,用于获取负载均衡的hash key。

hash_policy:
  header:
    header_name: "x-url"

或者,您可以考虑使用 Istio as an intermediate proxy which uses extended version of Envoy. Kubernetes services are involved into the service mesh by deploying a special sidecar proxy throughout your environment that intercepts all network communication between microservices. Istio can be also used for Hash consistent load balancing with session affinity based on HTTP headers via DestinationRule 资源。

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: example
spec:
  host: my-service.default.svc.cluster.local
  trafficPolicy:
    loadBalancer:
      consistentHash:
        httpHeaderName: x-url