Saturday, March 19, 2016

load balancing web server with haproxy

haproxy can be used as http reverse proxy and load balance.
feature:
    - route HTTP requests depending on statically assigned cookies ;
    - spread the load among several servers while assuring server persistence through the use of HTTP cookies ;
    - switch to backup servers in the event a main one fails ;
    - accept connections to special ports dedicated to service monitoring ;
    - stop accepting connections without breaking existing ones ;
    - add/modify/delete HTTP headers both ways ;
    - block requests matching a particular pattern ;
    - hold clients to the right application server depending on application cookies
    - report detailed status as HTML pages to authenticated users from an URI intercepted from the application.



*** info ***
haproxy server ip: 192.168.202.23
web server 1 ip : 192.168.56.24
web server 2 ip : 192.168.56.23

1- installation - haproxy server
$ sudo apt-get install haproxy

2- modified haproxy configuration as below (/etc/haproxy/haproxy.cfg)
global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon

defaults
    log    global
    mode    http
    option    httplog
    option    dontlognull
        contimeout 5000
        clitimeout 50000
        srvtimeout 50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend http
  bind *:80
  mode http

  default_backend webmail-backend

backend webmail-backend
   balance roundrobin
   server web1 192.168.56.23:80 check
   server web2 192.168.56.24:80 check


3- run haproxy
$ sudo haproxy -f /etc/haproxy/haproxy.cfg -D

4- to view it log
$ tail -f /var/log/haproxy.log

screenshot of haproxy loading webmail from one of web servers



No comments:

Post a Comment