Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAProxy dynamic server addresses

We have Similar setup to this diagram

enter image description here

Where request arrives to HAProxy, it get's roundrobin balanced to any servers, backend server checks its cache and if resource is not on that server it issues redirect with header set to the correct server IP.

Second time request arrives to HAProxy, it detects that the header with backend server is there, but how can I take that IP and direct request directly to it?

For example, second time request arrives to haproxy it has header X-BACKEND-IP=10.0.0.5

So instead haproxy trying to load balance that request, I want it to read the header, take that IP and go directly to that backend.

Is that possible? If not, would it be possible with nginx ?

like image 545
Tomas Avatar asked Oct 18 '22 15:10

Tomas


1 Answers

Assuming you're happy with trusting the IP in the header of the second request, then yes, you can do it with use-server:

backend bk_foo
  [...]
  server srv_0a_00_01_05 10.0.1.5:80 weight 100
  server srv_0a_00_02_05 10.0.2.5:80 weight 100
  use-server %[req.hdr(x-backend-ip),lower,map_str(/etc/haproxy/hdr2srv.map,srv_any)] if { req.hdr(x-backend-ip),lower,map_str(/etc/haproxy/hdr2srv.map) -m found }

Contents of /etc/haproxy/hdr2srv.map:

#ip srv_name
# hex of IP used for names in this example
10.0.1.5  srv_0a_00_01_05
10.0.2.5  srv_0a_00_02_05

If you need to down one of the servers, you should dynamically update the map to remove it, so that the requests with the header set get redirected again.

If you have multiple backends, you can do similar with use_backend.

like image 185
robbat2 Avatar answered Nov 15 '22 09:11

robbat2