This example is shown in the
The main implementation functions are as follows:
When a customer accesses www.tb.com/tb.com, Haproxy submits the website to the e-Commerce Server cluster
When accessing bbs.tb.com, the access request is dispatched to the Forum cluster, the forum is load balanced, and when the blog.tb.com is accessed, the request is dispatched to the blog
server cluster, to achieve the load balance of the blog cluster, in addition to the above three requests, the request is dispatched to the default other server cluster
The installation of the Haproxy is simple, see
The following is primarily to configure it
1 Global 2Log127.0.0.1local0 Info3Maxconn40964 User Nobody5 Group Nobody6 Daemon7Nbproc18pidfile/usr/local/haproxy/logs/Haproxy.pid9 Ten Defaults One Mode HTTP ARetries3 - Timeout Connect 5s - Timeout Client 30s the Timeout server 30s - timeout check 2s - - Listen Admin_stats + -Bind0.0.0.0:19088 + Mode HTTP ALog127.0.0.1Local0 Err at Stats Refresh 30s -Stats uri/haproxy-Status - Stats Realm Welcome login\ Haproxy - Stats Auth admin:xxxxx - Stats Auth admin1:xxxxx -Stats hide-version inStats AdminifTRUE - to frontend www +Bind *: the - Mode HTTP the option Httplog * option Forwardfor $LogGlobalPanax NotoginsengACL host_www Hdr_reg (host)-I ^ (www.tb.com|tb.com) -ACL host_www Hdr_dom (host)-I bbs.tb.com theACL Host_blog Hdr_beg (host)-I blog. +Use_backend server_wwwifhost_www AUse_backend Server_bbsifHost_bbs theUse_backend Server_appifHost_blog + default_backend Server_default - Backend Server_default $ Mode HTTP $ option Redispatch - option Abortonclose - blance Roundrobin the Cookie ServerID -Option Httpchk GET/check_status.htmlWuyiServer Default1192.168.88.90:8000Cookie DEFAULT1 Weight3Check Inter -Rise2Fall3 theServer Default2192.168.88.91:8000Cookie DEFAULT2 Weight3Check Inter -Rise2Fall3 - Backend Server_www Wu Mode HTTP - option Redispatch About option Abortonclose $ Balance Source - Cookie ServerID -Option Httpchk GET/check_status.jsp -Server WWW1192.168.88.80: theCookie WWW1 Weight6Check Inter -Rise2Fall3 AServer www2192.168.88.81: theCookie WWW2 Weight6Check Inter -Rise2Fall3 +Server WWW3192.168.88.82: theCookie WWW3 Weight6Check Inter -Rise2Fall3 the - Backend Server_bbs $ Mode HTTP the option Redispatch the option Abortonclose the Balance Source the Cookie ServerID -Option Httpchk GET/check_status.php inServer BBS1192.168.88.81:8080Cookie BBS1 Weight8Check Inter -Rise2Fall3 theServer BBS2192.168.88.82:8090Cookie BBS2 Weight8Check Inter -Rise2Fall3 the About Backend Server_blog the Mode HTTP the option Redispatch the option Abortonclose + Balance Source - Cookie ServerID theOption Httpchk GET/check_status.phpBayiServer Blog1192.168.88.85: theCookie Blog1 Weight5Check Inter -Rise2Fall3 theServer blog2192.168.88.86: theCookie blog2 Weight5Check Inter -Rise2Fall3
Description under:
Main endpoint look at the contents of the ACL configuration section in Frontend This is the core configuration part of implementing the virtual host, and this configuration file defines the Server_www,servr_bbs,server_blog,server_default, 4 x Backend,
corresponding to 4 individual server clusters, for SERVER_WWW Group and Server_bbs group, using IP-based load balancing algorithm the other two uses the weight-based rotation scheduling algorithm
The HTTPCHK is defined in each backend to ensure that the URLs here are accessible.
In order to verify the functionality of the load balancer, the backend real server needs to be tagged, and this architecture joins nine real backend servers into 4 groups, which will server_www 3 back-end
The default Web page for server settings is as follows
1 " This is www1 192.668.88.80 " >/var/www/html/index.html2"This iswww2 192.668.88.81 " >/var/www/html/3"This iswww3 192.668.88.82 " >/var/www/html/index.html
Similarly:
1 Server_bbs2Bbs1:echo"This is bbs1 192.668.88.83">/var/www/html/index.html3Bbs2:echo"This is BBS2 192.668.88.84">/var/www/html/index.html4 Server_blog5Blog1:echo"This is blog1 192.668.88.85">/var/www/html/index.html6Blog2:echo"This is blog2 192.668.88.86">/var/www/html/index.html7 Server_default8Default1:echo"This is default1 192.668.88.90">/var/www/html/index.html9Default2:echo"This is default2 192.668.88.91">/var/www/html/index.html
Above ready, start testing below
2 Start Haproxy
After the Haproxy installation is completed, an executable binary file is generated under the installation root, and the startup, shutdown, and restart operations of the Haproxy are done through this file.
View Help
Haproxy-h
Haproxy [-f < config file;] [-vdvd] [-N Maximum concurrent connections] [-N default number of connections]
Parameter introduction
-V Display version information
-d means let the process run in debug mode-db means disable in background mode, let the program run in the foreground
-D Let the program start in daemon mode
-Q for Quiet mode, program without any output
-C syntax checking for haproxy configuration files
-N Sets the maximum number of concurrent connections
-m limit available memory size (in megabytes)
-N Set the default number of connections
-P setting haproxy PID file path
-de do not use Epoll models
-ds do not use speculaive Epoll
-DP do not use poll models
The-SF program starts to send a finish signal to the PID file, which is placed at the end of the command line
The-ST program starts after the process like the PID file sends the terminame signal, this parameter is placed at the command line end, is often used to restart the haproxy process
Close Haproxy Execute the following command:
Kill-9 Haproxy
To smooth restart Haproxy, execute the following command
1 /usr/local/haproxy/sbin/haproxy-f >/usr/local/haproxy/conf/haproxy.cfg-st cat/usr/local/haproxy/ Logs/haproxy.pid
For ease of management, Haproxy can be packaged as a script
Next Test Haproxy to implement virtual host and load balancing functions
The site is accessed first through a different IP client with www.tb.com or tb.com, if the haproxy is working properly, and the ACL rules are set correctly, Server_www
Three back-end server Default Web page information will appear in turn, this shows that haproxy to the e-commerce website load Balancing, and no other back-end server Default Web page information, the ACL rules take effect, implement the virtual host function.
Similarly, when a client accesses a Web site with bbs.tb.com through a different IP, the default Web page information for the two Server_bbs backend servers is rotated, enabling the forum to load balance without the default Web page information for the other backend servers
Explains that ACL rules are in effect and implements virtual host functionality.
Similarly verify blog.tb.com.
At the end of the visit to other IP or other accesses, the request will be dispatched to the two back-end real servers specified by server_www.
Testing the failover capabilities of the Haproxy
This assumes that the httpd service of a back-end server 192.168.88.82 The SERVER_WWW group is stopped, and when the site is accessed through a www.tb.com or tb.com domain, the failed node will not be accessed because when the httpd service is stopped, Haproxy through Httpchk
The method immediately detects that the node cannot access the data, thereby masking the ability of the node to provide services externally, thus enabling the failover of the failure. Similar methods can be used to test other node applications
Using the Haproxy Web monitoring platform
Explain later, how to install, operation
Virtual Host-based Haproxy configuration combat