HAProxy - URL Based routing with load balancing
Asked Answered
A

2

38

I am new to HAProxy and I have a question about HAProxy configuration which helps me make a key decision in taking the right approach. This will greatly help me deciding the architecture.

I have 3 apps. Let's say app1, app2, app3.

Each app is differentiated by the urls as follows:

www.example.com/app1/123 -> app1
www.example.com/app2/123 -> app2
www.example.com/app3/123 -> app3

I am planning to have 2 instances of each app in 2 different regions:

Region 1 - app1, app2, app3
Region 2 - app1, app2, app3

I see 2 methods to configure this but I am not sure which is the best practice here:

  • Method 1: Have HAProxy1 to first differentiate the requests using the url patterns. Requests from HAProxy1 will be routed to another HAProxy server set up individual apps (3 HAProxy servers in this case) for load balancing.

  • Method 2: Have one great HAProxy server which does the both as stated in method 1. That is, have configuration to segregate the requests depending on the url and then pass each request through individual filter like things set up for each app for load balancing.

I am not sure if Method 2 is supported in haproxy. Any ideas or suggested is greatly appreciated. Please put some light.

Aldehyde answered 16/12, 2013 at 8:42 Comment(0)
C
62

You can segregate requests based on URL and load balance with a single HAProxy server. Your configuration will have something like this:

frontend http
acl app1 path_end -i /app1/123 #matches path ending with "/app/123"
acl app2 path_end -i /app2/123 
acl app3 path_end -i /app3/123 


use_backend srvs_app1    if app1
use_backend srvs_app2    if app2
use_backend srvs_app3    if app3

backend srvs_app1 #backend that lists your servers. Use a balancing algorithm as per your need.
   balance roundrobin 
   server host1 REGION1_HOST_FOR_APP1:PORT 
   server host2 REGION2_HOST_FOR_APP1:PORT

backend srvs_app2
   balance roundrobin
   server host1 REGION1_HOST_FOR_APP2:PORT 
   server host2 REGION2_HOST_FOR_APP2:PORT

backend srvs_app3
   balance roundrobin
   server host1 REGION1_HOST_FOR_APP3:PORT 
   server host2 REGION2_HOST_FOR_APP3:PORT

More information can be found on the homepage.

Cromer answered 17/12, 2013 at 17:16 Comment(3)
Don't forget to balance your loadbalancers too. If proxy fails your apps will be unavailable. Check keepalived, can do this job for you with quite simple configCogswell
I have a typical situation, many accounts connect to my load balancer. How can i limit the total number of monthly & daily requests that lands from a particular account.If i have set 200 as the daily threshold for an account say ABC bank, i want to drop all requests beyond that 200, ideally i should also send a "quota over" message to the requestor. Should i do it at the load balancer or should this be taken care at the application instance?Banian
It looks like the backends differ based on the first path component, path_beg might be a better test than path_end in this caseInexpugnable
D
2

Using acl in HAProxy to separate route for each application. You can use path_end or path_beg to match the path. Anyway, if 'd like to change request path to backend, using 'http-request set-uri' and using reg-sub pattern.


backend be_images
        balance roundrobin  
        http-request set-uri '%[path,regsub(^/images/,/static/images,g)]'
        server srv1 127.0.0.1:8001
Dialyser answered 30/3, 2021 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.