Haproxy Building a Web cluster

Source: Internet
Author: User
Tags haproxy rsyslog nginx load balancing

Haproxy Case Overview

------------Preface: The landlord built several clusters before, such as Tomcat+nginx load Balancing cluster, LVS load Balancing cluster, Haproxy cluster and three kinds of clusters compared to the LVS performance is the best, but the building complex, we just did this experiment, Also deep experience, Nginx upstream module support cluster, but the cluster node fault check function is not much, performance is not haproxy good, so should say Harpoxy is also very good, in some companies also use this software to build clusters,
Haproxy is currently a popular cluster scheduling tool.
Comparison of Haproxy with LVs and Nginx
LVS performance is the best, but the construction is relatively complex
Nginx Upstream module support cluster function, but the cluster node health Check function is not strong, performance is not haproxy good
This case introduces the use of Haproxy and Nginx to build a set of web clusters
Case study: Pre-knowledge points
HTTP request
Request mode: Get mode, post mode
Return status code: Normal status code is 2XX, 3xx, abnormal status code is 4xx, 5xx
Load balancing Common scheduling algorithm
RR (Round Robin): Polling Schedule
LC (Least Connections): Minimum number of connections
SH (source Hashing): Source-based access scheduling

Case study: Case environment


Comprehensive experiment: Build hproxy Cluster * *

1. Experimental topology

2, the virtual machine's experimental environment

A haproxy server, two Nginx servers, build two different websites (easy to test the cluster), a client online testing.

3. Experimental steps
1) Compile and install Haproxy
Install two support packages first


2) configuration of the Haproxy server
First build the Haproxy configuration file

Copy the sample copies of the configuration files to the/etc/haproxy directory

Modifying the Haproxy.cfg configuration file



3) Create a self-starter script

--a mistake, followed by/etc/init.d//haproxy.

--a mistake, followed by/usr/bin/haproxy.

--a mistake, followed by/etc/init.d/haproxy.
4) Install Nginx and make Web page

Start Nginx Service

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


5) Open Haproxy Service

Be careful to turn on exceptions on the firewall, or turn off firewalls
Iptables-i input-p TCP--dport 80-j ACCEPT

6) Testing the 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.

7) Configure Haproxy Log
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 record the log of info and notice separately into different files.

Then modify the Rsyslog configuration to independently define the Haproxy-related configuration to haproxy.conf and place it under/ETC/RSYSLOG.D, and the Rsyslog will automatically load all the configuration files in this directory when it starts.

Add the following content

The following is where the info and notice logs are placed 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 to instantly view the log

4, in order to achieve the high availability of haproxy, we can configure keepalived, configuration method reference on the blog, the previous blog is configured on LVS, but the difference is that we today in the Haproxy to configure.
The environment on the virtual machine is as shown

Added a haproxy, used to do backup, installed on the above keepalived, and the previous chapter configuration keepalived the same steps, we practice, after the configuration of the first haproxy broken, the site can still access the 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
----------------If you do not understand please leave a comment

Haproxy Building a Web cluster

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.