大点干!早点散----------使用Haproxy搭建web群集

时间:2022-07-24
本文章向大家介绍大点干!早点散----------使用Haproxy搭建web群集,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

文章目录

  • 一、常见的Web集群调度器
    • 1、nginx分析
      • 优点:
      • 缺点:
    • 2、lvs分析
      • 优点
      • 缺点
    • 3、haproxy分析
      • 优点
      • 缺点
    • 二、Haproxy调度算法
    • 1、RR(Round Robin)
    • 2、LC(Least Connections)
    • 3、SH(Source Hashing)
  • 二、Haproxy群集搭建
    • 1、环境准备
    • 2、Nginx的安装与启动
    • 3、Haproxy安装与启动
    • 4、Haproxy配置文件详解
    • 5、测试
  • 三、Haproxy日志管理
  • 四、Haproxy可优化的参数详解

一、常见的Web集群调度器

1、nginx分析

优点:

工作在七层,可对http做分流策略 正则表达式比haproxy强大 安装、测试、配置简单,通过日志可以解决大多问题 高并发,并发可达到几万次 nginx还可以作为web使用

缺点:

1、Nginx仅能支持http、https和Email协议,这样就在适用范围上面小些,这个是它的缺点。 2、对后端服务器的健康检查,只支持通过端口来检测,不支持通过url来检测。不支持Session的直接保持,但能通过ip_hash来解决。

2、lvs分析

优点

负载能力强,工作在4层,对内存,cpu消耗低 配置性低,没有太多可配置性,减少认为错误 应用面广,几乎可以为所有应用提供负载均衡

缺点

不支持正则表达式,不能实现动静分离 如果网站架构庞大,lvs-dr模式配置比较繁琐

3、haproxy分析

优点

支持session,cookie共享 可以通过url进行健康检查 效率、负载均衡速度高与nginx,低于lvs 支持tcp,可以对mysql进行负载均衡 调度算法丰富

缺点

正则弱于nginx 日志依赖syslogd,不支持apache日志

二、Haproxy调度算法

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

1、RR(Round Robin)

RR算法是最简单最常用的一种算法,即轮询调度

理解举例

有三个节点A、B、C,第一个用户访问会被指派到节点A,第二个用户访问会被指派到节点B,第三个用户访问会被指派到C节点 第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果

2、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算法有很大改进,是目前用到比较多的一种算法

3、SH(Source Hashing)

SH即基于来源访问调度算法,此算法用于一些有 Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度

理解举例 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第次访问被指派到了B 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用 一句话总结就是,上次你来找的谁 这次还找谁

二、Haproxy群集搭建

1、环境准备

VMware软件 两台centos7虚拟机作为nginx(IP地址:192.168.110.133/IP地址:192.168.100.134) 一台centos7虚拟机作为Haproxy(IP地址:192.168.110.132)

2、Nginx的安装与启动

在两台网站服务器上安装Nginx,并启动服务

[root@nginx1 LNMP-C7]# tar zxvf nginx-1.12.2.tar.gz -C /opt	'//解压NGINX源码包'
[root@nginx1 LNMP-C7]# cd /opt/nginx-1.12.2/
[root@nginx1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx		'//configure 配置'
[root@nginx1 nginx-1.12.2]# make && make install		'//编译安装'
[root@nginx1 nginx-1.12.2]# echo "this is erbao web" > /usr/local/nginx/html/test.html		'//创建站点首页'
[root@nginx1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/		'//创建nginx命令软连接'
[root@nginx1 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@nginx1 nginx-1.12.2]# nginx	'//开启nginx'
[root@nginx1 nginx-1.12.2]# systemctl stop firewalld.service 	'//关闭防火墙'
[root@nginx1 nginx-1.12.2]# setenforce 0
'//第二台节点采用相同方法设置,其中站点首页需要更改,内容为this is sanbao web'

使用源码编译的方式进行安装 关闭 Firewall防火墙 安装基础软件包 增加系统用户账号 nginx 编译安装 Nginx并启动 在两台Nginx上配置测试网站,测试网页的内容应该不同,以便进行测试

3、Haproxy安装与启动

在负载均衡器上安装 Haproxy 安装步骤 安装基础软件包 编译安装 haproxy 要注意操作系统版本,是32位系统还是64位 建立 Haproxy的配置文件 创建配置文件目录/etc/haproxy 将源码包提供的配置文件样例 haproxy.cfg复制到配置文件目录中

[root@haproxy ~]# mount.cifs //192.168.23.1/ccc /mnt
[root@haproxy ~]# cd /mnt/ccc
[root@haproxy ccc]# tar zxvf haproxy-1.5.19.tar.gz -C /opt
[root@haproxy ccc]# cd /opt/haproxy-1.5.19
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install
[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.5.19]# cd /etc/haproxy/
[root@haproxy haproxy]# vim haproxy.cfg 
# this config needs haproxy-1.1.28 or haproxy-1.2.1

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	'//删除原本listen内容,添加一下内容'
        option httpchk GET /test.html	'//监听检查服务器的index.html文件(节点服务器的主页)'
        balance roundrobin	'//负载均衡调度算法使用轮询算法'
        server inst1 192.168.79.134:80 check inter 2000 fall 3	'//定义在线节点'
        server inst1 192.168.79.135:80 check inter 2000 fall 3

[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy	'//复制创建启动脚本'
[root@haproxy haproxy]# chmod +x /etc/init.d/haproxy 	'//添加启动脚本权限'
[root@haproxy haproxy]# chkconfig --add /etc/init.d/haproxy 	'//为service添加启动脚本'
[root@haproxy haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/	'//创建命令软连接'
[root@haproxy haproxy]# service haproxy start	'//开启haproxy'
Starting haproxy (via systemctl):                          [  确定  ]
[root@haproxy haproxy]# netstat -ntap | grep haproxy	'//检查开启状态'
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26964/haproxy    

4、Haproxy配置文件详解

Haproxy配置文件通常分为三个部分

  • global:为全局配置
  • defaults:为默认配置
  • listen:为应用组件配置 global配置参数
  • log127.0.0.1 lcal0:配置日志记录,local0为日志设备,默认存放到系统日志
  • log127.0.0.1 loca1 notice:notice为日志级别,通常有24个级别
  • maxconn4096:最大连接数
  • uid 99:用户uid
  • gid 99:用户gid

defaults配置项配置默认参数,一般会被应用组件继承,如果在应用组件中没有特别声明,将安装默认配置参数设置

  • log global:定义日志为global配置中的日志定义
  • mode http:模式为http
  • option httplog:采用http日志格式记录日志
  • retries 3:检查节点服务器失败连续达到三次则认为节点不可用
  • maxconn2000:最大连接数
  • contimeout5000:连接超时时间
  • clitimeout50000:客户端超时时间
  • srvtimeout50000:服务器超时时间
  • 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:定义备份节点

5、测试

测试Haproxy集群 测试高性能 可在两个不同的浏览器中分别访问两个测试网站,正常情况下应该出现两个网站的测试页面

三、Haproxy日志管理

Haproxy的日志默认是输出到系统的 syslog中,在生产环境中一般单独定义出来

定义的方法步骤

修改 Haproxy配置文件中关于日志配置的选项,加入配置:

log /dev/log local0 info
log /dev/log local0 notice

修改 rsyslog配置,将 Haproxy相关的配置独立定义到 haproxy.conf,并放到/etc/rsyslog.d/下 保存配置文件并重启 rsyslog服务,完成 rsyslog配置

[root@haproxy haproxy]# vim haproxy.cfg 	'//编辑haproxy配置文件'
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0 info	
        log /dev/log    local1 notice
    ...省略内容
[root@haproxy haproxy]# service haproxy restart	'//重启haproxy服务'
Restarting haproxy (via systemctl):                        [  确定  ]
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf	'//创建一个新haproxy配置文件'
[root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf 	'//编写haproxy配置文件脚本'
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@haproxy haproxy]# systemctl restart rsyslog.service 	'//重启日志服务'

访问 Haproxy集群测试网页并测试日志信息

'//未访问网页,查看/var/log'
[root@haproxy dev]# cd /var/log
[root@haproxy log]# ls
发现没有haproxy文件
'//查看网页之后,再次查看/var/log'
[root@haproxy log]# ls        

[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls               已经生成haproxy文件了,可以进去查看
haproxy-info.log
[root@haproxy haproxy]# cat haproxy-info.log 

四、Haproxy可优化的参数详解

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

  • maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10 240
  • 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左右就可以了