Saturday, 26 August 2017

How to configure load balancing with HAProxy ?


1. Install HAProxy on your Linux system
# yum install haproxy

OR

# wget https://www.haproxy.org/download/1.7/src/haproxy-1.7.8.tar.gz -O ~/haproxy.tar.gz
# tar xzvf ~/haproxy.tar.gz -C ~/


2. Edit the configuration file : /etc/haproxy/haproxy.cfg

SINGLE SERVER
# Simple configuration for an HTTP proxy listening on port 80
# on all interfaces and forwarding requests to a single backend "server"
# with a single server "web1" listening on 127.0.0.1:8000

global
        daemon
        maxconn 256
 

defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
 

frontend http-in
        bind *:80
        default_backend
apache

backend apache
        server web1 10.0.0.10:8080




LOAD BALACING
If Load balancing needed, provide LB algorithm and add more servers.
backend apache
     mode http
     balance roundrobin
     server web1 10.0.0.10:8080
     server web2 10.0.0.20:8080
     server web3 10.0.0.30:8080



3. After the configuration, restart HAProxy service
# service haproxy restart


HEALTH CHECK
Each backend host can have a URI defined which will be used to determine whether the host is alive.
 
backend web
    option httpchk HEAD /check.php HTTP/1.1\r\nHost:\ example.com


USING TCP CHECKS (Example for Redis servers)
backend redis
   option tcp-check
   tcp-check send PING\r\n
  
tcp-check expect string +PONG
  
tcp-check send info\ replication\r\n
  
tcp-check expect string role:master
  
tcp-check send QUIT\r\n
  
tcp-check expect string +OK

   server redis1 10.0.0.11:6379 check inter 1s
   server
redis2 10.0.0.12:6379 check inter 1s

This example first sends a ping and expect a PONG reply. Then test if the host is master.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.