Haproxy搭建web群集

一:Haproxy概述

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

二:Haproxy调度算法

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

2.1:RR(Round Robin)

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

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

2.3:SH(Source Hashing)

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

三:使用haproxy搭建web群集

3.1:实验环境介绍

在这里插入图片描述

3.2:实验步骤

3.2.1:节点服务器nginx的配置

  • 安装所需软件包
[root@nginx1 ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y
  • 创建运行用户、组
[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx
  • 解压软件包
[root@nginx1 ~]# cd /opt
[root@nginx1 opt]# rz -E
rz waiting to receive.
[root@nginx1 opt]# ls
nginx-1.12.2.tar.gz  rh
[root@nginx1 opt]# tar zxvf nginx-1.12.2.tar.gz 
  • 编译安装
[root@nginx1 opt]# cd nginx-1.12.2/
[root@nginx1 nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx1 nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@nginx1 nginx-1.12.2]# make && make install
  • 优化执行路径
[root@nginx1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin
  • 添加测试站点
[root@nginx1 nginx-1.12.2]# cd /usr/local/nginx/html/
[root@nginx1 html]# ls
50x.html  index.html
[root@nginx1 html]# echo "this is nginx1 web" > test.html
  • 启动nginx服务
[root@nginx1 html]# systemctl stop firewalld.service 
[root@nginx1 html]# setenforce 0
[root@nginx1 html]# nginx
[root@nginx1 html]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25880/nginx: master 

另一台节点nginx2与nginx1配置一致,测试站点为this is nginx2 web

3.2.2:haproxy配置

  • 安装环境包
[root@haproxy ~]# yum install pcre-devel bzip2-devel gcc gcc-c++ make -y
  • 解压压缩包
[root@haproxy ~]# cd /opt
[root@haproxy opt]# rz -E
rz waiting to receive.
[root@haproxy opt]# ls
haproxy-1.5.19.tar.gz  rh
[root@haproxy opt]# tar zxvf haproxy-1.5.19.tar.gz
  • 编译安装
[root@haproxy opt]# cd haproxy-1.5.19.tar.gz
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install
  • 创建haproxy目录并把压缩包自带的配置文件copy到此目录
[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp /opt/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
  • 修改配置文件
[root@haproxy haproxy-1.5.19]# vim /etc/haproxy/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.209.146:80 check inter 2000 fall 3	'//定义在线节点'
        server inst1 192.168.209.147:80 check inter 2000 fall 3
  • 复制启动脚本给service管理
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.5.19]# chmod +x /etc/init.d/haproxy 
[root@haproxy haproxy-1.5.19]# chkconfig --add /etc/init.d/haproxy 
  • 建立haproxy命令的软链接
[root@haproxy haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
  • 启动haproxy服务
[root@haproxy haproxy-1.5.19]# service haproxy start

3.2.3: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 nginxcluster 0.0.0.0:80:定义一个nginxcluster的应用
    option httpchk GET /index.html检查服务器的index.html文件
    option persist:强制将请求发送到已经down掉的服务器
    balance roundrobin:负载均衡调度算法使用轮询算法
    server inst1 192.168.209.146:80 check inter 2000 fall 3:定义在线节点
    server inst2 192.168 209.147:80 check inter 2000 fall 3 backup:定义备份节点

3.3:实验验证

客户端访问192.168.209.145/test.html
在这里插入图片描述
刷新一下
在这里插入图片描述
出现另一个节点页面

四: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
已经生成haproxy文件了,可以进去查看
[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls
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左右就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值