- haproxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。haproxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
结构拓补图
实验环境
主机 | 操作系统 | IP地址 | 主要软件 |
---|---|---|---|
haproxy服务器 | CentOS7.3x86_64 | 192.168.100.101/24 | haproxy-1.5.19.tar.gz |
web服务器1 | CentOS7.3x86_64 | 192.168.100.102/24 | nginx-1.12.0.tar.gz |
web服务器2 | CentOS7.3x86_64 | 192.168.100.103/24 | nginx-1.12.0.tar.gz |
安装配置web服务器
- 两台web服务器的安装配置过程相同,主要对其设置不同测试首页来区分两台web服务器。
安装编译环境包pcre-devel bzip2-devel gcc gcc-c++;
yum install pcre-devdl zlib-devel gcc gcc-c++ make -y
创建nginx用户 ,解压nginx压缩包,并进行编译安装;
useradd -M -s /sbin/nologin nginx ?
mkdir /opt/abc ???#创建挂载点
mount.cifs //192.168.100.3/rhel7 /opt/abc ????#将宿主机的相关软件包挂载
tar zxvf nginx-1.12.0.tar.gz -C /opt ?????????#解压nginx压缩包
cd /nginx-1.12.0
./configure \
--prefix=/usr/local/nginx \ ????#安装目录
--user=nginx \ ?????????????????#用户
--group=nginx ??????????????????#属组make && make install
添加测试首页(分别在两台web服务器中添加测试首页);
cd /user/local/nginx/hatml
echo "this is web1 test" > test.html ?????#web1服务器测试首页内容
echo "this is web2 test" > test.html ?????#web2服务器测试首页内容
建立软连接,使系统识别nginx命令,启动nginx;
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ???#使系统识别nginx命令
nginx ?????#启动
关闭防火墙和安全功能;
systemctl stop firewalld.service ?????#关闭防火墙
setenforce 0 ?????????????????????????#关闭安全功能
安装配置haproxy服务
安装编译环境包pcre-devel bzip2-devel gcc gcc-c++ make;
yum install pdre-devel bzip2-devel gcc gcc-c++ make
解压haproxy压缩包,并进行编译安装;
mkdir /opt/abc ???????#创建挂载点
mount.cifs //192.168.100.3/rhel7 /opt/abc ????????#将宿主机中的相关软件包挂载
tar zxvf haproxy-1.2.19.tar.gz -C /opt ????#解压haproxy服务压缩包
cd /opt/haproxy-1.5.19
make TARGET=linux26 ??????#64为系统
make install
建立haproxy的配置文件,并将进行编辑;
mkdir /etc/harpoxy
cp examples/haproxy.cfg /etc/haproxy ?????#将haproxy.cfg文件复制到配置文件
cd /etc/haproxy
vim haproxy.cfg ??????????#编辑配置文件
删除以下语句chroot /usr/share/haproxyredispatch ?????????????????添加listen ?webcluster 0.0.0.0:80 ???????option httpchk GET /test.html ???????balance roundrobin ???????server inst1 192.168.100.102:80 check inter 2000 fall 3 ????#节点1服务器 ???????server inst2 192.168.100.103:80 check inter 2000 fall 3 ????#节点2服务器
将haproxy的启动脚本复制到/etc/init.d中,建立软连接便于系统所识别;
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy ???#将haproxy的启动脚本复制到/etc/init.d中
chmod +x haproxy
chkconfig --add /etc/init.d/haproxy ??
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy ??????????#建立软连接便于系统所识别
关闭防火墙和安全功能;
systemctl stop firewalld.service
setenforce 0
登录验证
- haproxy服务器做为负载均衡服务器,客服机浏览器只需访问haproxy服务器的IP地址时进行刷新,便可访问到两台web服务器的不同此时首页。
使用haproxy搭建web群集
原文地址:http://blog.51cto.com/13659182/2133606