HAproxy是一个免费的负载均衡的软件,可以运行在大部分主流的Linux操作系统上
HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力,具备丰富的功能。HAProxy的社区非常活跃,版本更新快速(最新稳定版1.7.2于2017/01/13推出)。最关键的是,HAProxy具备媲美商用负载均衡器的性能和稳定性。
因为其专注于负载均衡,因此与Nginx相比,在这方面更专业,更好。
参考文献:https://blog.csdn.net/qq_34556414/article/details/107280964
作为建议以单进程模式运行的程序,HAProxy对稳定性的要求是十分严苛的。按照作者的说法,HAProxy在13年间从未出现过一个会导致其崩溃的BUG,HAProxy一旦成功启动,除非操作系统或硬件故障,否则就不会崩溃(我觉得可能多少还是有夸大的成分)。
在上文中提到过,HAProxy的大部分工作都是在操作系统内核完成的,所以HAProxy的稳定性主要依赖于操作系统,作者建议使用2.6或3.x的Linux内核,对sysctls参数进行精细的优化,并且确保主机有足够的内存。这样HAProxy就能够持续满负载稳定运行数年之久。
yum install -y haproxy
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global# to have these messages end up in /var/log/haproxy.log you will# need to:## 1) configure syslog to accept network log events. This is done# by adding the '-r' option to the SYSLOGD_OPTIONS in# /etc/sysconfig/syslog## 2) configure local2 events to go to the /var/log/haproxy.log# file. A line like the following can be added to# /etc/sysconfig/syslog## local2.* /var/log/haproxy.log#log 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaultsmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:5000acl url_static path_beg -i /static /images /javascript /stylesheetsacl url_static path_end -i .jpg .gif .png .css .jsuse_backend static if url_staticdefault_backend app#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend staticbalance roundrobinserver static 127.0.0.1:4331 check#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend appbalance roundrobinserver app1 127.0.0.1:5001 checkserver app2 127.0.0.1:5002 checkserver app3 127.0.0.1:5003 checkserver app4 127.0.0.1:5004 check
haproxy的配置文件由两部分组成:全局设置和对代理的设定,共分为五段:global
,defaults
,frontend
,backend
,listen
。
一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀。
通常主要定义全局配置主要用于设定义全局参数,属于进程级的配置,通常和操作系统配置有关。
globallog 127.0.0.1 local3 #定义haproxy日志输出设置log 127.0.0.1 local1 notice #log loghost local0 info #定义haproxy 日志级别ulimit-n 82000 #设置每个进程的可用的最大文件描述符maxconn 20480 #默认最大连接数chroot /usr/local/haproxy #chroot运行路径uid 99 #运行haproxy 用户 UIDgid 99 #运行haproxy 用户组giddaemon #以后台形式运行harpoxynbproc 1 #设置进程数量pidfile /usr/local/haproxy/run/haproxy.pid #haproxy 进程PID文件#debug #haproxy调试级别,建议只在开启单进程的时候调试#quiet
用于设置配置默认参数,这些参数可以被用到frontend,backend,listen组件。
在此部分中设置的参数值,默认会自动引用到下面的frontend、backend、listen部分中。如果某些参数属于公用的配置,只需要在defaults部分添加一次即可。而如果frontend、backend、listen部分也配置了与defaults部分一样的参数,defaults部分参数对应的值自动被覆盖。
defaultslog global #引入global定义的日志格式mode http #所处理的类别(7层代理http,4层代理tcp)maxconn 50000 #最大连接数option httplog #日志类别为http日志格式option httpclose #每次请求完毕后主动关闭http通道option dontlognull #不记录健康检查日志信息option forwardfor #如果后端服务器需要获得客户端的真实ip,需要配置的参数,可以从http header 中获取客户端的IPretries 3 #3次连接失败就认为服务器不可用,也可以通过后面设置option redispatch
#《---上述选项意思是指serverID 对应的服务器挂掉后,强制定向到其他健康的服务器, 当使用了cookie时,
haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果
后端的服务器宕掉了,但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个
后端server上,以保证服务的正常---》stats refresh 30 #设置统计页面刷新时间间隔option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接balance roundrobin #设置默认负载均衡方式,轮询方式#balance source #设置默认负载均衡方式,类似于nginx的ip_hash #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
是在haproxy 1.3版本以后才引入的一个组件,同时引入的还有backend
组件。通过引入这些组件,在很大程度上简化了haproxy配置文件的复杂性。forntend
可以根据ACL规则直接指定要使用的后端backend
。
frontend http_80_inbind 0.0.0.0:80 #设置监听端口,即haproxy提供的web服务端口,和lvs的vip 类似mode http #http 的7层模式log global #应用全局的日志设置option httplog #启用http的logoption httpclose #每次请求完毕后主动关闭http通道,HAproxy不支持keep-alive模式 option forwardfor #如果后端服务器需要获得客户端的真实IP需要配置此参数,将可以从HttpHeader中获得客户端IPdefault_backend wwwpool #设置请求默认转发的后端服务池
用来定义后端服务集群的配置,真实服务器,一个Backend对应一个或者多个实体服务器。
backend wwwpool #定义wwwpool服务器组。mode http #http的7层模式option redispatchoption abortonclosebalance source #负载均衡的方式,源哈希算法cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义option httpchk GET /test.html #心跳检测server web1 10.1.1.2:80 cookie 2 weight 3 check inter 2000 rise 2 fall 3 maxconn 8
常常用于状态页面监控,以及后端server检查,是Fronted和backend的组合体。
listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称 bind 0.0.0.0:8888 #监听端口 mode http #http的7层模式 log 127.0.0.1 local3 err #错误日志记录 stats refresh 5s #每隔5秒自动刷新监控页面 stats uri /admin?stats #监控页面的url访问路径 stats realm itnihao\ welcome #监控页面的提示信息 stats auth admin:admin #监控页面的用户和密码admin,可以设置多个用户名 stats auth admin1:admin1 #监控页面的用户和密码admin1 stats hide-version #隐藏统计页面上的HAproxy版本信息 stats admin if TRUE #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global# to have these messages end up in /var/log/haproxy.log you will# need to:## 1) configure syslog to accept network log events. This is done# by adding the '-r' option to the SYSLOGD_OPTIONS in# /etc/sysconfig/syslog## 2) configure local2 events to go to the /var/log/haproxy.log# file. A line like the following can be added to# /etc/sysconfig/syslog## local2.* /var/log/haproxy.log#log 127.0.0.1 local2chroot /var/lib/haproxypidfile /var/run/haproxy.pidmaxconn 4000user haproxygroup haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaultsmode httplog globaloption httplogoption dontlognulloption http-server-closeoption forwardfor except 127.0.0.0/8option redispatchretries 3timeout http-request 10stimeout queue 1mtimeout connect 10stimeout client 1mtimeout server 1mtimeout http-keep-alive 10stimeout check 10smaxconn 3000#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend test_frontend # 申明一个frontendbind *:81 # 指定监听的端口mode tcp # 在三层default_backend app # 指定后端服务器
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend staticbalance roundrobinserver static 127.0.0.1:4331 check#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend appbalance roundrobin# server app1 127.0.0.1:5001 check# server app2 127.0.0.1:5002 check# server app3 127.0.0.1:5003 check# server app4 127.0.0.1:5004 checkserver app4 127.0.0.1:80 check # 这里指定了后端的ip:port
此时请求当前服务器的81端口,就能将请求转发到后端80端口