Preface: We KnowTomcat+nginxLoad Balancing cluster,LVsLoad Balancing clusterAndHaproxyto build a cluster, these three clusters are comparedLVsthe performance is the best, but the building is complexNginxof theUpstreamThe module supports clustering, but there is not much fault checking on the nodes of the cluster, and no performanceHaproxyWell, so it should be saidHarpoxyis also very good. So then we started using haproxy to build the cluster.
One, topology diagram:
Build the Environment:
a haproxy server, two nginx servers, build two different websites (easy to test the cluster), a client online testing.
Start building:
Install two support packages first
2) Configuration of the Haproxy server
First Build the haproxy configuration file
Copy the sample copies of the configuration file to under the/etc/haproxy directory
Modify haproxy.cfg configuration file
3) Create a self-starter script
4, install NGINGX and make Web page.
[Email protected] ~]# yum-y install Pcre-devel zlib-devel
[Email protected] ~]# tar zxf nginx-1.6.2.tar.gz
[Email protected] ~]# CD nginx-1.6.2
[Email protected] nginx-1.6.2]#/configure--prefix=/usr/local/nginx--user=nginx--group=nginx && Make & & Make Install
[Email protected] nginx-1.6.2]# useradd-m-s/sbin/nologin nginx
[Email protected] nginx-1.6.2]# ln-s/usr/local/nginx/sbin/*/usr/local/sbin/
[Email protected] nginx-1.6.2]# echo "Node_1" >/usr/local/nginx/html/index.html
start ngiNX service
[Email protected] nginx-1.6.2]# Nginx
Establish firewall rules
[[email protected] nginx-1.6.2]# iptables-i input-p TCP--dport 80-j ACCEPT
As with the rest of the node configuration, it is recommended that the contents of the test page not be consistent in order to see the effect during testing
First set:
Second set:
5) Open haproxy service
Note: Fire nets are turned on with exceptions or shutdowns
Iptables-i input-p TCP--dport 80-j ACCEPT
6) Test your Web cluster
Test Load Balancing First
Open http://192.168.1.10 on the client, the browser displays the first page
Open a new browser again to access, display the second page
Next Test the high availability, we put the first server's network card disconnected, the simulation server failure, if the page is still accessible, the high availability of the cluster is not a problem.
5) Configuring haproxy Logs
The Haproxy log is saved to the syslog in the system by default, which is inconvenient to view, so we can store the logs separately in a production environment in separate files, configured as follows
First modify the configuration file, mainly change the following section
The purpose of these two lines is to the logs for info and notice are recorded in separate files
then modify the Rsyslog configuration to independently define the haproxy related configuration to haproxy.confand put it in / etc /RSYSLOG.D ,rsyslog will automatically load all the configuration files under this directory when it starts.
Add the following content
The following content is the info and notice logs in different files
then restart the Rsyslog service
Test log information
after the client has visited the Web site, you can use tail-f/var/log/haproxy/haproxy-info.log Instant View Log
in order to achieve high availability of haproxy, we can configure keepalived
a Haproxy is added tomake a backup, install keepalivedon it,and then break off the first haproxy , the site can still be accessed right.
Note that the firewall opens the 224.0.0.18 port, which is the keepalived multicast address.
Iptables-i input-p ip-d 224.0.0.18-j ACCEPT
Build a Web cluster using Haproxy