Consistent Hashing based on HTTP header for Kubernetes Service

1/19/2021

I have a K8s Service named orderProcessor with 5 pods. Based on the orderId HTTP Request header, I want to route the HTTP call to a specific pods.

For example:

requestId with orderId ABC should always go to Pod A and requestId with orderId PQR should always go to Pod B

If pod A is down and replaced with Pod C, then all request with orderId ABC should go to Pod C.

Basically, for the same header value, request should always end up in same pod.

Is this possible at service level? How to achieve this?

-- Jerald Baker
docker
kubernetes
kubernetes-ingress
kubernetes-service

1 Answer

1/27/2021

This is a community wiki answer. Feel free to expand it.

As already mentioned in the comments it is not possible to configure it at the Service level as it operates on Layer 4 which has no notion of "HTTP Headers".

The alternative would be to choose a fitting Ingress Controller for that task. For example you may choose from:

Bear in mind that different Controllers support different sets of annotations so if you mix them up than some of them will not be satisfied. You can find some of the key differences between the mentioned above here and here.

EDIT:

Including VASャ suggestion from the comments:

I recon, the combination of a regular nginx pod configured as a reverse proxy and StatefulSet pods as a backend endpoints can do the trick. Every pod in StatefullSet has own permanent name and A record in Kubernetes DNS service, so you can refer to pod name in the nginx configuration.

-- Wytrzymały Wiktor
Source: StackOverflow