0

Suppose that you have some API requests like these:

  • example.com/api/projects/123/something
  • example.com/api/projects/456/something

Is it possible to route the requests of a project always to the same pod?

This would allow us to use local disk storage for caching and improve performance. If a request goes to a "wrong" pod it's not a problem, but we want that most requests of the same project go to the same pod (affinity between project param in the URL and pods).

Ideally you just need to compute a hash function on the project ID that generates a number between [1, N] where N is the number of pods. However I don't know how to implement this routing in K8s.

collimarco
  • 34,231
  • 36
  • 108
  • 142
  • I found that you posted this same question on reddit, and I saw that you found a useful response there related to consistent hashing. Did the answer solve your issue? – Ismael Clemente Aguirre Sep 06 '22 at 15:43

1 Answers1

0

Instead of forcing the requests to go to same pods, you can mount a PersistentVolume with the accessPolicy of type of ReadWriteMany (all nodes can read/write the same PV) and mount it in all the Pods. This way, each Pod will have access to the same cache.

If the above solution doesn't work then you will need to explore IPVS which allows you to specify the load-balancing algorithm. By default, Kubernetes uses IP Tables to route the traffic.

Support of IPVS depends on the CNI plugin that you're currently using and the current mode of kube-proxy service.

Check out these links regarding IPVS:

  1. Calico
  2. Enable IPVS in K8s
Taimoor Mirza
  • 1,093
  • 7
  • 19
  • Thanks, but the problem with a shared PersistentVolume is that is not distributed and can become a bottleneck. I could also use Redis in that case... But the problem here is that I want to use a distributed cache (cache distributed locally on every pod or node). – collimarco Sep 03 '22 at 14:31