Haproxy+nginx implementing a Web Load Balancing cluster

Source: Internet
Author: User
Tags nginx server haproxy rsyslog

Haproxy is currently a popular cluster scheduling tool, similar cluster scheduling tool has many, such as LVS and Nginx, compared to the best performance of LVS, but the construction of relatively complex, Nginx upstream module support cluster function, but the cluster node health Check function is not strong, Performance is not hapr oxy good. The official website of Haproxy is http://haproxy.1wt.eu/.
This case describes the use of Haproxy and Nginx to build a set of Web clusters.

First, the case environment

Using three server simulations to build a Web cluster, the specific topology is as follows:

Host system IP Address main software
Haproxy Server CentOS7.4 x86_64 172.16.10.30 Haproxy-1.5.19.tar.gz
Nginx server 1 CentOS7.4 x86_64 172.16.10.10 Nginx-1.12.0.tar.gz
Nginx server 2 CentOS7.4 x86_64 172.16.10.20 Nginx-1.12.0.tar.gz
Client Windows7 172.16.10.8 IE browser
Two. Compile and install the Nginx server 1. Build Nginx1

Compiling and installing using the NGINX-1.12.0.TAR.GZ installation package

[[email protected] ~]# yum install gcc gcc-c++ pcre-devel zlib-devel make-y[[email protected] ~]# useradd -M -s /sbin/nologin nginx                #创建运行用户[[email protected] ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/[[email protected] ~]# cd /opt/nginx-1.12.0/[[email protected] nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx         #配置相关参数[[email protected] nginx-1.12.0]# make && make install   #安装

The default information after installation is as follows

    • Default installation directory:/usr/local/nginx
    • Default log:/usr/local/nginx/logs
    • Default Listening port: 80
    • Default Web directory:/usr/local/nginx

Next, set up the test page and start the Nginx service.

[[email protected] ~]# cd /usr/local/nginx/html[[email protected] html]# echo "Server 172.16.10.10" > test.html[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [[email protected] ~]# nginx             #启动[[email protected] ~]# systemctl stop firewalld.service    #关闭防火墙[[email protected] ~]# setenforce 0

On the client Access http://172.16.10.10/test.html, the Web page is displayed properly.

2. Build Nginx2

The steps to compile the installation are the same as Nginx1, except that the test page is built.

[[email protected] ~]# cd /usr/local/nginx/html[[email protected] html]# echo "Server 172.16.10.20" > test.html
Third, compile and install Haproxy

Compiling and installing using the HAPROXY-1.5.19.TAR.GZ installation package

[[email protected] ~]# yum install gcc gcc-c++ pcre-devel bzip2-devel make -y #用yum安装一系列d的环境支持[[email protected] ~]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/ #解压haproxy软件包至/opt目录下[[email protected] ~]# cd /opt/haproxy-1.5.19/[[email protected] haproxy-1.5.19]# make TARGET=linux26  #64位系统[[email protected] haproxy-1.5.19]# make install   #安装
Four. Create a haproxy configuration file

Haproxy does not create a configuration file by default, it needs to copy the template profile of the package and edit it

[[email protected] haproxy-1.5.19]# mkdir /etc/haproxy    #创建配置文件目录[[email protected] haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/  #将haproxy.cfg文件复制到配置文件目录下 [[email protected] haproxy-1.5.19]# cd /etc/haproxy/[[email protected] haproxy]# vim haproxy.cfg
#删除以下语句chroot /usr/share/haproxy  #禁锢到haproxy的根目录下redispatch   #强制将请求发送到已经down掉的机器#添加listen  webcluster 0.0.0.0:80             #定义一个webcluster的应用        option httpchk GET /test.html     #访问服务器的test.html文件        balance roundrobin        server inst1 172.16.10.10:80 check inter 2000 fall 3    #定义nginx节点服务器        server inst2 172.16.10.20:80 check inter 2000 fall 3
Five. Create a haproxy self-starter script
[[email protected] haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy    [[email protected] haproxy]# chmod +x haproxy[[email protected] haproxy]# chkconfig --add /etc/init.d/haproxy  #添加系统服务[[email protected] haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy[[email protected] haproxy]# service haproxy start      #启动Haproxy服务[[email protected] haproxy]# systemctl stop firewalld.service  #关闭防火墙
VI. Testing the Web cluster

With the above steps, you have completed the Haproxy Web cluster and then verify that the cluster is working correctly. A cluster typically requires two features, the first being high performance, and the second being highly available.

1. Test High Performance

When the client uses the browser to open http://172.16.10.30/test.html, the browser displays information:

Refresh and display the message:

You can see that the cluster's load-balancing schedule takes effect to meet the high-performance requirements of the cluster.

2. Test High Availability

Deactivate the 172.16.10.10 Nginx Server and open Http://172.16.10.30/test in the client using the browser. HTML, the browser displays information:

As you can see, when a node fails, it does not affect the use of the cluster, which satisfies the high availability of the cluster.

Seven, Haproxy's diary

In syslog of the Haproxy log default output system, viewing is inconvenient and can be defined separately in a production environment.

1. Modify the options for log configuration in the Haproxy configuration file (/etc/haproxy/haproxy.cfg), and add the following configuration:
log /dev/log    local0 infolog /dev/log    local0 notice

These two lines of configuration are placed in the global configuration item, which is mainly to record the info and notice logs into separate log files.

2. Then restart the Haproxy service
[[email protected] ~]# service haproxy restart      #重新启动Haproxy服务
3. Modify the Rsyslog configuration

For ease of administration, the Haproxy configuration is independently defined to haproxy.conf and placed under/etc/rsyslog.d/, and all configuration files under this directory are automatically loaded at rsyslog startup.

[[email protected] ~]# touch /etc/rsyslog.d/haproxy.conf[[email protected] ~]# vim /etc/rsyslog.d/haproxy.conf

Add the following content:

if ($programname == ‘haproxy‘ and $syslogseverity-text == ‘info‘)then -/var/log/haproxy/haproxy-info.log        #info日志记录到/var/log/haproxy/haproxy-info.log下&~      #表示日志写入到日志文件后,rsyslog停止处理这个信息if ($programname == ‘haproxy‘ and $syslogseverity-text == ‘notice‘)then -/var/log/haproxy/haproxy-notice.log       #notice日志记录到/var/log/haproxy/haproxy-notice.log下&~
4. Save the configuration file and restart the Rsyslog service
[[email protected] ~]# systemctl restart rsyslog.service
5. Test log information

After the client accesses http://172.16.10.30/test.html, you can use tail-f/var/log/haproxy/haproxy-info.log to instantly view the Haproxy access request log.

[[email protected] ~]# tail -f /var/log/haproxy/haproxy-info.log Jun 30 21:20:48 localhost haproxy[4202]: 172.16.10.8:50352 [30/Jun/2018:21:20:48.466] webcluster webcluster/inst1 1/0/0/1/2 200 279 - - ---- 1/1/0/1/0 0/0 "GET /test.html HTTP/1.1"Jun 30 21:21:58 localhost haproxy[4202]: 172.16.10.8:50353 [30/Jun/2018:21:21:58.114] webcluster webcluster/inst2 0/0/4/1/5 200 249 - - ---- 1/1/0/1/0 0/0 "GET /test.html HTTP/1.1"

Haproxy+nginx implementing a Web Load Balancing 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.