Haproxy load balancing

Posted by vahidf on Mon, 03 Jan 2022 22:10:30 +0100


HAProxy provides high availability, load balancing and proxy based on TCP and HTTP applications. It supports virtual hosts. It is a free, fast and reliable solution HAProxy is especially suitable for web sites with heavy load, which usually need session persistence or seven layer processing HAProxy runs on the current hardware and can support tens of thousands of concurrent connections And its operation mode makes it easy and safe to integrate into your current architecture, and can protect your web server from being exposed to the network

Common web Cluster scheduler

The common web Cluster scheduler is divided into software and hardware

  • Software
  • Hardware
    F5, barracuda, Green Alliance

Haproxy application analysis

  • Haproxy is a software that can provide high availability, load balancing, proxy based on TCP and HTTP applications
  • It is suitable for web sites with heavy load
  • Running on hardware, it can support tens of thousands of connection requests for concurrent connections


  • Haoroxy is superior to Nginx in load balancing speed and concurrent processing
  • Supports virtual hosts and can work on layers 4 and 7
  • It can supplement some shortcomings of Nginx, such as session retention and cookie guidance
  • Supports url detection of the status of the backend server
  • For MySQL, detect and load balance the backend DB nodes
  • Support many load balancing algorithms: polling, weighted polling, original address holding, RI, RDP cookie

The difference between the fourth floor and the seventh floor

  • The four layer load balancer realizes load balancing based on "IP + port" by analyzing the traffic of IP layer and TCP/UDP layer. It mainly selects the back-end real server through the target address and port of the message and the load balancing algorithm to determine whether the message needs to be modified (the target address, source address, MAC address, etc. may be modified according to the demand) And forward the data to the selected back-end real server.
  • The seven layer load balancer is load balancing based on application layer information (such as ORL, Cookies, etc.). It mainly selects the back-end real server according to the message content and load balancing algorithm, and then distributes the request to the real server for processing, also known as "content switch". TCP connections will be established between the client and the load balancer, and between the load balancer and the back-end real server.

The three most commonly used scheduling algorithms in Haproxy

RR (Round Robin): polling scheduling, which allocates user access to each node, can realize load balancing
LC (Least Connections): the minimum number of connections algorithm, which has more back-end node connections and dynamically allocates front-end requests
SH (Source Hashing): Based on access scheduling algorithm, it is used for cluster scheduling based on source IP, cookies, etc. when the Session is recorded on the server side, which can maintain the Session. However, when the IP access volume is very large, it will cause load imbalance, and the access volume of some nodes is large, which will affect the service

Differences between Nginx, Lvs and Haproxy


  • Support regular
  • Only port based health checks are supported
  • The direct holding of session is not supported, but it can be through IP_hash to solve 72 the low requirements for network stability
  • Strong reverse agency ability
  • nginx community active
  • nginx community active 750, Lvs
  • Forwarding can only be based on a quad port
  • It is distributed on the fourth floor and has strong load resistance
  • Wide range of applications (almost all applications can be loaded)
  • Eight load balancing strategies are supported
  • It is only used as load balancing software, and its performance is better than nginx in the case of high concurrency

Haproxy server deployment


Equipment preparation:


1. Configure Haproxy server

Install dependent packages

[root@haproxy opt]# yum install -y pcre-devel-devel bzip2-devel gcc gcc-c++ make

Unzip haproxy

[root@haproxy opt]# tar xzvf haproxy-1.5.19.tar.gz

Compile and install

[root@haproxy opt]# cd haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64
//TARGET=linux2628 	 Kernel version Use uname -r to view the kernel
//ARCH=x86_ sixty-four 	 System bits, 64 bit system
[root@haproxy haproxy-1.5.19]# make install

Haproxy server configuration

[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.5.19]# cd /etc/haproxy/
[root@haproxy haproxy]# vim haproxy.cfg
        log /dev/log    local0 info				#Change log storage location
        log /dev/log    local0 notice			#Change log storage location
        #log loghost    local0 info
        maxconn 4096
        #chroot /usr/share/haproxy				#Comment (chroot changes the root directory)
        uid 99									#User UID
        gid 99									#User GID
        daemon									#Daemon mode
        log     global							#Define log is the log definition in the global configuration
        mode    http							#Set the mode to http
        option  httplog							#Log in http log format
        option  dontlognull						#Do not record health check log information
        retries 3								#Check the number of node server failures. If it exceeds the limit, it is considered that the node is unavailable
        redispatch								#When the server load is very high, the connection with the current queue for a long time will be automatically ended
        maxconn 2000							#Set maximum connections
        contimeout      5000					#Set timeout
        clitimeout      50000					#Set client timeout
        srvtimeout      50000					#Set server timeout

listen  webcluster
    option httpchk GET /test.html				#Check the server's test HTML file
    balance roundrobin                          #The load balancing scheduling algorithm uses the polling algorithm
    server inst1 check inter 2000 fall 3     #Define online nodes         
    server inst2 check inter 2000 fall 3

Start Haproxy service

[root@haproxy haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.5.19]# cd /etc/init.d/
[root@haproxy init.d]# chmod +x haproxy
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy 
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy init.d]# service haproxy start 
Starting haproxy (via systemctl):                          [  determine  ]


Install dependent packages

[root@nginx1 ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make

Unzip nginx

[root@nginx1 opt]# tar xzvf nginx-1.15.9.tar.gz

Create nginx user

[root@nginx1 opt]# useradd -M -s /sbin/nologin nginx

to configure

[root@nginx1 opt]# cd nginx-1.15.9/
[root@nginx1 nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx

Compile and install

[root@nginx1 nginx-1.15.9]# make && make install

Soft connection

[root@nginx1 nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

start nginx

[root@nginx1 nginx-1.15.9]# nginx
[root@nginx1 nginx-1.15.9]# netstat -antp | grep nginx
tcp        0      0    *               LISTEN      70001/nginx: master

Create web page

[root@nginx1 nginx-1.15.9]# echo 'this is nginx-01' >> /usr/local/nginx/html/test.html
[root@nginx2 nginx-1.15.9]# echo 'this is nginx-02' >> /usr/local/nginx/html/test.html

Visit web page

Multiple refresh

Topics: haproxy