Haproxy:常用的负载均衡软件,和nginx一样,支持四层转发和七层转发
可以支持一万以上的并发请求,是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理软件
主要用户高并发的web场景,工作原理和nginx、lvs都一样,配置方式有区别
注:LVS的DR和NAT模式都是基于四层转发
TUN模式是基于四层加七层
Haproxy一半都是单节点部署、单实例运行。一旦代理服务器出现故障,整个负载集群全部不可用,所以一般结合keepalived实现代理地址的高可用
Haproxy本身是一个无状态的负载均衡器,没缓存,也没有会话保持,靠应用程序实现会话保持。状态不是保存在代理服务器,而是在后端服务器,或者依靠cookie
Haproxy的日志比较简单,一般只提供基本的请求日志和错误日志。需要更高级的日志便依赖人工自定义
- 实验配置:
- 20.0.0.61:代理服务器
- 20.0.0.62:web服务器1
- 20.0.0.63:web服务器2
- 20.0.0.10:客户端
- [root@nginx1 ~]# cd /opt
- --传入Haproxy软件包--
- [root@nginx1 opt]# tar -xf haproxy-1.5.19.tar.gz
- [root@nginx1 opt]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make #安装依赖环境
- [root@nginx1 opt]# cd haproxy-1.5.19/
- [root@nginx1 haproxy-1.5.19]# make TARGET=LInux2628 ARCH=X86_64 #uname -r查看内核版本,只要内核版本大于2.6,就用2628
- [root@nginx1 haproxy-1.5.19]# make install
- [root@nginx1 haproxy-1.5.19]# mkdir /etc/haproxy
- [root@nginx1 haproxy-1.5.19]# cd examples/
- [root@nginx1 examples]# cp haproxy.cfg /etc/haproxy/
- [root@nginx1 examples]# cd /etc/haproxy/
- [root@nginx1 haproxy]# cp haproxy.cfg haproxy.cfg.bak
- [root@nginx1 haproxy]# vim haproxy.cfg
Haproxy配置文件修改及解析
- global #全局配置,主要用于定义全局参数
- --4~5行--修改,定义haproxy日志输出设置和日志级别,local0为日志设备,默认存放到系统日志
- log /dev/log local0 info
- log /dev/log local0 notice
- #log loghost local0 info
- maxconn 4096 #最大连接数,需考虑ulimit -n限制,推荐使用10240
- --8行--注释,chroot运行路径,为该服务自设置的根目录,一般需将此行注释掉
- #chroot /usr/share/haproxy
- uid 99 #用户UID
- gid 99 #用户GID
- daemon #守护进程模式
- nbproc 4 #添加,设置并发进程数,建议与当前服务器CPU核数相等或为其2倍
-
- defaults #配置默认参数,包括监听地址、协议、backend(upstream)组件
- log global #引入global定义的日志格式
- mode http #模式为http(7层代理http,4层代理tcp)
- option httplog #日志类别为http日志格式
- option dontlognull #不记录健康检查日志信息
- retries 3 #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
- redispatch #当服务器负载很高时,自动结束当前队列处理比较久的连接
- maxconn 2000 #最大连接数,“defaults”中的值不能超过“global”段中的定义
- #contimeout 5000 #设置连接超时时间,默认单位是毫秒
- #clitimeout 50000 #设置客户端超时时间,默认单位是毫秒
- #srvtimeout 50000 #设置服务器超时时间,默认单位是毫秒
- timeout http-request 10s #默认http请求超时时间
- timeout queue 1m #默认队列超时时间
- timeout connect 10s #默认连接超时时间,新版本中替代contimeout,该参数向后兼容
- timeout client 1m #默认客户端超时时间,新版本中替代clitimeout,该参数向后兼容
- timeout server 1m #默认服务器超时时间,新版本中替代srvtimeout,该参数向后兼容
- timeout http-keep-alive 10s #默认持久连接超时时间
- timeout check 10s #设置心跳检查超时时间
-
-
- --删除下面所有listen项--,添加
- listen test 0.0.0.0:80 #haproxy实例状态监控部分配置,定义一个名为webcluster的应用
- option httpchk GET /index.html #检查服务器的test.html文件
- balance roundrobin #负载均衡调度算法使用轮询算法roundrobin
- server inst1 20.0.0.62:80 check inter 2000 fall 3 weight 3
- server inst2 20.0.0.63:80 check inter 2000 fall 3 weight 4 #设置权重
- #开启对后端服务器的健康检查,检查的时间间隔:2000ms
- #fall 3:表示连续三次检测不到后端服务器的心跳线,则认为该节点失效
-
- 添加haproxy系统服务
- [root@nginx1 haproxy]# cd /opt/haproxy-1.5.19/examples/
- [root@nginx1 examples]# cp haproxy.init /etc/init.d/haproxy
- [root@nginx1 examples]# chmod 777 /etc/init.d/haproxy
- [root@nginx1 examples]# chkconfig --add /etc/init.d/haproxy
- [root@nginx1 examples]# ln -s /usr/local/sbin/haproxy /usr/sbin/
- [root@nginx1 examples]# systemctl restart haproxy.service

- [root@nginx2 ~]# vim /usr/local/nginx/html/index.html
- --添加--
- this is test1
-
- [root@nginx3 ~]# vim /usr/local/nginx/html/index.html
- --添加--
- this is test2

- [root@nginx1 haproxy]# vim haproxy.cfg
-
- #listen test 0.0.0.0:80
- # option httpchk GET /index.html
- # balance roundrobin
- # server inst1 20.0.0.62:80 check inter 2000 fall 3
- # server inst2 20.0.0.63:80 check inter 2000 fall 3
-
- frontend test
- bind *:80
- mode tcp
- default_backend test
-
- backend test
- mode tcp
- balance static-rr
- server server1 20.0.0.62:80 check inter 2000 fall 3 weight 3
- server server2 20.0.0.63:80 check inter 2000 fall 3 weight 4
-
- [root@nginx1 haproxy]# systemctl restart haproxy.service


- [root@nginx2 ~]# systemctl stop nginx
- 关闭web服务器1地nginx服务

- [root@nginx2 ~]# systemctl restart nginx
- 重启web服务器1的nginx服务

默认haproxy的日志是输出到系统的syslog中,查看起来不是非常方便,为了更好的管理haproxy的日志,我们在生产环境中一般单独定义出来。需要将haproxy的info及notice日志分别记录到不同的日志文件中
- [root@nginx1 haproxy]# vim /etc/haproxy/haproxy.cfg
-
- global
- log /dev/log local0 info
- log /dev/log local0 notice
-
- service haproxy restart
-
- #需要修改rsyslog配置,为了便于管理。将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件
-
- [root@nginx1 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
- &~
-
- -------------------------------------------------------------------------------------------
- 将haproxy的info 日志记录到/var/log/haproxy/haproxy-info.log下
- 将haproxy的notice日志记录到/var/log/haproxy/haproxy-notice.log下
- “&~”表示当日志写入到日志文件后,rsyslog停止处理这个信息
- ----------------------------------------------------------------------------------------
- [root@nginx1 haproxy]# systemctl restart rsyslog.service
- [root@nginx1 haproxy]# systemctl restart haproxy.service
- [root@nginx1 haproxy]# tail -f /var/log/haproxy/haproxy-info.log
- #查看haproxy的访问请求日志信息

