使用Haproxy 搭建web群集

 

 

 

 

1

 

 

实验环境

主机

操作系统

ip地址
负载调度服务器 centos 7 192.168.100.202
Nginx 服务器 1 centos 7 192.168.100.201
Nginx 服务器 2 centos 7 192.168.100.221
客户机 window 7 192.168.100.22

 

Haproxy软件包链接:https://pan.baidu.com/s/1yQw5KGJyb6X46G5Ekixc9g 密码:sfz2

 

实验目的

使用Haproxy成功搭建web群集

 

实验步骤

一、安装编译nginx1服务器

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 nginx test web</h1>" > /usr/local/nginx/html/index.html
8.建立软链接,便于管理nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
9.检查nginx配置
nginx –t
10.启动服务
nginx
11.关闭防火墙及selinux
systemctl stop firewalld.service 
setenforce 0
12.在客户机上访问http://192.168.100.201/进行测试

1

 

二、搭建nginx2服务器

1.编译安装的步骤与nginx 1相同,不同之处在于建立测试页面

echo "<h1>this is nginx test2 web</h1>" > /usr/local/nginx/html/index.html
 

2.在客户端访问http://192.168.100.221/ 进行测试

2

 

三、编译安装haproxy服务器

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

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

 

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

 

4.创建自启动脚本
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
5.为haproxy赋可执行权限
chmod +x /etc/init.d/haproxy
6.加入系统服务
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
7.启动haproxy
service haproxy start
8.关闭防火墙及selinux
systemctl stop firewalld.service 
setenforce 0
五、测试web群集

1.测试高性能

3

 

刷新页面或者关闭网页再重新访问http://192.168.100.202/

4

 

2.测试高可用

现在将192.168.100.201的nginx服务停用,再访问http://192.168.100.202/

3

猜你喜欢

转载自blog.51cto.com/13760351/2165469