Haproxy搭建Web群集

Haproxy概述

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

常见的 Web 集群调度器

目前常见的Web集群调度器分为软件和硬件- 软件通常使用开源的LVS、Haproxy、Nginx - 硬件一般使用比较多的是 F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等

Haproxy 应用分析

LVS 虽然在企业应用中抗负载能力很强,但存在不足
LVS不支持正则处理,不能实现动静分离- 对于大型网站,LVS 的实施配置复杂,维护成本相对较高
Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件- 特别适用于负载特别大的 Web 站点- 运行在当前的硬件上可支持数以万计的并发连接连接请求

Haproxy 调度算法

Haproxy支持多种调度算法,最常用的有三种:
RR(Round Robin)
LC(Least Connections)
SH(Source Hashing)

RR(Round Riobin)

  • RR算法是最简单最常用的一种算法,即轮询调度
    举例
    ◆ 有三个节点 A、B、C
    ◆ 第一个用户访问会被指派到节点A
    ◆ 第二个用户访问会被指派到节点B
    ◆ 第三个用户访问会被指派到C节点
    ◆ 第四个用户访问继续指派到节点A
    轮询分配访问请求实现负载均衡效果

LC(Least Connections)

LC算法即最小连接数算法,根据后端的节点连接数大小动态分配前端请求
举例
◆ 有三个节点A、B、C,各节点的连接数分别为A:4、B:5、C:6
◆ 第一个用户连接请求,会被指派到A上,连接数变为A:5、B:5、C:6
◆ 第二个用户请求会继续分配到A上,连接数变为A6、B:5、C:6;再有新的请求会分配给B,每次将新的请求指派给连接数最小的客户端
◆ 由于实际情况下A、B、C的连接数会动态释放,很难会出现一样连接数的情况
◆ 此算法相比较rr算法有很大改进,是目前用到比较多的一种算法

SH(Source Hashing)

SH即基于来源访问调度算法,此算法用于一些有 Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
举例
◆ 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第次访问被指派到了B
◆ 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
◆ 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用

实验

步骤

调试存储服务器(192.168.100.24)

[root@localhost ~]# rpm -q nfs-utils          
###如果没装,yum -y install nfs-utils 
[root@localhost ~]# rpm -q rpcbind            
###如果没装,yum -y install rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# vi /etc/exports
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo "this is www.51xit.com" >/opt/51xit/index.html
[root@localhost ~]# echo "this is www.52xit.com" >/opt/52xit/index.html

编译安装Nginx服务器1(192.168.100.22)

[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost opt]# ll
total 960
-rw-r--r-- 1 root root 981687 Sep 24 15:58 nginx-1.12.2.tar.gz
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 5 月 16 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
[root@localhost nginx-1.12.2]# nginx                                
####启动nginx
[root@localhost nginx-1.12.2]# yum -y install net-tools
[root@localhost nginx-1.12.2]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      20107/nginx: master
'###添加 Nginx 系统服务###'
[root@localhost nginx-1.12.2]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx      ####描述
After=network.target      ###描述服务类别[Service]
Type=forking          ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid         ##PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx       ##启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID       ##根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID       ##根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.12.2]# systemctl status nginx
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
    Active: inactive (dead)
 [root@localhost nginx-1.12.2]# systemctl start nginx.service
 Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
 [root@localhost nginx-1.12.2]# killall -3 nginx
 [root@localhost nginx-1.12.2]# systemctl start nginx.service
 [root@localhost nginx-1.12.2]# systemctl enable nginx.service
 Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service
 '####刷下命令,服务正常工作了###
 killall -3 nginx                                          ###停止服务
 systemctl start nginx.service
 ###上面命令一刷,下面就可以正常使用了###
 systemctl start nginx.service
 systemctl stop nginx.service
 systemctl reload nginx.service
 systemctl restart nginx.service
 systemctl status nginx.service'
 ###安装httpd 挂载测试页###
 [root@localhost nginx-1.12.2]# yum -y install nfs-utils
 [root@localhost nginx-1.12.2]# systemctl start nfs
 [root@localhost nginx-1.12.2]# systemctl enable nfs
 Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@localhost nginx-1.12.2]# showmount -e 192.168.100.24
 Export list for 192.168.100.24:
 /opt/52xit 192.168.100.0/24
 /opt/51xit 192.168.100.0/24
[root@localhost nginx-1.12.2]# mount 192.168.100.24:/opt/51xit /usr/local/nginx/html/          ###临时挂载
[root@localhost nginx-1.12.2]# vi /etc/fstab                ###永久挂载,需要重启
192.168.100.24:/opt/51xit/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0
[root@localhost nginx-1.12.2]# init 6

192.168.100.22验证

在这里插入图片描述

编译安装Nginx服务器2(192.168.100.23)

与22一样相同,只需要将 51xit 改为 52xit 就可以了

在这里插入图片描述

配置Haproxy 服务器(192.168.100.21)

[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz 
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26
[root@localhost haproxy-1.4.24]# make install
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice    
        #log loghost    local0 info
        maxconn 4096        
        #chroot /usr/share/haproxy        
        uid 99        
        gid 99        
        daemon        
        #debug        
        #quiet 

defaults
        log     global        
        mode    http        
        option  httplog        
        option  dontlognull        
        retries 3        
        #redispatch        
        maxconn 2000        
        contimeout      5000        
        clitimeout      50000        
        srvtimeout      50000 
listen  webcluster 0.0.0.0:80        
        option httpchk GET /index.html        
        balance roundrobin        
        server inst1 192.168.100.22:80 check inter 2000 fall 3        
        server inst2 192.168.100.23:80 check inter 2000 fall 3
###上述配置文件解释###
1、Haproxy配置文件通常分为三个部分
global:为全局配置
defaults:为默认配置
listen:为应用组件配置
global配置参数
log 127.0.0.1 local0:配置日志记录,配置日志记录,local0为日志设备,默认存放到系统日志
log 127.0.0.1 local1 notice:notice为日志级别,通常有24个级别
maxconn 4096:最大连接数
uid 99:用户uid          gid 99:用户gid 
2、defaults配置项配置默认参数,一般会被应用组件继承,如果在应用组件中没有特别声明,将安装默认配置参数设置
log global:定义日志为global配置中的日志定义
mode http:模式为http
option httplog:采用http日志格式记录日志
option dontlognull :保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
retries 3:检查节点服务器失败连续达到三次则认为节点不可用
maxconn 2000:最大连接数
contimeout 5000:连接超时时间
clitimeout 50000:客户端超时时间
srvtimeout 50000:服务器超时时间
 3、listen配置项目一般为配置应用模块参数
 listen  appli4-backup 0.0.0.0:10004:定义一个appli4-backup的应用
 option  httpchk /index.html:检查服务器的index.html文件
 option  persist :强制将请求发送到已经down掉的服务器
 balance roundrobin:负载均衡调度算法使用轮询算法
 server  inst1 192.168.114.56:80 check inter 2000 fall 3:定义在线节点
 server  inst2 192.168.114.56:81 check inter 2000 fall 3 backup:定义备份节点
 ##############
[root@localhost haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chkconfig --add haproxy
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.4.24]# service haproxy start
[root@localhost haproxy-1.4.24]# systemctl stop haproxy.service 
[root@localhost haproxy-1.4.24]# systemctl start haproxy.service

登录:192.168.100.21验证
切换,会发现不同的网站页面,说明已经实现了负载均衡
在这里插入图片描述
在这里插入图片描述

Haproxy日志文件

[root@localhost haproxy-1.4.24]# mkdir -p var/log/haproxy                 ###创建日志文件存放点
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global                        ###下面log开头的 统统去掉换下面的配置
        log /dev/log    local0 info        
        log /dev/log    local1 notice
[root@localhost haproxy-1.4.24]# systemctl restart haproxy.service
[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /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
& ~
[root@localhost haproxy-1.4.24]#  systemctl restart rsyslog.service 
[root@localhost haproxy]# tail -f /var/log/haproxy/haproxy-info.log
Sep 24 17:23:14 localhost haproxy[65722]: 192.168.100.2:62258 [24/Sep/2020:17:22:21.206] 
webcluster webcluster/inst1 0/0/0/1/52926 200 1700 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
Sep 24 17:23:54 localhost haproxy[65722]: 192.168.100.2:62275 [24/Sep/2020:17:23:41.993] 
webcluster webcluster/inst2 0/0/0/0/12236 200 258 - - ---- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
Sep 24 17:24:53 localhost haproxy[65722]: 192.168.100.2:62293 [24/Sep/2020:17:24:03.530] 
webcluster webcluster/inst1 0/0/0/1/50041 200 984 - - cD-- 0/0/0/0/0 0/0 "GET / HTTP/1.1"
[root@localhost haproxy-1.4.24]# cd /var/log/haproxy
[root@localhost haproxy]# ll
total 4-rw------- 1 root root 538 Sep 24 17:24 haproxy-info.log

网页登录:192.168.32.21
然后查看日志文件不断生成情况

五、Haproxy参数优化

随着企业网站负载增加,haproxy参数优化相当重要:

  • maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10240- daemon:守护进程模式,Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动
  • nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍
  • retries:重试次数,主要用于对集群节点的检查,如果节点多,且并发量大,设置为2次或3次
  • option http-server-close:主动关闭http请求选项,建议在生产环境中使用此选项
  • timeout http-keep-alive:长连接超时时间,设置长连接超时时间,可以设置为10s
  • timeout http-request:http请求超时时间,建议将此时间设置为5~10s,增加http连接释放速度
  • timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值