Haproxy搭建web群集

一、常见的Web群集调度器:

1、分为软件和硬件。
2、软件通常使用开源的LVS(4层)、Haproxy(4层)、Nginx(4、7层一身)。
3、硬件一般使用比较多的是F5,也有很多人使用国内一些产品,如梭子鱼、绿盟。
4、四层(tcp、ip、port、协议)、七层调度(域名DNS、http)。

二、Haproxy应用分析:

1、LVS在企业应用中抗负载能力很强,但也存在不足:

LVS不支持正则处理,不能实现动静分离。
对于大型网站,LVS的实施配置负载,维护成本相对较高。

2、Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理软件。

适用于负载大的web站点。
运行在硬件上可支持数以万记的并发连接的连接请求。

三、Haproxy调度算法:

Haproxy支持多种调度算法,常用的有三种:
1、RR:是最简单最常用的一种算法,即:轮询调度
2、LC:最小连接数算法,根据后端的节点连接数大小动态分配前端请求。
3、SH:基于来源访问调度算法,用于一些有Session会话在服务器端的场景,可以基于来源的ip、cookie等做群集调度。(原来找谁。下次还找谁)

四、Haproxy配置文件详解:

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
defaults配置项配置默认参数,一般会被应用组件继承,如果应用组件中没有特别声明,将按默认配置参数设置。

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

五、Haproxy日志管理

默认是输出到系统的syslog中,生产环境中一般单独定义
定义的方法步骤
修改Haproxy配置文件中关于日志配置的选项,加入配置
log /dev/log local0 info
log /dev/log local0 notice
修改rsyslog配置,将Haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d下
保存配置文件并重启rsyslog服务,完成rsyslog配置
访问Haproxy集群测试网页并查看日志信息

六、Haproxy参数优化

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

七、模拟实验

节点服务器web1和web2配置
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostname web1
[root@localhost ~]# su
[root@web1 ~]# yum install gcc-c++ pcre-devel zlib-devel make -y
[root@web1 ~]# rz -E    拖软件包
rz waiting to receive.
[root@web1 ~]# useradd -M -s /sbin/nologin nginx
[root@web1 ~]# tar zxvf nginx-1.12.0.tar.gz 
[root@web1 ~]# cd nginx-1.12.0/
[root@web1 nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx
[root@web1 nginx-1.12.0]# make && make install
[root@web1 nginx-1.12.0]# cd /usr/local/nginx/
[root@web1 nginx]# cd html/
[root@web1 html]# vim test.html
<h1>this is kgc web</h1>
[root@web1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@web1 html]# nginx    启动nginx
在网站上测试下nginx是否成功


haproxy服务器配置
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostname haproxy
[root@localhost ~]# su
[root@haproxy ~]# yum install pcre-devel bzip2-devel gcc gcc-c++ make -y
[root@haproxy ~]# rz -E
rz waiting to receive.
[root@haproxy ~]# tar zxvf haproxy-1.5.19.tar.gz 
[root@haproxy ~]# cd 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 
#       chroot /usr/share/haproxy
#       redispatch
把这两句话注释掉
 set nu   26行以下的全部删除

添加
listen webcluster 0.0.0.0:80
         option httpchk GET /test.html
         balance roundrobin
         server inst1 192.168.169.100:80 check inter 2000 fall 3     
         server inst2 192.168.169.200:80 check inter 2000 fall 3
[root@haproxy haproxy]# cd ~/haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.5.19]# cd /etc/init.d/
[root@haproxy init.d]# chmod +x haproxy 
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy   名称自动添加
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy  /usr/sbin/
[root@haproxy init.d]# service haproxy start
在网站上测试输入192.168.169.88/test.html  就可以访问kgc和benet了


日志定义
[root@haproxy haproxy]# vim /etc/haproxy/haproxy.cfg 
        log /dev/log    local0 info
        log /dev/log    local0 notice
[root@haproxy haproxy]# service haproxy restart 
[root@haproxy haproxy]# cd /etc/rsyslog.d/
[root@haproxy rsyslog.d]# vim 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@haproxy rsyslog.d]# systemctl restart rsyslog.service
[root@haproxy rsyslog.d]# service haproxy restart
[root@haproxy rsyslog.d]# ls /var/log/   现在查看里面就有了haproxy这个文件了
[root@haproxy haproxy]# ls /var/log/haproxy/
haproxy-info.log  haproxy-notice.log

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值