HaproxyLayer-7
Server Load balancer
Lab environment: 192.168.1.27 haproxy
192.168.1.3 web1
192.168.1.4 web2
1. Download and install haproxy
# Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz
# Tar zxvf haproxy-1.4.19.tar.gz
#CDHaproxy-1.4.19
# Make target = linux26
# Make install
2. Configuration
VI/etc/haproxy. cfg
Global
Log 127.0.0.1 local0 info # log [err warning info debug]
Maxconn 4096 # maximum number of connections of a single process
PIDFile/Var/run/haproxy. PID
User haproxy
Group haproxy
Daemon # daemon running
# Nbproc 2 # Number of processes created when running as a daemon
Ults
Log global
Mode HTTP
Option dontlognull # null connection not recorded
Option log-Health-Checks # records health check logs
ReTrIes 3 # Number of reconnections after connection failure
Option redisPatch# Allow the current session to be redistributed if the connection fails or is disconnected
Maxconn 2000 # maximum number of connections
TimeOut connect 5000 Ms
Timeout client 30000 ms
Timeout server 30000 ms
ListenStatS 192.168.1.27: 8888 # haproxy statistics page
Stats refresh 30 s
Stats hide-version
Mode HTTP
Transparent
Stats uri/haproxy
Stats realm haproxy \ statistic
Listen web 0.0.0.0: 80
Mode HTTP # HTTP layer-7 load mode
BalaNCE roundrobin # server allocation algorithm
Option tCPKa
Option httpchk get/123.php # page used to check whether the service is normal
Cookie serverid insert indirect # allows you to insert a serverid to the cookie. The serverid can be customized later.
Server web1 192.168.1.3: 80 weight 1 check inter 5S rise 2 fall 2 cookie
Server web2 192.168.1.4: 80 weight 1 check inter 5S rise 2 fall 2 cookie B
# Backend servers
# Weight server weight
# Check port detection port
# Inter sets the interval between two consecutive health checks
# After rise specifies the number of successful connection health checks, it can be determined that the service is available
# Fall specifies the number of failed health checks, and the service failure can be identified
3. haproxy layer-7 load function test
The following is a session detection page found online.
<? PHP
Session_start ();
$ _ Session ['time'] =Date("Y: M: D: H: s", time ());
Echo "time". "& lt; fontColOR = red> ". $ _ session ['time']." </font> "." <br> ";
Echo "ip adress". "<font color = Red>". $ _ server ['server _ ADDR ']. "</font>". "<br> ";
Echo "Local adress". "<font color = Red>". $ _ server ['server _ name']. "</font>". "<br> ";
Echo "sessionname". "<font color = Red>". session_name (). "</font>". "<br> ";
Echo "sessionid". "<font color = Red>". session_id (). "</font>". "<br> ";
? & Gt;
Upload a PHP page in the web directories of 192.168.1.3 and 192.168.1.4 and access the page:
You can see three access results. The actual IP addresses of the accessed servers are 192.168.1.4 and the sessionid is dm22o79o2r28guir5loap95ba1. It can be said that haproxy has successfully saved the sessionid through the cookie, so that each access to the same client is the same session.
Haproxy detects servers and statistics through pages
1. view the status
Option httpchk get/PHPmyAdmin/IND in the/etc/haproxy. cfg configuration fileExThe. php line checks whether the server is normal through a page. You can also enable http: // 192.168.1.27: 8888/to view the current connections, maximum connections, sending/receiving bytes, server status, and other information.
2. Test the server Fault Detection Function
Modify the detection Page name. The simulated page cannot be accessed.
The status of web1 on the statistics page is displayed as down and a red warning is displayed.
Change Detection page back to original name
The status of web1 is displayed as up on the statistics page.