一、Haproxy介绍
1、实现了一种事件驱动,单一进程模型,支持数万计的并发连接,用于为tcp和http应用程序提供高可用,负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制的web站点
2、代理(http)作用: web缓存(加速)、反向代理、内容路由(根据流量及内容类型等将请求转发至特定服务器)、转码器(将后端服务器的内容压缩后传输给client端)-在代理服务器上添加Via首部;
- 正向代理:
- 反向代理:
3、缓存的作用:减少冗余内容传输,节省带宽、缓解网络瓶颈,降低了对原始服务器的请求压力,降低了传输延迟
4、负载均衡作用:支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机,特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理
5、负载均衡对比
LVS: 1、抗负载能力强。抗负载能力强、性能高,能达到F5硬件的60%;对内存和cpu资源消耗比较低 2、工作在网络4层,通过vrrp协议转发(仅作分发之用),具体的流量由linux内核处理,因此没有流量的产生。 2、稳定性、可靠性好,自身有完美的热备方案;(如:LVS+Keepalived) 3、应用范围比较广,可以对所有应用做负载均衡; 4、不支持正则处理,不能做动静分离。 5、支持负载均衡算法:rr(轮循)、wrr(带权轮循)、lc(最小连接)、wlc(权重最小连接) 6、配置 复杂,对网络依赖比较大,稳定性很高。 Ngnix: 1、工作在网络的7层之上,可以针对http应用做一些分流的策略,比如针对域名、目录结构; 2、Nginx对网络的依赖比较小,理论上能ping通就就能进行负载功能; 3、Nginx安装和配置比较简单,测试起来比较方便; 4、也可以承担高的负载压力且稳定,一般能支撑超过1万次的并发; 5、对后端服务器的健康检查,只支持通过端口来检测,不支持通过url来检测。 6、Nginx对请求的异步处理可以帮助节点服务器减轻负载; 7、Nginx仅能支持http、https和Email协议,这样就在适用范围较小。 8、不支持Session的直接保持,但能通过ip_hash来解决。、对Big request header的支持不是很好, 9、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、Ip-hash(Ip哈希) 10、Nginx还能做Web服务器即Cache功能。 HAProxy的特点是: 1、支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机; 2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作 3、支持url检测后端的服务器出问题的检测会有很好的帮助。 4、更多的负载均衡策略比如:动态加权轮循(Dynamic Round Robin),加权源地址哈希(Weighted Source Hash),加权URL哈希和加权参数哈希(Weighted Parameter Hash)已经实现 5、单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度。 6、HAProxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。 9、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie) 10、不能做Web服务器即Cache。
二、Haproxy安装
1、简单的架构图
2、环境(说明):
HAProxy:192.168.1.157
web1:192.168.1.110
web2:192.168.1.184
3、安装步骤
(1)关闭Selinux和防火墙
(2)yum -y install haproxy
(3)主配置文件/etc/haproxy/haproxy.cfg---以下是负载的配置
#--------------------------------------------------------------------- # Global settings #--------------------------------------------------------------------- global log 127.0.0.1 local2 ###[err warning info debug] chroot /usr/local/haproxy pidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 maxconn 4000 ###最大连接数,默认4000 user haproxy group haproxy daemon ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon" #--------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #--------------------------------------------------------------------- defaults mode http ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK log global ###采用全局定义的日志 option dontlognull ###不记录健康检查的日志信息 option httpclose ###每次请求完毕后主动关闭http通道 option httplog ###日志类别http日志格式 option forwardfor ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip option redispatch ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器 timeout connect 10000 #default 10 second timeout if a backend is not found timeout client 300000 ###客户端连接超时 timeout server 300000 ###服务器连接超时 maxconn 60000 ###最大连接数 retries 3 ###3次连接失败就认为服务不可用,也可以通过后面设置 ####################################################################
#启用haproxy的状态监控页配置如下 listen stats bind 0.0.0.0:1080 #监听端口 stats refresh 30s #统计页面自动刷新时间 stats uri /stats #统计页面url stats realm Haproxy Manager #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 #stats hide-version #隐藏统计页面上HAProxy的版本信息 #--------------------------------------------------------------------- # main frontend which proxys to the backends #--------------------------------------------------------------------- frontend main bind 0.0.0.0:80 acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js
#动态静态页面的分离 use_backend static if url_static ###满足策略要求,则响应策略定义的backend页面 default_backend dynamic ###不满足则响应backend的默认页面 #--------------------------------------------------------------------- # static backend for serving up images, stylesheets and such #--------------------------------------------------------------------- #静态页面服务器 backend static balance roundrobin ###负载均衡模式轮询 server static 127.0.0.1:80 check ###后端服务器定义
#动态页面服务器 backend dynamic balance roundrobin server websrv1 192.168.1.110:80 check maxconn 2000 server websrv2 192.168.1.184:80 check maxconn 2000 #--------------------------------------------------------------------- # round robin balancing between the various backends #--------------------------------------------------------------------- #errorloc 503 http://www.osyunwei.com/404.html errorfile 403 /etc/haproxy/errorfiles/403.http errorfile 500 /etc/haproxy/errorfiles/500.http errorfile 502 /etc/haproxy/errorfiles/502.http errorfile 503 /etc/haproxy/errorfiles/503.http errorfile 504 /etc/haproxy/errorfiles/504.http
(4)设置HAProxy日志
vi /etc/syslog.conf #编辑,在最下边增加 # haproxy.log local2.* /var/log/haproxy.log
local2.* 定义haproxy的日志记录位置,service rsyslog restart 重启日志服务
(5)启用haproxy的状态监控页配置如下
#################################################################### listen stats bind 0.0.0.0:1080 #监听端口 stats refresh 30s #统计页面自动刷新时间 stats uri /stats #统计页面url stats realm Haproxy Manager #统计页面密码框上提示文本 stats auth admin:admin #统计页面用户名和密码设置 #stats hide-version #隐藏统计页面上HAProxy的版本信息 #---------------------------------------------------------------------
(6)重启并访问
service haproxy start #启动 service haproxy stop #关闭 service haproxy restart #重启
三、配置的讲解
1、haproxy 配置中分成五部分内容,分别如下:
-
- global:参数是进程级的,通常是和操作系统相关。这些参数一般只设置一次,如果配置无误,就不需要再次进行修改
- defaults:配置默认参数,这些参数可以被用到frontend,backend,Listen组件
- frontend:接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend
- backend:后端服务集群的配置,是真实服务器,一个Backend对应一个或者多个实体服务器
- Listen Fronted和backend的组合体
2、haproxy 通过acl功能根据http请求报文中的请求方法实现对后端不同服务器组的调度
read 定义acl的名字, method匹配请求报文中首部Request Method的值
if write 如果匹配write这条acl 就使用tag_engine这个服务器组(if后面如果给了多个名字则要同时满足)
3、动静态页面分离
acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js #动态静态页面的分离 use_backend static if url_static ###满足策略要求,则响应策略定义的backend页面 default_backend dynamic ###不满足则响应backend的默认页面
4、haproxy 负载均衡MySQL服务的配置示例
#--------------------------------------------------------------------- # 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 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon defaults mode tcp log global option httplog option dontlognull retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 600 listen stats mode http bind 0.0.0.0:1080 stats enable stats hide-version stats uri /haproxyadmin?stats stats realm Haproxy\ Statistics stats auth admin:admin stats admin if TRUE frontend mysql bind *:3306 mode tcp log global default_backend mysqlservers backend mysqlservers balance leastconn server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300 server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
5、反向代理的配置
haproxy代理https有两种方式:
1)haproxy服务器本身提供ssl证书,后面的web服务器走正常的http
2)haproxy服务器本身只提供代理,后面的web服务器走https(配置ssl证书)
第一种方式:haproxy服务器本身提供ssl证书 注意: 需要编译haproxy的时候支持ssl 编译参数: #make TARGET=linux26 USE_OPENSSL=1 ADDLIB=-lz #ldd haproxy | grep ssl libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fb0485e5000) 配置参数(修改haproxy.cfg文件) frontend https_frontend bind *:443 ssl crt /etc/ssl/certs/servername.pem mode http option httpclose option forwardfor reqadd X-Forwarded-Proto:\ https default_backend web_server backend web_server mode http balance roundrobin cookie SERVERID insert indirect nocache server s1 192.168.1.150:80 check cookie s1 server s2 192.168.1.151:80 check cookie s2 ----------------------------------------------------------- 注意:这里的pem 文件是下面两个文件合并而成: #cat servername.crt servername.key |tee servername.pem ----------------------------------------------------------- 第二种方式:haproxy服务器本身只提供代理,没有ssl证书 (一般我们常用的就是这种方式) 这种方式,haproxy不需要重新编译支持ssl,简单方便,只需要后面的web服务器配置好ssl即可。 配置参数(修改haproxy.cfg文件) frontend https_frontend bind *:443 mode tcp default_backend web_server backend web_server mode tcp balance roundrobin stick-table type ip size 200k expire 30m stick on src server s1 192.168.1.150:443 server s2 192.168.1.151:443 --------------------------------------------------------- 注意,这种模式下mode 必须是tcp 模式
5、正向向代理的配置
配置文件几乎跟反向代理差不多,修改下代理的ip的地址,前端为内部地址,后端为外部地址
6、 acl <aclname> <criterion> [flags] [operator] <value> .
#用法一、允许10.0.0.0/24的用户访问,其他用户将禁止
acl goodguys src 10.0.0.0/24 tcp-request content accept if goodguys tcp-request content reject tcp-request content accept [{if | unless} <condition>] Accept a connection if/unless a content inspection condition is matched
#用法二、将源IP为172.16.254.52的用户禁止、将403的错误重定向到其他服务器;
acl badguy src 172.16.254.52 block if badguy errorloc 403 http://www.afwing.com/
#用法三、当用户访问172.16.1.100时,重定向到http://www.afwing.com
acl dstipaddr hdr(Host) 172.16.1.100 redirect location http://www.afwing.com if dstipaddr
#用法四、限制某个IP访问的路径(文件)
acl badguy src 172.16.254.52 acl denyfile path /index.html http-request deny if denyfile badguy
#用法五、读写、动静分离(见上面)
参考文献
https://www.cnblogs.com/linuxboke/p/5425914.html
http://www.jb51.net/article/101996.htm