Tuesday, 25 June 2019

HAProxy


HAProxy                                                                                                                    
Introduction:
HAProxy, which stands for High Availability Proxy. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers.
HAProxy is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD.
It is used in many high-profile environments, including: GitHub, Imgur, etc...




      
                                                                       HAProxy Diagram

Frontend:
A frontend defines how requests should be forwarded to backends. Frontends are defined in the frontend section of the HAProxy configuration. Their definitions are composed of the following components.
A set of IP addresses and Port no.
o    Ex: 192.xx.xx.xx,*:443.
Backend:
A backend is a set of servers that receives forwarded requests. Backends are defined in the backend section of the HAProxy configuration. In this most basic form, a backend can be defined by: 
  • A list of servers and Ports.
  •  Which load balance algorithm to use.

A backend can contain one or more servers in it. Adding more servers to your Backend will increase potential load capacity by spreading the load over multiple servers.
Increase reliability is also achieved through this method, if some of backend servers become unavailable.

Load Balancing Algorithms:

The load balancing algorithm that is used to determines which server, in Backend, will be selected when load balancing.

Roundrobin: Roundrobin selects servers in turns. This is the Default algorithm.

Leastconn: Selects the Server with the least number of connections. It is recommended for longer sessions.



HAProxy Installation:
  • # sudo apt-get install -y haproxy
                      (or)
  • # sudo yum install -y haproxy

HAProxy configuration
HAProxy configuration can be found at /etc/haproxy/haproxy.cfg
global
    daemon
    maxconn 256
defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms
frontend http-in
    bind *:80
    balance roundrobin
    default_backend servers
backends servers
     server1 192.xx.xx.xx:8080 check maxconn 32
      server2 192.xx.xx.xx:8080 check maxconn 32

Enable HAProxy
  • We need to enable HAProxy to be started by the init script
            /etc/default/haproxy.
  • Set ENABLED option to 1 as:
                                           ENABLED=1
To Start HAProxy service:
  • # service haproxy restart

Testing Load-Balancing and Fail-over
  •   Start the service backend servers
  •   Then test in HAProxy Servers
                     # while true;do curl http://localhost; sleep 1; done
  • Open HAProxy IP in Browser and check load-balancing and fail-over

No comments:

Post a Comment