Haproxy搭建Web群集

常见的Web群集调度器

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

Haproxy应用分析

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

Haproxy调度算法原理

  • RR(Round Robin)
    • RR算法是最简单最常用的一种算法,即轮询调度
  • LC(Least Connections)
    • 最小连接数算法,根据后端的节点连接数大小动态分配前端请求
  • SH(Source Hashing)
    • 基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做群集调度
#编译安装nginx服务器(两个nginx网站的步骤完全一样,只有测试页面不同)
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc*
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz 
[root@localhost ~]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/
[root@localhost html]# echo "test web01" > test.html
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx		##开启nginx进程
[root@localhost ~]# netstat -anpt | grep nginx


#编译安装Haproxy
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc*
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz 
[root@localhost ~]# cd haproxy-1.5.19/
[root@localhost haproxy-1.5.19]# make TARGET=linux26 & make install
#TARGET=linux26:内核版本

#Haproxy服务器配置
#建立haproxy配置文件
[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy

[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/

#创建服务脚本
[root@localhost haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# chkconfig --add haproxy


#Haproxy配置介绍
[root@localhost haproxy-1.5.19]# vi /etc/haproxy/haproxy.cfg 
global
        log 127.0.0.1   local0     #配置日志记录,local0为日志设备,默认是系统日志
        log 127.0.0.1   local1 notice      #日志级别为notice
        #log loghost    local0 info    
        maxconn 4096      #最大连接数
        uid 99          #用户uid
        gid 99           3用户gid
        daemon        #以守护进程的方式运行
        #debug      #调试模式,输出启动信息到标准输出
        #quiet    #安静模式,启动时无输出

defaults
        log     global           #使用globle中定义的日志
        mode    http           #模式为http
        option  httplog         #采用http的格式记录日志
        option  dontlognull     #保证HAProxy不记录上级负载均衡发送过来的用于检测状态数据的心跳包
        retries 3             #检查节点连接失败的次数,超过3次认为节点不可用
       # redispatch         #当负载很高时,自动结束当前队列处理比较久的连接
        maxconn 2000      #最大连接数
        contimeout      5000         #连接超时时间ms
        clitimeout      50000        #客户端超时时间ms
        srvtimeout      50000             #服务器超时时间ms
listen  webcluster 0.0.0.0:80        #定义群集和监听的端口号
        option  httpchk GET /index.html    #检查服务器的index.html文件,心跳检测URL设置
        balance roundrobin      #负载均衡的调度算法为轮询
                server  inst1 192.168.10.102:80 check inter 2000 fall 3  weight 1
        server  inst2 192.168.10.103:80 check inter 2000 fall 3  weight 2

#启动
[root@localhost haproxy-1.5.19]# /etc/init.d/haproxy start
#测试web群集
[root@localhost ~]# for i in $(seq 10); do curl http://192.168.1.60/test.html  ;done
#Haproxy的日志
#haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件。
#方法一:
[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
        log /dev/log local0 info
        log /dev/log local0 notice

[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]# service rsyslog restart
[root@localhost ~]#/etc/init.d/haproxy restart
[root@localhost ~]# cat /var/log/haproxy/haproxy-info.log


#方法二
#编辑/etc/haproxy/haproxy.conf
[root@localhost ~]# vi /etc/haproxy/haproxy.cfg
global
 log 127.0.0.1 local3     
#local3是设备,对应于 /etc/rsyslog.conf中的配置,默认回收info的日志级别
 
#编写haproxy日志文件
[root@localhost ~]# vim  /etc/rsyslog.d/haproxy.conf
$ModLoad imudp   #采集日志的协议UDP
$UDPServerRun 514    #指定日志采集使用的端口号
local3.*     /var/log/haproxy.log   #指定日志存放位置
 
#配置rsyslog的主配置文件,开启远程日志(可以不配)
[root@localhost ~]# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=”-c 2 -r -m 0#-c 2 使用兼容模式,默认是 -c 5
#-r 开启远程日志
#-m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能

#重启haproxy和rsyslog服务
[root@localhost ~]# systemctl restart rsyslog
[root@localhost ~]# systemctl restart haproxy

#访问网站后查看日志
[root@localhost ~]# cat /var/log/haproxy.log
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值