lang110 发表于 2019-1-1 09:38:21

使用 haproxy 搭建 web 群集

使用 haproxy 搭建 web 群集

案例概述:

haproxy是目前比较流行的一种群集调度工具 。相比较而言 ,LVS 性能最好 ,但是搭建复杂 ,Nginx 的 upstream 模块支持群集功能 , 但是对群集节点的健康检查功能不强 ,性能没有 haproxy好。

实验环境:




主机
操作系统
IP地址
主要软件




haproxy 服务机
CentOS 7.3 x86_64
192.168.217.128
haproxy-1.5.19.tar.gz


Nginx 服务器 1
CentOS 7.3 x86_64
192.168.217.129
nginx-1.12.0.tar.gz


Nginx 服务器 2
CentOS 7.3 x86_64
192.168.217.130
nginx-1.12.0.tar.gz


客户机
Windows 7
192.168.217.131
IE浏览器



配置 Nginx 服务器:


[*]
安装服务所需的环境 :

yum install -y pcre-devel zlib-devel gcc gcc-c++   #pcre 支持正则表达式   zlib 网页压缩
[*]
安装 Nginx 服务 :

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                     #编译安装
[*]
添加测试首页,关闭防火墙:

cd /usr/local/nginx/html                                          
echo "this is accp web" > test.html                        #新建的网页 ,也可以直接修改 index
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/       #建立软连接 ,方便系统管理
nginx                                                                     #启动
systemctl stop firewalld.service

配置 Haproxy 服务器


[*]
安装服务所需的环境 :

yum install -y pcre-devel gcc gcc-c++
[*]
安装 Haproxy 服务 :

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,此时该参数就为linux26
make install
[*]
编辑 haproxy 配置文件 :

mkdir /etc/haproxy                                    
cp examples/haproxy.cfg /etc/haproxy/   #复制配置文件到 etc下
cd /etc/haproxy/
vim haproxy.cfg
chroot /usr/share/haproxy       #删除    改变根目录
redispatch                               #删除   强制将请求发送给已经 down 掉的服务器
listenwebcluster 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
[*]
启动 haproxy 服务 :

cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy#复制启动脚本
chmod +x haproxy
chkconfig --add /etc/init.d/haproxy    # 添加服务、也可以设置开机自启
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy   #建立软连接 ,方便系统管理
service haproxy start                         #开启服务
[*]
Haproxy 日志管理 :(日志默认输出到syslog中 ,查看不是很方便)

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   #将haproxy相关的配置独立定义到haproxy.conf
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
&~          #通告日志位置
systemctl restart rsyslog.service
测试


打开客户机访问192.168.217.128/test.html ,刷新页面 ,查看两个网页是否轮流出现 。
http://i2.运维网.com/images/blog/201806/25/b386d16f5804d57d937671e11eec6422.jpghttp://i2.运维网.com/images/blog/201806/25/283b18dd3debf0df4fa1397a3ee5cbba.jpg





页: [1]
查看完整版本: 使用 haproxy 搭建 web 群集