haproxy+nginx负载均衡群集及调度日志管理

关于负载均衡群集,在本文之前已经发表有关负载均衡群集的文章,如:Nginx+Tomcat负载均衡群集LVS-NAT模式的负载均衡群集LVS-DR+Keepalive高可用群集;

  • 就性能而言,Nginx的upstream模块支持群集功能,但是对群集节点的健康检查功能不强。
  • LVS的两种模式性能较好,由于搭建过程较为复杂,后期维护也较为繁琐。
  • haproxy是一种轻量级调度服务软件,HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。且搭建配置较为简单。

常用负载均衡调度算法:

  • RR(Round Robin)较为简单的一种轮询算法,即轮询调度;在轮询调度中还有一种加权轮询,即根据每个节点所分配权重轮询分配访问请求。
  • LC(Least Connections)即最小连接数算法,这种算法会根据节点服务器已存在链接服务数比较,会将访问请求首先分配到链接数较少的节点服务器上,由于实际情况节点服务器的连接数会动态释放,很难会出现链接数一样的情况,因此LC算法较RR算法有较大改进。
  • SH(Source Hashing)基于来源访问的调度算法,此算法用于一些有session会话记录在服务器端的场景,可以基于来源的IP地址Cookie等做群集调度。

实验模型

haproxy+nginx负载均衡群集及调度日志管理

实验环境

服务器 系统环境 IP地址 所需软件
haproxy服务器 centos7.3 192.168.100.101 haproxy
节点web1 centos7.3 192.168.100.201 Nginx
节点web2 centos7.3 192.168.100.202 Nginx

实验部署

1、haproxy服务器

  • 这里服务器都是托管在IDC机房中,若是公网访问需要利用防火墙NAT规则制作映射访问内网服务器IP。

  • 编译安装haproxy调度软件,使得可以调度节点服务器。

yum install pcre pcre-devel bzip2-devel gcc gcc-c++ make -y //安装编译所需环境包

tar zxvf haproxy-1.5.19.tar.gz -C /opt/ //解压haproxy软件包
cd /opt/haproxy-1.5.19/
make TARGET=linux26 //编译64位版本
make install //编译安装,默认路径为/etc下
mkdir /etc/haproxy //创建haproxy工作目录
cp /opt/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/ //创建haproxy的配置文件

  • 编辑haproxy主配置文件:

cd /etc/haproxy/
vim 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   //当前工作路径,因为直接make install默认在/etc/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   //定义haproxy监听页面
        option httpchk GET /test.html   //调度网页形式
        balance roundrobin   //采用轮询调度算法
        server inst1 192.168.100.201:80 check inter 2000 fall 3
        server inst2 192.168.100.202:80 check inter 2000 fall 3

(···删除其他listen区域)
  • 复制服务启动脚本

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

2、节点服务器

  • 解压安装Nginx服务

tar zxvf nginx-1.12 -C /opt
cd /opt/nginx-1.12

  • 创建进程用户

useradd -M -s /sbin/nologin nginx

扫描二维码关注公众号,回复: 1775409 查看本文章
  • 配置安装

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

  • 编译安装

    make && make install

    • 创建测试首页

此处创建首页格式为test.html,因为在haproxy配置文件中有注明调度网页形式。为区分调度节点服务器,测试首页需要不同。

cd /usr/local/nginx/html
echo "this is first test web" > test.html

  • 建立Nginx启动命令

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

  • 启动服务

    systemctl stop firewalld.service
    setenforce 0
    nginx -t 检查配置文件
    nginx 启动服务

对于节点服务器二,配置方式同节点服务器一,仅需要在测试页面区分。

3、调度测试

haproxy+nginx负载均衡群集及调度日志管理

haproxy+nginx负载均衡群集及调度日志管理

4、调度日志管理

由于haproxy调度软件的日志文件默认输出到和系统日志syslog中,而在生产环境中,这样的结果容易导致日志混乱且不便管理查询,因此需要将haproxy日志文件单独定义出来。

  • 修改haproxy配置文件

vim /etc/haproxy/haproxy.cfg

global
        log /dev/log    local0 info      //调度日志为info类型及存放位置
        log /dev/log    local0 notice    //调度日志为notice类型及存放位置
  • 编写系统日志管理haproxy日志的配置文件,方便系统日志进行管理

touch /etc/rsyslog.d/haproxy.conf

vim haproxy.conf

if ($programname == 'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log    //定义新的haproxy日志文件位置
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
  • 重启服务

    service haproxy start
    systemctl restart rsyslog.service

  • 日志访问测试
    当使用客户端再次访问时,可以观察到/var/log/haproxy/下已经生成相应日志文件。

haproxy+nginx负载均衡群集及调度日志管理

猜你喜欢

转载自blog.51cto.com/13659253/2133545