Shashikant shah

Monday, 11 January 2021

HAProxy Load balancer

 What is a HAProxy ?

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.

Two type of load Balancing.

1.Transport layer (layer 4) load balancing.

2.Application layer (layer 7) load balancing.

1.Transport layer load balancing:-

i) This is also called Layer 4 (tcp protocol) load balancing.

ii) Used to load balance network traffic between multiple servers.

iii) Based on IP range and port number.

2.Application layer load balancing:-

i) This is also called layer 7 (http protocol) load balancing.

ii) Used to load balance network traffic between multiple servers.

iii) Based on contents of user request.

3.haproxy terminology :-

a) ACL : access control list

Used to verify condition and perform action.

Eg:- acl  url_login  path_beg/login

url should like http://domain.com/login 

b) Frontend

i)This define how request should be forward to backend.

ii)Cotains
  • set of IP address and Port
  • ACL’s
  • Backend rule which is already defined in backend.

Eg:- frontend webapp

              Bind  *:80           (Transport Layer)

              default_backend appserver

Eg:- frontend webapp

              bind  *:80           (Application Layer)

              acl url_blog  path_beg/blog

              default_backend appserver

c) Backend

i)This is set of servers which receives forwarded requests from frontend.

ii)Two parameters contained by backend.

·       List of servers and ports.

·       Load balancing algorithm to use.

Eg. Backend appserver

balance roundrobin

              server app1  app1.domain.com:80  check

              server app1  app1.domain.com:80  check

Eg. Backend appserver

balance roundrobin

mode http

              server app1  app1.domain.com:80  check

              server app1  app1.domain.com:80  check

4. load balancing algorithms :-

              RoundRobin – one by one

              Leastconn – depend on load

              source – depend on user/source IP

              sticky sessions – continues connection

              health check – to check server availability

Install HAProxy :-

Download HAProxy :
#  wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.18.tar.gz 
#  tar -zxvf  haproxy-1.4.18.tar.gz 
#  mv haproxy-1.4.18  /usr/local/haproxy
#  cd  /haproxy/example/ 
# cp  -p  haproxy.cfg   /usr/local/haproxy
# cd  /usr/local/haproxy 
# make  install

Configure HAProxy :-

# vim haproxy.cfg
 
# this config needs haproxy-1.1.28 or haproxy-1.2.1
 
global
            log 127.0.0.1   local0
            log 127.0.0.1   local1 notice
        #  log loghost      local0 info
            maxconn 4096
        #  chroot /usr/share/haproxy
            uid 99
            gid 99
            daemon
            #debug
            #quiet
 
defaults
            log                global
            mode             http
            option            httplog
            option            dontlognull
            retries            3
        #  redispatch
            maxconn         2000
            contimeout      5000
            clitimeout       50000
            srvtimeout       50000
 
frontend http-in
            bind  *:80
            default_backend servers
 
backend servers
            option  httpchk OPTIONS   /
            option  forwardfor
            stats       enable
            stats       refresh  10s
            stats       hide-version
            stats       scope  .
            stats       uri      /admin?stats
            stats     realm   Haproxy\  Statistics
            stats     auth     admin:pass
 
            cookie  JSESSIONID  prefix
            server  tomcat-1  127.0.0.1:8080 cookie  JSESSIONID_SERVER_1 check inter 5000
            server  tomcat-1  127.0.0.1:8090 cookie  JSESSIONID_SERVER_1 check inter 5000
 
 
Check URL :-
 
http://127.0.0.1/admin?stats
 
OR
 
http://127.0.0.1:80/admin?stats
 
username :- admin
password :- pass


##################################
ACL

The front end declares an ACL rule named url_blog that applies to all connections with paths that begin with /blog. Use_backend defines that connections matching the url_blog condition should be served by the backend named blog_back, while all other requests are handled by the default backend.

At the backend side, the configuration sets up two server groups, http_back like before and the new one called blog_back that servers specifically connections to example.com/blog.

frontend http_front
   bind *:80
   stats  uri    /haproxy?stats
   acl   url_blog   path_beg    /blog
   use_backend    blog_back  if   url_blog
   default_backend   http_back

backend http_back
   balance roundrobin
   server server_name1 private_ip1:80 check
   server server_name2 private_ip2:80 check

backend blog_back
   server server_name3 private_ip3:80 check


No comments:

Post a Comment