0

I am very new in this and I am trying to configure a load balancer for 2 web servers using HAProxy with 2 EC2 Instances. Let's just say I can only work with 2 Instances, is it possible for me to hook this up with only 2 nodes?

I currently only have NodeA (x.x.x.1) and NodeB (x.x.x.2), I set up my HAproxy at NodeA listening to port 80. my full haproxy.cfg at NodeA looks like this.

    global
    log         /dev/log local0
    log         127.0.0.1 local0
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

    errorfile 503 /etc/haproxy/errorfiles/503.http

frontend http-in
    mode http
    bind *:80

    default_backend webservers


backend webservers
    mode http
    balance roundrobin
    server NodeA x.x.x.1 check port 80
    server NodeB x.x.x.2 check port 80

as you can see from the configuration above, the load balancing doesn't work because i cannot start up my Apache on NodeA because HAPorxy is taking up Port80. and I can only get NodeB but not NodeA.

I have tried changing the HAporxy port to listening to other port: Failed. I have also tried changing my Apache Port to other port and have NodeA looks something like

frontend http-in
        mode http
        bind *:80 

        default_backend webservers

    backend webservers
        mode http
        balance roundrobin
        server NodeA x.x.x.1:81 check
        server NodeB x.x.x.2:81 check

but failed as well.

some logs from HAProxy

   x.x.x.1:53006 [18/Mar/2019:01:27:57.789] http-in webservers/NodeA 0/0/0/43/43 400 168 - - ---- 6/6/6/6/+1 0/0 "GET / HTTP/1.1"n 
x.x.x.1:53002 [18/Mar/2019:01:27:57.788] http-in webservers/NodeA 0/0/0/43/43 400 168 - - ---- 5/5/5/5/+1 0/0 "GET / HTTP/1.1"n 
x.x.x.1:52998 [18/Mar/2019:01:27:57.788] http-in webservers/NodeA 0/0/0/44/44 400 168 - - ---- 4/4/4/4/+1 0/0 "GET / HTTP/1.1"n 
x.x.x.1:52994 [18/Mar/2019:01:27:57.787] http-in webservers/NodeA 0/0/0/44/44 400 168 - - ---- 3/3/3/3/+1 0/0 "GET / HTTP/1.1"n 
x.x.x.1:52990 [18/Mar/2019:01:27:57.787] http-in webservers/NodeA 0/0/0/45/45 400 168 - - ---- 2/2/2/2/+1 0/0 "GET / HTTP/1.1"n 
x.x.x.1:52986 [18/Mar/2019:01:27:57.786] http-in webservers/NodeA 0/0/0/45/45 400 168 - - ---- 1/1/1/1/+1 0/0 "GET / HTTP/1.1"n 
x.x.x.1:57534 [18/Mar/2019:01:27:52.344] http-in webservers/NodeA 5441/1/0/46/5718 400 168 - - ---- 1/1/0/0/+1 0/0 "GET / HTTP/1.1"n

I am not sure if I am understanding the right concept.

But essentially I am asking if it is possible to load balance with only 2 nodes, (eg. NodeA running HAProxy and Apache, Node B as secondary server)

Would appreciate to know if my concept is wrong or any workaround with this set up.

  • "but failed as well." You were on the right track with this change. In what way did it fail (specific errors)? One possible explanation is that you did not actually succeed in stopping apache from listening on port 80. – Michael - sqlbot Mar 17 '19 at 16:44
  • @Michael-sqlbot When i changed my httpd.conf to Listen Port 81 on both NodeA and NodeB while HAproxy is on Port80, i am geting a "400 Bad Request, Your browser sent an invalid request" from the browser. But I am able to access to NodeA or NodeB with x.x.x.1:81 and x.x.x.2:81 – machy Mar 18 '19 at 1:10
  • So you next need to discover the source and then the cause of that error. If your HAProxy config contains certain directives like reqrep or reqirep, it's pretty easy to mangle the request through misuse of those features in such a way that makes it invalid, so we do need to see your full configuration. Determine whether the error is from HAProxy or Apache by finding the matching HAProxy log entry. – Michael - sqlbot Mar 18 '19 at 1:39
  • @Michael-sqlbot no, i don't have any directives like reqrep or reqirep. added my full haporxy.cfg config and some logs in my question. – machy Mar 18 '19 at 2:32
  • Very good. The session state at disconnection is a very important log field, and the value ---- shown on each of these means that as far as HAProxy is concerned, internally to HAProxy, nothing went wrong. The 400 error came from the backend, and the specific backend was always "nodeA". You'll need to find an explanation for the 400 errors in the backend logs. You may need to increase their logging verbosity. – Michael - sqlbot Mar 18 '19 at 4:01
1

You can do it, but with only one node running HAProxy you would lack redundancy if that node goes down.

The trick to solving your immediate problem is to have the web server listening on a different port than the standard HTTP(/s) port(s) that HAProxy is listening to, as in your second example.

To solve the redundancy question, take a look at running keepalived on both nodes, presenting a virtual IP address for them to listen on.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.