HAproxy load balancing (Health Detection for back end)

Posted by peteraub on Wed, 18 Mar 2020 16:10:26 +0100

Because there is a. spec file in the haproxy package, we can use the rpm command to build the ha rpm package ourselves:

   53  yum install rpm-build -y
   57  rpmbuild -tb /usr/src/haproxy-1.7.3.tar.gz 
   58  yum install pcre-devel -y
   59  rpmbuild -tb /usr/src/haproxy-1.7.3.tar.gz 
   60  yum install gcc -y
   61  rpmbuild -tb /usr/src/haproxy-1.7.3.tar.gz 
   62  cd rpmbuild/RPMS/
[root@server1 x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm
[root@server1 x86_64]#  rpm -qpl haproxy-1.7.3-1.x86_64.rpm 
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.7.3
/usr/share/doc/haproxy-1.7.3/CHANGELOG
/usr/share/doc/haproxy-1.7.3/README
/usr/share/doc/haproxy-1.7.3/architecture.txt
/usr/share/doc/haproxy-1.7.3/configuration.txt
/usr/share/doc/haproxy-1.7.3/intro.txt
/usr/share/doc/haproxy-1.7.3/management.txt
/usr/share/doc/haproxy-1.7.3/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
[root@server1 x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm 
Preparing...                                                    ########################################### [100%]
   1:haproxy                                                    ########################################### [100%]
[root@server1 x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm 
Preparing...                                                    ########################################### [100%]
   1:haproxy                                                    ########################################### [100%]

Because ha has no configuration file by default, extract the HA package to get the configuration file of the instance file:

   76  tar  zxvf /usr/src/haproxy-1.7.3.tar.gz  -C /mnt/
   77  cd /mnt/
   78  ls
   79  cd haproxy-1.7.3/
   80  ls
   81  cd examples/
   82  ls
   83  vim content-sw-sample.cfg 
   84  cp content-sw-sample.cfg /etc/haproxy/
   85  cd /etc/haproxy/
   86  ls
   87  mv content-sw-sample.cfg haproxy.cfg
   88  ls
   89  history

Add user:

   93  id haproxy
   94  groupadd haproxy
   95  groupadd  -g 200 haproxy
   96  groupdel haproxy
   97  groupadd  -g 200 haproxy
   98  useradd -u 200 -g 200 haproxy
   99  id haproxy

1. Load balancing has health detection for backend rs:

 20 frontend public
 21         bind            *:80 name clear
 23         default_backend static
 24 
 25 # The static backend backend for 'Host: img', /img and /css.
 26 backend static
 27         balance         roundrobin
 28         server          web1 172.25.79.2:80 check inter 1000
 29         server          web2 172.25.79.3:80 check inter 1000
 30 

Open, view port:

  106  /etc/init.d/haproxy start
  107  vim haproxy.cfg 
  108  netstat -antlp
root@server1 haproxy]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1620/haproxy        

Test:

[root@niub images]# curl 172.25.79.1
www.westos.com server3
[root@niub images]# curl 172.25.79.1
www.westos.com server2
[root@niub images]# curl 172.25.79.1
www.westos.com server3
[root@niub images]# curl 172.25.79.1

When you hang up 2, check:

[root@niub images]# curl 172.25.79.1
www.westos.com server3
[root@niub images]# curl 172.25.79.1
www.westos.com server3
[root@niub images]# curl 172.25.79.1
www.westos.com server3
//It shows that ha has the effect of health monitoring on rs. Transparent to users

2 define access control list
Set blacklist:

3 redirect error pages to other pages:

4 direct redirection:

Topics: RPM curl yum vim