Reputation:
I have configured a load balancer using HAProxy on frontend and on the backend I have 4 server serving the request using Apache web server.
#HAProxy for web servers
frontend web-frontend
bind IPADDRESS_LB:80
mode http
default_backend web-backend
backend web-backend
balance roundrobin
server web-server1 IPADDRESS1:80 check
server web-server2 IPADDRESS2:80 check
server web-server3 IPADDRESS3:80 check
server web-server4 IPADDRESS4:80 check
My requests are served by either of the machines in a round robin mechanism. But now I want to implement my own algorithm to send the request based on the request parameters, eg IP.
Like Implementing my own hash function based on the result of which I can route my request to either of the backend servers.
How can I do that?
Upvotes: 2
Views: 2320
Reputation: 374
You can parametrize what to use in the hash (source with examples : HaProxy's blog)
In your case, simply use balance source
to hash based on the source (IP) :
#HAProxy for web servers
frontend web-frontend
bind IPADDRESS_LB:80
mode http
default_backend web-backend
backend web-backend
balance source
hash-type consistent
server web-server1 IPADDRESS1:80 check
server web-server2 IPADDRESS2:80 check
server web-server3 IPADDRESS3:80 check
server web-server4 IPADDRESS4:80 check
(hash-type consistent
makes sure that distinct IP hashes give the same result)
Or based on the URL parameter named ip
:
#HAProxy for web servers
frontend web-frontend
bind IPADDRESS_LB:80
mode http
default_backend web-backend
backend web-backend
balance url_param ip
hash-type consistent
server web-server1 IPADDRESS1:80 check
server web-server2 IPADDRESS2:80 check
server web-server3 IPADDRESS3:80 check
server web-server4 IPADDRESS4:80 check
To base the hash on multiple parameters, you can :
Example from HaProxy's blog :
backend bk_static
http-request set-header X-LB %[req.hdr(Host),lower]%[req.uri,lower]
balance hdr(X-LB)
hash-type consistent
Upvotes: 1