一、负载均衡工具haproxy完整安装部署流程
1. 关于负载均衡和haproxy
负载均衡是系统设计最常见的一种方式,Nginx、HAProxy、LVS、F5用得比较普遍,不过Nginx只能在HTTP层负载,而HAProxy即可以在7层做负载,也可以在4层做负载,LVS配置有点太麻烦。
HAProxy是免费、极速且可靠的用于为TCP和基于HTTP应用程序提供高可用、负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制的web站点。HAProxy还可以将后端的服务器与网络隔离,起到保护后端服务器的作用。HAProxy的负载均衡能力虽不如LVS,但也是相当不错,而且由于其工作在7层,可以对http请求报文做深入分析,按照自己的需要将报文转发至后端不同的服务器(例如动静分离),这一点工作在4层的LVS无法完成。
2. haproxy的安装过程
haproxy的安装,haproxy官网下载地址:http://www.haproxy.org/download/
cd /opt/download
sudo wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.3.tar.gz
sudo tar zxvf haproxy-1.7.3.tar.gz
sudo mkdir /opt/modules/haproxy
cd haproxy-1.7.3
sudo make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1
sudo make install PREFIX=/opt/modules/haproxy
安装后将安装目录中example里的.cfg文件复制一个过来,比如:transparent_proxy.cfg,进行修改。
#完整配置文件:
globallog 127.0.0.1 local0 #日志输出配置,所有日志都记录在本机,通过local0输出maxconn 4096chroot /opt/modules/haproxy #运行目录daemon #后台运行nbproc 1 #启动1个haproxy实例uid 99 #所属运行的用户uidgid 99 #所属运行的用户组pidfile /opt/modules/haproxy/haproxy.pid #指定PID文件路径#debugdefaultstimeout client 3s #客户端连接超时时间timeout server 3s #服务器端连接超时时间timeout connect 3s #连接超时时间log 127.0.0.1 local3 #日志文件的输出定向mode httpoption httplog #日志类别,采用httplogoption httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现option dontlognull#option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ipoption redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查maxconn 65535 #最大连接数balance roundrobin#stats uri /haproxy-statslisten redis-6382bind 0.0.0.0:6382maxconn 10000mode tcp #所处理的类别,默认采用http模式,可配置成tcp作4层消息转发option tcplogoption tcpkatimeout connect 3stimeout client 60stimeout server 60s#balance roundrobinbalance leastconn #负载均衡策略server redis-83 192.168.90.123:6383 check inter 2000 rise 2 fall 3 weight 100server redis-84 192.168.90.123:6384 check inter 2000 rise 2 fall 3 weight 100server redis-85 192.168.90.123:6385 check inter 2000 rise 2 fall 3 weight 100frontend MyFrontendmode httpbind 0.0.0.0:8080 #前端浏览器中查看统计的WEB界面地址acl is_stats_req url_beg -i /haproxy/stats #haproxy 监控页面的访问地址default_backend TransparentBack_http #前端请求WEB时,后端的处理逻辑,即是下面的backendbackend TransparentBack_httpstats enablestats auth admin:123456 #设置查看统计的账号密码stats admin if { always_true }stats hide-version #隐藏统计页面的HAproxy版本信息stats realm Haproxy\ Statisticsstats uri /haproxy/stats stats refresh 5 #几秒刷新一次
3. haproxy的安装启动及报错处理
然后启动haproxy.及haproxy的重启和停止命令
#启动命令:
/opt/modules/haproxy/sbin/haproxy -f /opt/modules/haproxy/haproxy.cfg
#haproxy的重启命令
/opt/modules/haproxy/sbin/haproxy -f /opt/modules/haproxy/haproxy.cfg -st `cat /opt/modules/haproxy/haproxy.pid`
#停止haproxy服务
killall haproxy
遇到报错,这是因为1.7.3里的面的配置contimeout,clitimeout,srvtimeout已过时,需要使用timeout connect,timeout client,timeout server来替换。还有两项警告是stats和option forwardfor只针对http负载,因为我这里只有tcp负载,所以报警这两项配置无效,将其注释掉即可。
[root@123 haproxy]# [WARNING] 186/171608 (26758) : parsing [/opt/modules/haproxy/haproxy.cfg:32] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.
[WARNING] 186/171608 (26758) : parsing [/opt/modules/haproxy/haproxy.cfg:33] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.
[WARNING] 186/171608 (26758) : parsing [/opt/modules/haproxy/haproxy.cfg:34] : the 'srvtimeout' directive is now deprecated in favor of 'timeout server', and will not be supported in future versions.
[WARNING] 186/171608 (26758) : config : 'stats' statement ignored for proxy 'redis-6382' as it requires HTTP mode.
[WARNING] 186/171608 (26758) : config : 'option forwardfor' ignored for proxy 'redis-6382' as it requires HTTP mode.
[WARNING] 186/171608 (26758) : <debug> mode incompatible with <quiet>, <daemon> and <systemd>. Keeping <debug> only.
haproxy自带web界面,上面的配置文件中有提及,可查看文章:直接在Centos服务器上部署haproxy 以及 使用docker搭建Haproxy负载均衡、代理服务_docker haproxy-CSDN博客
二、 haproxy通过域名匹配进行负载-基于域名负载均衡的Haproxy配置
尝试了一下haproxy基于域名进行负载配置,配置起来也很简单。我这里的尝试是先将nginx下三个域名绑定给三个端口81,82,83,haproxy启动绑定80端口。然后请求时haproxy根据域名将请求分别分发给81,82,83三个nginx+php服务。haproxy主要的配置如下:
frontend wwwbind *:80 #haproxy绑定80端口mode http #使用httpacl domain_shouji hdr_beg(host) -i shouji.com #-i指定匹配域名,如果匹配则分发给后端domain_shoujiacl domain_kermit1 hdr_beg(host) -i kermit1.com #同上acl domain_kermit2 hdr_beg(host) -i kermit2.comuse_backend shouji if domain_shouji #设置每个后端使用哪个服务器集群use_backend kermit1 if domain_kermit1use_backend kermit2 if domain_kermit2backend shouji #后端服务器集群配置mode httpbalance roundrobin #后端负载均衡策略server web01 192.168.90.123:81 check inter 2000 fall 3 #后端服务器列表
backend kermit1mode httpbalance roundrobinserver web01 192.168.90.123:82 check inter 2000 fall 3
backend kermit2mode httpbalance roundrobinserver web01 192.168.90.123:83 check inter 2000 fall 3
上面配置中hdr_beg是haproxy对字符匹配的方法,同类的方法还有以下几个:
path_beg <string> 用于测试请求的URI是否以<string>指定的模式开头。
acl url_static path_beg -i /static /images /javascript /stylesheets
#测试URL是个以/static /images /javascript /stylesheets开头
path_end <string> 用于测试请求的URL是否以<string>指定的模式结尾
acl url_static path_end -i .jpg .gif .png .css .js
#测试URI是否以.jpg .gif .png .css .js结尾
hdr_beg <string> 用于测试请求报文的指定首部的开头部分是否符合<string>指定的模式
acl host_static hdr_beg(host) -i img. video. download. ftp.
#用于测试请求报文首部中的主机是否以img. video. download. ftp.开头
acl monitor hdr_beg(host) -i monitor.test.com
#定义ACL名称,对应的请求的主机头是monitor.test.com
配置结束后,当在前端请求kermt1.com和kermit2.com时,haproxy会分发给82,83两个端口,此时在输出内容上做些区别就很容易进行验证。