Haproxy搭建高可用Web集群

本文详细介绍了使用HAProxy和Nginx搭建高可用Web集群的过程,包括环境配置、软件安装、服务启动及日志功能的添加。通过在CentOS 7上部署负载均衡器HAProxy和Web服务器Nginx,实现了一个稳定且可扩展的Web集群解决方案。

拓扑架构图

Haproxy搭建高可用Web集群

系统环境:

主机操作系统IP地址软件包
负载调度服务器:CentOS 7.0 x86_64192.168.100.25haproxy-1.5.19.tar.gz
web1节点服务器:CentOS 7.0 x86_64192.168.100.26nginx-1.12.0.tar.gz
web2节点服务器:CentOS 7.0 x86_64192.168.100.27nginx-1.12.0.tar.gz
Win 7 客户端:Windows 7192.168.100.30

开始部署

一、web1节点服务器

1.安装环境包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
2.新建管理用户nginx
useradd -M -s /sbin/nologin nginx
3.加压缩nginx软件包
tar zxvf nginx-1.12.0.tar.gz -C /opt/
4.切换到nginx目录
cd /opt/nginx-1.12.0/
5.配置

./configure \
--prefix=/usr/local/nginx \ #安装目录
--user=nginx \ #用户
--group=nginx #用户组

6.编译及安装
make && make install
7.创建web站点首页 index.html
echo "<h1>this is nginx2 web</h1>" > /usr/local/nginx/html/index.html
8.建立软链接,便于管理nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
9.检查nginx配置
nginx -t

Haproxy搭建高可用Web集群

10.启动服务
nginx
关闭防火墙及selinux
systemctl stop firewalld.service 
setenforce 0
测试

Haproxy搭建高可用Web集群

二、web2节点服务器

1.安装环境包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make 
2.新建用户nginx
useradd -M -s /sbin/nologin nginx
3.加压缩nginx软件包
tar zxvf nginx-1.12.0.tar.gz -C /opt/
4.切换到nginx目录
cd /opt/nginx-1.12.0/
5.配置

./configure \
--prefix=/usr/local/nginx \ #安装目录
--user=nginx \ #用户
--group=nginx #用户组

6.编译及安装
make && make install
7.创建web站点首页 index.html
echo "<h1>this is nginx1 web</h1>" > /usr/local/nginx/html/index.html
8.建立软链接,便于管理nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
9.检查nginx配置
nginx -t

Haproxy搭建高可用Web集群

10.启动服务
nginx
关闭防火墙及selinux
systemctl stop firewalld.service 
setenforce 0
测试

Haproxy搭建高可用Web集群

三、haproxy服务器

1.安装环境包
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
2.解压haproxy
tar zxvf /linux-tools/haproxy-1.5.19.tar.gz -C /opt/
3.切换到haproxy目录
cd /opt/haproxy-1.5.19/
4.编译及安装
make TARGET=linux26
make install
5.创建haproxy配置文件目录
mkdir /etc/haproxy
6.复制模板配置文件至haproxy目录下
cp examples/haproxy.cfg /etc/haproxy/     
7.编辑haproxy.cfg配置文件
vim /etc/haproxy/haproxy.cfg

#1.删除以下2行语句
chroot /usr/share/haproxy
redispatch

#2.删除所有listen项目
#3.新添加以下数据
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.100.26:80 check inter 2000 fall 3
server inst2 192.168.100.27:80 check inter 2000 fall 3

8.创建启动脚本haproxy
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
9.为haproxy赋可执行权限
chmod +x /etc/init.d/haproxy
10.加入系统服务
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
11.启动haproxy
service haproxy start
12.关闭防火墙及selinux
systemctl stop firewalld.service 
setenforce 0
13.测试:

win7客户端:http://192.168.100.25/
Haproxy搭建高可用Web集群

Haproxy搭建高可用Web集群

四、添加日志功能

说明:haproxy默认是不会直接输出文件日志,所以我们要借助Linux的rsyslog来让haproxy输出日志

1.编辑haproxy.cfg
vim /etc/haproxy/haproxy.cfg

#将信息提示与告警进行分离
log /dev/log local0 info
log /dev/log local0 notice

2.重启haproxy
service haproxy restart
3.新建配置格式
vim /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
&~

4.重启系统日志服务
systemctl restart rsyslog.service
5.客户机进行访问后,到调度服务器的日志目录下查看记录
cat /var/log/haproxy/haproxy-info.log

至此,Haproxy搭建高可用Web集群已经完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值