Build a Web cluster using Haproxy Case Overview: Haproxy is currently a popular cluster scheduling tool. In comparison, LVS performance is the best, but the construction of complex, Nginx upstream module support cluster function, but the cluster node health Check function is not strong, performance is not haproxy good. Lab Environment:
Host |
Operating System |
IP Address |
main software |
Haproxy Service Machine |
CentOS 7.3 x86_64 |
192.168.217.128 |
Haproxy-1.5.19.tar.gz |
Nginx server 1 |
CentOS 7.3 x86_64 |
192.168.217.129 |
Nginx-1.12.0.tar.gz |
Nginx server 2 |
CentOS 7.3 x86_64 |
192.168.217.130 |
Nginx-1.12.0.tar.gz |
Client |
Windows 7 |
192.168.217.131 |
IE browser |
To configure the Nginx server:
- Environment required to install the service:
yum install -y pcre-devel zlib-devel gcc gcc-c++ #pcre 支持正则表达式 zlib 网页压缩
- Install Nginx Service:
useradd -M -s /sbin/nologin nginx #创建一个管理Nginx的程序用户tar zxvf nginx-1.12.0.tar.gz -C /opt/ #解压cd /opt/nginx-1.12.0/./configure \ --prefix=/usr/local/nginx \ #指定Nginx安装路径--user=nginx \ #管理用户--group=nginx #管理组
make && make install #编译安装
- Add test home, turn off firewall:
cd /usr/local/nginx/html echo "this is accp web" > test.html #新建的网页 ,也可以直接修改 indexln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #建立软连接 ,方便系统管理nginx
Configuring the Haproxy Server
- Environment required to install the service:
yum install -y pcre-devel gcc gcc-c++
- To install the Haproxy service:
tar zxvf haproxy-1.5.19.tar.gz -C /opt/cd /opt/haproxy-1.5.19/make TARGET=linux26 #使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26make install
- To edit the Haproxy configuration file:
mkdir /etc/haproxy cp examples/haproxy.cfg /etc/haproxy/ #复制配置文件到 etc下cd /etc/haproxy/vim haproxy.cfg
chroot /usr/share/haproxy #删除 改变根目录redispatch #删除 强制将请求发送给已经 down 掉的服务器
listen webcluster 0.0.0.0:80 #监听所有地址的80端口 option httpchk GET /test.html #检查服务器的 test.html 文件 balance roundrobin #负载均衡使用轮询算法 server inst1 192.168.217.129:80 check inter 2000 fall 3 #指向服务器 server inst2 192.168.217.130:80 check inter 2000 fall 3
- Start the Haproxy service:
cp/opt/haproxy-1.5.19/examples/haproxy.init/etc/init.d/haproxy #复制启动脚本chmod +x Haproxychkconfig--add/etc/init.d/haproxy # Add service, you can also set the boot ln-s/usr/local/sbin/haproxy/usr/sbin/haproxy #建立软连接, convenient System Management Service Haproxy start #开启服务
- Haproxy Log Management: (log default output to Syslog, viewing is not very convenient)
vim /etc/haproxy/haproxy.cfg #修改log /dev/log local0 info log /dev/log local0 notice //将这两行配置放到haproxy的global配置项目中,主要是将haproxy的info及notice日志分别记录到不同的日志文件中
service haproxy restart #重启服务touch /etc/rsyslog.d/haproxy.conf
vim /etc/rsyslog.d/haproxy.confif ($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&~ #通告日志位置
systemctl restart rsyslog.service
Test
Open the client to access 192.168.217.128/test.html, refresh the page, and see if the two pages appear in turn.
Build a Web cluster using Haproxy