Service proxy with affinity based on URL

3/18/2019

I am looking for a service proxy (or load-balancer) with URL-based affinity.

This is for using in Kubernetes, inside the cluster: I am looking for an "internal" load balancer, I don't need to expose the service outside.

By default, the Service in Kubernetes is using a "round robin" algorithm.

I would like some affinity based on a part of the HTTP URL: a 1st request would go to a random pod, and subsequent requests that use the same URL would (preferably) go to the same pod.

I have read some doc about affinity based on sourceIP, does this exist based on URLs ?

I have quickly read about Envoy, maybe using the "Ring hash" load-balancing algorithm would do, but I don't know if it's possible to hash based on the URL.

Maybe using the "ipvs" proxy-mode of kube-proxy (https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-ipvs) would do, but I see only "destination hashing" and "source hashing" as load balancing algorithms, and I don't know how to configure it either.

-- TagadaPoe
envoyproxy
haproxy
kubernetes

1 Answer

3/25/2019

As you have already mentioned, IPVS proxy algorithm defines source and destination IP addresses in order to generate a unique hash key for load balancing. However, it operates in L4 transport layer of intercepting network traffic for TCP or UDP services. Therefore it might be difficult to interact with HTTP request and make a route decision based on URL path.

Envoy proxy represents consistent hashing via HTTP header values, specified inside HTTP router filter along with Ring hash load balancing policy. Therefore, you can specify the appropriate header name in Hash policy that can be used to obtain the hash key for load balancing.

hash_policy:
  header:
    header_name: "x-url"

Alternatively, you can consider to use 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 resource.

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