Hi all!
For avoiding the problem of port exhaustion when connecting to upstream servers I have configured several IPs in my HAProxy machine. My intention is HAProxy to use those IPs as source addresses when connecting to the upstream servers (outgoing connections).
For selecting the source address for outgoing connections I have found the “source” option. The problem is that there is not an easy way of using several source addresses using this option.
The only way I have found for doing this is to duplicate the server definitions, using a different source address for each of them. For example, for 2 real servers (app1 and app2) and 3 outgoing IPs:
backend my-backend
balance leastconn
server app1_src1 app1.test.com source 192.168.1.1
server app1_src2 app1.test.com source 192.168.1.2
server app1_src3 app1.test.com source 192.168.1.3
server app2_src1 app2.test.com source 192.168.1.1
server app2_src2 app2.test.com source 192.168.1.2
server app2_src3 app2.test.com source 192.168.1.3
As you can see, I have had to create 6 “virtual” servers in my backend, one for each application-sourceIP combination.
Although this solutions works, it is not ideal, as we are not balancing the load properly between the two real servers, but on the 6 “virtual/artificial” servers, combinations of real server and source IP. Also, disabling a specific real server from the backend is more complex, because we need to disable all the possible combinations of that server.
I have to say that this can be implemented in Nginx in a very elegant way. For example:
http {
upstream backend {
server app1.test.com:80;
server app2.test.com:80;
}
server {
location / {
proxy_pass http://backend;
proxy_bind $split_ip;
}
}
split_clients "$remote_addr$remote_port" $split_ip {
33% 192.168.1.1;
33% 192.168.1.2;
* 192.168.1.3;
}
}
For implementing something like that in HAProxy we would need to use dynamic variables or converters (maps) in the “source
” option. For example:
backend my-backend
balance leastconn
source %[rand(3),map(myips.map)]
server app1 app1.test.com
server app2 app2.test.com
Unfortunately, it seems that the “source
” option does not expand a logformat string, so we cannot use variables or converters with it.
Is there a better way of doing this?
Regards.