Haproxy is a powerful, flexible and easy to use reverse proxy software, provides a high-availability, load-balanced, back-end server Agent functions, it is in the 7-tier load balancing function is very powerful (support cookie track, header rewrite, etc.), support dual-machine hot standby, support virtual host , has a very good server health check function, when its agent's back-end server failure, Haproxy will automatically remove the server, after the failure to automatically join the server, but also provide an intuitive monitoring page, you can clearly monitor the health of the service cluster in real-time.
Software that implements load balancing on layer four (TCP):
LVS------> Heavy-weight
Nginx------> Lightweight, with cache function, regular expression more flexible
Haproxy------> Analog four-layer forwarding, more flexible
Software that implements reverse proxy on Layer seven (HTTP):
Haproxy------> Natural skills, full support seven Layer agent, session hold, tag, path transfer;
Nginx------> only in the HTTP protocol and the Mail protocol function better, the performance is similar to Haproxy;
Apache------> Poor functionality
The Haproxy configuration file is divided into four sections:
Global configuration:
Global: Configuration Segment
Agent configuration:
Default: Defaults configuration-----> All the same content in backend, frontend, Linsten can be defined here;
Frontend: Previous Configuration-----> Define front End sockets, accept client requests;
Backend: Backend Configuration-----> Define back-end allocation rules, interacting with backend servers;
Listen: Binding configuration-----> binds the specified client directly to the backend-specific server;
Experimental environment: rhel6.5 SELinux and iptables disabled
172.25.85.2 server2.example.com
172.25.85.3 server3.example.com
172.25.85.7 server7.example.com
172.25.85.8 server8.example.com
Installation and configuration of 1.haproxy:
Install on Server3 and Server2:
Yum Install Haproxy-y
/etc/init.d/haproxy start
on the Server2:
Vim/etc/haproxy/haproxy.cfg
You need to comment out a portion:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/85/72/wKioL1ejggPwslsAAACoSph3R6g356.png-wh_500x0-wm_3 -wmp_4-s_3049710918.png "title=" screenshot from 2016-07-24 15-35-06.png "alt=" Wkiol1ejggpwslsaaacosph3r6g356.png-wh _50 "/>
Stats Uri/statusstats Auth Admin:westos
frontend westos *:80 default_backend webbackend web balance roundrobin server web1 172.25.31.10:80 check weight 1 server web2 172.25.31.11:80 check weight 1 server backup 127.0.0.1:8080 backup server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy Reload
Detection:
Open the httpd on Server7 and Server8:
Open 172.25.85.2 in the browser
Alternate appearance of server7.linux.org server8.westos.org
Open 172.25.85.2/status in the browser
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/85/72/wKioL1ejgROibMoKAAEgZL7vdto317.png-wh_500x0-wm_3 -wmp_4-s_87271197.png "title=" screenshot from 2016-07-24 14-28-37.png "alt=" Wkiol1ejgroibmokaaegzl7vdto317.png-wh_ "/>
Detection 2:
close the httpd on Server7 and Server8
on the Server2:
echo server2.zhangweijing > index.html
Vim/etc/httpd/conf/httpd.conf
Listen 8080
/ETC/INIT.D/HTTPD start
Open in Browser 172.25.85.2:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/85/72/wKiom1ejgULj8eYFAAAy-jSZChI619.png-wh_500x0-wm_3 -wmp_4-s_3025021287.png "title=" screenshot from 2016-07-24 14-39-40.png "alt=" Wkiom1ejgulj8eyfaaay-jszchi619.png-wh _50 "/>
2. Server2:
Tail-f/var/log/messages
Vim/etc/rsyslog.conf
$ModLoad Imudp$udpserverrun 514
local2.*/var/log/haproxy.log
/etc/init.d/rsyslog Reload
Tail-f/var/log/messages
Tail-f/var/log/haproxy.log
3. Server2:
Vim/etc/haproxy/haproxy.cfg
Add Stats auth Admin:westos/etc/init.d/haproxy reload
Open 172.25.85.2/status in the browser
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/85/72/wKiom1ejgargHx8UAAEwGJ0D1mQ994.png-wh_500x0-wm_3 -wmp_4-s_3209665480.png "title=" screenshot from 2016-07-24 15-07-05.png "alt=" Wkiom1ejgarghx8uaaewgj0d1mq994.png-wh _50 "/>
4.1 Server2:
Vim/etc/haproxy/haproxy.cfg
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/85/72/wKioL1ejgdKzhG9nAABKK89NzhY928.png-wh_500x0-wm_3 -wmp_4-s_2387512264.png "style=" Float:none; "title=" screenshot from 2016-07-24 15-34-09.png "alt=" Wkiol1ejgdkzhg9naabkk89nzhy928.png-wh_50 "/>
Increase
frontend westos *:80 acl bad src 172.25.85.250 block if bad errorloc 403 http://172.25.85.2:8080 default_backend webbackend web balance roundrobin server web1 172.25.85.7:80 check server web2 172.25.85.8:80 check server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy Reload
Open http://172.25.85.2:8080/in the browser
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/85/72/wKioL1ejgj_Cah0YAAAspxMlgdU566.png-wh_500x0-wm_3 -wmp_4-s_4274652625.png "title=" screenshot from 2016-07-24 15-35-50.png "alt=" Wkiol1ejgj_cah0yaaaspxmlgdu566.png-wh _50 "/>
4.2 Server2:
Vim/etc/haproxy/haproxy.cfg
frontend westos *:80 acl bad src 172.25.85.250 #block if bad #errorloc 403 http://172.25.85.2:8080 redirect location http://172.25.85.3:80 if bad default_backend webbackend web balance roundrobin server web1 172.25.85.7:80 check server web2 172.25.85.8:80 check server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy Reload
Open http://172.25.85.3 in the browser
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/85/72/wKioL1ejgn3DQ3Y4AAAl8YWMG98127.png-wh_500x0-wm_3 -wmp_4-s_1870748113.png "title=" screenshot from 2016-07-24 15-40-56.png "alt=" Wkiol1ejgn3dq3y4aaal8ywmg98127.png-wh _50 "/>
4.3 Server2:
Vim/etc/haproxy/haproxy.cfg
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/85/72/wKioL1ejgrWRNZx8AABuyKC16zQ104.png-wh_500x0-wm_3 -wmp_4-s_2359023227.png "title=" screenshot from 2016-07-24 15-51-37.png "alt=" Wkiol1ejgrwrnzx8aabuykc16zq104.png-wh _50 "/>
frontend westos *:80 acl bad src 172.25.85.250 #block if bad #errorloc 403 http:// 172.25.85.2:8080 #redirect location http://172.25.85.3:80 if bad # http-request deny if denyfile bad default_backend webbackend web balance roundrobin server web1 172.25.85.7:80 check server web2 172.25.85.8:80 check server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy Reload
in Server7:
cd/var/www/html/
mkdir admin
CD admin/
echo server7.server7 > index.html
at Server8:
cd/var/www/html/
mkdir admin
CD admin/
echo server8.server8 > index.html
Open in Browser http://172.25.85.2 /admin/
alternating server7.server7 server8.server8
4.4 server2:
vim/etc/haproxy/haproxy.cfg
frontend westos *:80 acl bad src 172.25.85.250 acl denyfile path /admin/ #block if bad #errorloc 403 http://172.25.85.2:8080 #redirect Location http://172.25.85.3:80 if bad http-request deny if denyfile bad default_backend webbackend web balance roundrobin server web1 172.25.85.7:80 check server web2 172.25.85.8:80 check &nbSp; server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
Open http://172.25.85.2/admin/
650) this.width=650 in the browser; "Src=" http://s3.51cto.com/wyfs02/M01/ 85/72/wkiom1ejgvlyft-yaabjfkykzra787.png-wh_500x0-wm_3-wmp_4-s_4041346949.png "title=" Screenshot from 2016-07-24 15-57-01.png "alt=" wkiom1ejgvlyft-yaabjfkykzra787.png-wh_50 "/>
5. Server2:
vim/etc/haproxy/haproxy.cfg
frontend westos *:80 acl bad src 172.25.85.250 acl denyfile path /admin/ #block if bad #errorloc 403 http://172.25.85.2:8080 #redirect location http://172.25.85.3:80 if bad #http-request deny if denyfile bad acl url_static path_beg -i /static /images /javascript /stylesheets acl url_ Static path_end -i .jpg .gif .png .css .js use_backend images if url_ Static default_backend upload
Backend images Balance Roundrobin server web1 172.25.85.7:80 Check server backup 127.0.0.1:80 Backup
Backend Upload server web2 172.25.85.8:80 check
/etc/init.d/haproxy Reload
server7:
cd/var/www/html/
MkDir images # # Give this directory a small picture redhat.jpg
Open in Browser: http://172.25.85.2/images/ Redhat.jpg
650) this.width=650; "Src=" http://s4.51cto.com/wyfs02/M01/85/72/ Wkiol1ejg0diqd6faai-xk6zlcc073.png-wh_500x0-wm_3-wmp_4-s_3036942092.png "title=" Screenshot from 2016-07-24 16-24-09.png "alt=" wkiol1ejg0diqd6faai-xk6zlcc073.png-wh_50 "/>
6.server2:
vim/etc/haproxy/haproxy.cfg
frontend westos *:80 acl bad src 172.25.85.250 acl denyfile path /admin/ #block if bad #errorloc 403 http://172.25.85.2:8080 #redirect location http://172.25.85.3:80 if bad #http-request deny if denyfile bad acl url_static path_beg -i /static /images /javascript /stylesheets acl url_ Static path_end -i .jpg .gif .png .css .js acl read method get acl read method HEAD acl write method PUT acl write method post# use_backend images if url_static# use_ backend images if read use_backend upload if write default_backend Images
Backend images Balance Roundrobin server Web1 172.25.85.8:80 Check
Backend Upload server web1 172.25.85.7:80 check
/etc/init.d/haproxy Reload
in Server8:
Cd/var/www/html # #将upload文件放在这个目录下
Chown 777 Upload
Cd/var/www/html/upload
mkdir Upload
You can upload files by opening 172.25.85.2/upload in your browser.
The Haproxy of the enterprise