HAProxy is currently a popular cluster scheduling tool, similar cluster scheduling tool, compared with LVS, LVS performance is the best, but the building is relatively complex, Nginx upstream module support cluster function, but the cluster node health Check function is not strong, performance is not HAProxy good.
System environment
Host |
IP Address |
main software |
HAProxy Server |
192.168.100.200 |
Haproxy-1.5.19.tar.gz |
Nginx server 1 |
192.168.100.201 |
Nginx-1.12.0.tar.gz |
Nginx server 2 |
192.168.100.202 |
Nginx-1.12.0.tar.gz |
Start deploying an Nginx server
First, compile and install the Nginx server
yum install -y pcre-devel zlib-devel gcc gcc-c++ make useradd -M -s /sbin/nologin nginxtar zxvf nginx-1.12.0.tar.gz -C /optcd /opt/nginx-1.12.0/./configure --prefix=/usr/local/nginx --user=nginx --group=nginxmake && 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/html
- Next, set up the test page and start the Nginx service
echo "
For convenience, the website is accessed directly using an IP address. The client accesses the http://192.168.100.201/test.html for testing.
- The process of building Nginx server 2 and the installation steps of Nginx server 1 are the same, the different place is to build the test page.
echo "
Deploying HAProxy Servers
Compiling and installing HAProxy
yum install pcre-devel bzip2-devel gcc gcc-c++ make -ytar zxvf haproxy-1.5.19.tar.gz -C /optcd haproxy-1.5.19/make TARGET=linnux26 ##64位系统make install
HAProxy Server Configuration
mkdir /etc/haproxycp examples/haproxy.cfg /etc/haproxy/ ##复制haproxy.cfg 文件复制到配置文件目录
- Editing a configuration file
vim /etc/haproxy/haproxy.cfgchroot /usr/share/haproxy ##删除两条语句及所有 listen 项目redispatch##添加listen webcluster 0.0.0.0:80 option httpchk GET /test.html balance roundrobin server inst1 192.168.100.201:80 check inter 2000 fall 3 server inst2 192.168.100.202:80 check inter 2000 fall 3
Copy the self-startup script and start the service
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxychmod +x haproxychkconfig --add /etc/init.d/haproxyln -s /usr/local/sbin/haproxy /usr/sbin/haproxyservice haproxy start
- Windows Client test (can see access to HAProxy server address, polling to access two Nginx servers)
HAProxy Log Definition separation
HAProxy logs are output to system syslog by default, and viewing is very inconvenient. For better management of logs, you can log HAProxy info access logs and notice error logs to different log files.
Modifying a configuration file
vim /etc/haproxy/haproxy.cfg##修改为下面的行global log /dev/log local0 info log /dev/log local0 notice
Modify the Rsyslog configuration and restart the Rsylog service
touch /etc/rsyslog.d/haproxy.confvim /etc/rsyslog.d/haproxy.conf##添加下面的脚本if ($programname == ‘haproxy‘ and $syslogseverity-text == ‘info‘)then -/var/log/haproxy/haproxy-info.log&~if ($programname == ‘haproxy‘ and $syslogseverity-text == ‘notice‘)then -/var/log/haproxy/haproxy-notice.log&~
- Access the HAProxy server through the client and view the logs
cd /var/log/haproxy/cat haproxy-info.log
Using HAProxy + Nginx to build a Web cluster