haproxy负载均衡

haproxy负载均衡

haproxy:基于C语言开发的开源软件
支持高性能的tcp和http负载均衡器,工作中用的版本1.5.9
haproxy功能:主要用于高并发的web站点,工作原理和nginx、lvs都一样

haproxy缺点: 单节点部署,单实例运行。代理服务器出现故障,整个负载集群全部不可用。

正常功能:

1、tcp和http的反向代理
2、https的代理配置
3、可以针对http请求添加cookie,转发到后端服务器(添加缓存)
4、也支持主备切换(keepalive)
5、基于端口的实时监控
6、压缩响应的报文

haproxy的特点

1、可靠性和稳定非常好,可以和硬件F5 BIG 负载均衡的硬件设备
2、可以同时维护4w-5w个并发,单位时间内处理最大的请求20000个
3、支持8中负载均衡算法。但是haproxy不带缓存功能,但是可以支持会话保持
4、也支持配置虚拟主机。

haproxy的负载均衡算法:

1、roundrobin  rr轮询
2、static-rr    wrr加权轮询
3、leastconn    最小连接数
4、source        根据请求的源IP进行调度 sh
5、uri            根据请求地址进行调度
6、url param URL的参数实现调度
7、hdr(name)     根据http的请求头锁定每一次http的请求
8、rdp-cookie(name)根据cookie的名称来锁定每一次请求。

lvs nginx haproxy三种负载均衡的区别

1、Ivs基于linux内核实现负载均衡,性能最高,但是对系统硬件要求也比较高。
        haproxy和nginx基于第三方应用实现负载均衡,性能较低。
2、lvs可以实现 'ip+端口' 的四层负载均衡,无法实现http请求的转发。
        haproxy和nginx都可以实现四层和七层的转发。
3、lvs只能四层转发,单一的功能:基于端口来进行检测
        haproxy 可以实现端口,uri 也可以
4、haproxy虽然功能强大但整体功能低于lvs的性能比gaproxy低
        lvs-------haproxy---------nginx (bug多,稳定也差)
5、nginx主要应用还是web服务或者缓存服务器,nginx的stream模块和upstream也可以支持集群,但是对节点的健康检查能力不行。没有lvs和haproxy的监控性能好

Haproxy:是常用的负载均衡软件
nginx 支持四层转发,七层转发
haproxy 也可以四层和七层转发

基于四层的转发
1、vs
2、nginx
3、haproxy
基于七层:
nginx
haproxy

/etc/haproxy/haproxy.cfg内容说明

global                          #全局配置定义 定义全局参数
log /dev/log localo info        系统日志
log /dev/log -local0	notice	修改日志的存放路径
log loghost localo info		    注释
maxconn 10240					支持每个cpu的最大连接数 1024 一定要改limits.conf
chroot /usr/share/haproxy		注释
nbproc 6 					    在daemon下添加,同时并发进程数,要么和cpu相同,要么是cpu的两倍。
defaults 					    默认配置,包括监听地址和协议backend (upstream)
log global 					    引入全局配置日志格式
mode http 					    模式为http 七层
option dontlognull			    不记录健康检查的日志信息
retries 3 					    检查节点服务器的失败次数 3次失败就认为节点服务器失效
redispatch 					    服务器的负载很高,自动结束当前队列处理比较久的连接
maxconn 2000 					最大连接数,不能超过全局配置的定义数量
timeout http-request 10s		默认http请求的超时时间
timeout queue 1m				默认队列超时时间
timeout connect 10s			    默认连接超时的时间
timeout client 1m				客户端的超时时间
timeout server 1m 			    服务端的超时时间
timeout http-keep-alive 10s	    默认会话保持的超时时间
timeout check 10s				心跳检查的超时时间

nginx-haproxy负载均衡

1、haproxy代理服务器

        192.168.10.10

2、web服务器

        192.168.10.20

        192.168.10.30

        提供 nginx 页面服务

数据流向

haproxy代理服务器(七层)

        192.168.10.10

yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
cd /opt/
tar zxvf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install
mkdir /etc/haproxy
cp /opt/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod 777 /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin

vim /etc/haproxy/haproxy.cfg
.................

修改
log /dev/log    local0 info
log /dev/log    local0 notice
chroot /usr/share/haproxy        注释掉
nbproc 2                         在daemon下添加
contimeout     5000              注释掉
clitimeout     50000             注释掉
srvtimeout     50000             注释掉
添加以下内容
timeout http-request 10s
#默认http请求的超时时间
timeout queue 1m
#默认队列的超时时间
timeout connect 10s
#默认连接的超时时间
timeout client 1m
#客户端默认的连接超时时间
timeout server 1m
#客户端默认的超时时间
timeout http-keep-alive 10s
#默认会话的保持的超时时间
timeout check 10s
#心跳检查的超时时间,在下面添加
listen  liu 0.0.0.0:80
        option httpchk GET /index.html
        balance roundrobin        算法
        server  liu1 192.168.10.20:80 check inter 2000 fall 3
        server  liu2 192.168.10.30:80 check inter 2000 fall 3

把添加完成后面的全部删除
.................

systemctl restart haproxy

haproxy代理服务器(四层)

yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
cd /opt/
tar zxvf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install
mkdir /etc/haproxy
cp /opt/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod 777 /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin

vim /etc/haproxy/haproxy.cfg
.................

修改
log /dev/log    local0 info
log /dev/log    local0 notice
chroot /usr/share/haproxy        注释掉
nbproc 2                         在daemon下添加
contimeout     5000              注释掉
clitimeout     50000             注释掉
srvtimeout     50000             注释掉
添加以下内容
timeout http-request 10s
#默认http请求的超时时间
timeout queue 1m
#默认队列的超时时间
timeout connect 10s
#默认连接的超时时间
timeout client 1m
#客户端默认的连接超时时间
timeout server 1m
#客户端默认的超时时间
timeout http-keep-alive 10s
#默认会话的保持的超时时间
timeout check 10s
#心跳检查的超时时间,在下面添加

frontend liu
bind *:80
mode tcp
default_backend liu

backend liu
mode tcp
balance roundrobin
        server  liu1 192.168.10.20:80 check inter 2000 fall 3
        server  liu2 192.168.10.30:80 check inter 2000 fall 3

把添加完成后面的删除
.................

systemctl restart haproxy

Haproxy的日志重定义

日志单独存放

nginx服务
keepalive_timeout  65;        注释掉或将其改为0



haproxy服务
vim /etc/haproxy/haproxy.cfg
global
	log /dev/log local0 info
	log /dev/log local0 notice

systemctl restart haproxy


vim /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
&~
只能添加ifno和notice其他的要手动添加,格式类似

....................

systemctl restart rsyslog.service

haproxy日志会修改到cd /var/log/haproxy/下,/var/log/messages中将不会显示haproxy的日志

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值