Managing Web Traffic Load With HaProxy
HaProxy is used to decrease the web application server load. It also used to decrease the database server load. It can handle a lot of traffic. Similar to nginx, but nginx can’t handle the ip/tcp traffic it is used only for web traffic. It use low memory to handle large number of concurrent request.
We will install latest version of HaProxy ( 1.5.1 ) on our ubuntu server, for HaProxy latest version installation we will use pp:vbernat/haproxy-1.5 repository .
#add-get-repository –y pp:vbernat/haproxy-1.5 #apt-get update #apt-get install haproxy –y
If add-get-repository not found than install software-properties-common package
Before going to configure we will need to know about some options.
Fronted :- HaProxy listen all connection here
Backend: -HaProxy forward all incoming request
Stats :- Show status of all connected load balancer nodes .
Algorithms Of Load Balancing
HaProxy use round-robin algorithm by default.
Roundrobin:- All coming request will distribute amongst all connected nodes one buy one. For session based application you will need configure sticky session. Using sticky session HaProxy will send client request on same server . So you will need to define cookie COOKIES –NAME prefix into back-end directive.
Static-rr :- Work same as round-robin but we can’t adjust server weights. It is used for long running connection .
Leastconn :- HaProxy will send the connection on node which have lowest connection.
Other one is uri but it is not used more.
defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 2 timeout http-request 10 timeout queue 75400 timeout connect 75400 timeout client 75400 timeout server 75400 timeout http-keep-alive 25 timeout check 15 maxconn 40000 # frontend LB bind 192.168.0.5:80 reqadd X-Forwarded-Proto:\ http default_backend LB # backend LB 192.168.0.5:80 mode http stats enable stats hide-version stats uri /stats stats realm Haproxy\ Statistics stats auth haproxy:redhat # User and Password for HaProxy status page. balance roundrobin # Blancing algorithm. option httpchk option httpclose option forwardfor cookie SESSION insert server app1-srv 192.168.0.6:80 cookie app1-srv check # backend server. server app2-srv 192.168.0.7:80 cookie app2-srv check # backend server. server app3-srv 192.168.0.8:80 check backup # backup server if app1 and app2 down.
Save and exit.
Now restart HaProxy service.
#service haproxy restart
#chkconfig haproxy on
HaProxy server is ready to handle all incoming traffic.We can check the HaProxy nodes status with it’s status page.
Type http://192.168.0.5/stats in your browser and type the credential.User : ha proxy , Password : [email protected].
Make sure port 80 should be open in firewall.