一、实现了一种事件驱动,单一进程模型,支持数万计的并发链接,用于为tcp和http应用程序提供高可用,负载均衡和代理服务的解决方案,尤为适用于高负载且须要持久链接或7层处理机制的web站点javascript
二、代理(http)做用: web缓存(加速)、反向代理、内容路由(根据流量及内容类型等将请求转发至特定服务器)、转码器(将后端服务器的内容压缩后传输给client端)-在代理服务器上添加Via首部;css
三、缓存的做用:减小冗余内容传输,节省带宽、缓解网络瓶颈,下降了对原始服务器的请求压力,下降了传输延迟html
四、负载均衡做用:支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机,特别适用于那些负载特大的web站点,这些站点一般又须要会话保持或七层处理前端
五、负载均衡对比java
LVS: 一、抗负载能力强。抗负载能力强、性能高,能达到F5硬件的60%;对内存和cpu资源消耗比较低 二、工做在网络4层,经过vrrp协议转发(仅做分发之用),具体的流量由linux内核处理,所以没有流量的产生。 二、稳定性、可靠性好,自身有完美的热备方案;(如:LVS+Keepalived) 三、应用范围比较广,能够对全部应用作负载均衡; 四、不支持正则处理,不能作动静分离。 五、支持负载均衡算法:rr(轮循)、wrr(带权轮循)、lc(最小链接)、wlc(权重最小链接) 六、配置 复杂,对网络依赖比较大,稳定性很高。 Ngnix: 一、工做在网络的7层之上,能够针对http应用作一些分流的策略,好比针对域名、目录结构; 二、Nginx对网络的依赖比较小,理论上能ping通就就能进行负载功能; 三、Nginx安装和配置比较简单,测试起来比较方便; 四、也能够承担高的负载压力且稳定,通常能支撑超过1万次的并发; 五、对后端服务器的健康检查,只支持经过端口来检测,不支持经过url来检测。 六、Nginx对请求的异步处理能够帮助节点服务器减轻负载; 七、Nginx仅能支持http、https和Email协议,这样就在适用范围较小。 八、不支持Session的直接保持,但能经过ip_hash来解决。、对Big request header的支持不是很好, 九、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、Ip-hash(Ip哈希) 十、Nginx还能作Web服务器即Cache功能。 HAProxy的特色是: 一、支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机; 二、可以补充Nginx的一些缺点好比Session的保持,Cookie的引导等工做 三、支持url检测后端的服务器出问题的检测会有很好的帮助。 四、更多的负载均衡策略好比:动态加权轮循(Dynamic Round Robin),加权源地址哈希(Weighted Source Hash),加权URL哈希和加权参数哈希(Weighted Parameter Hash)已经实现 五、单纯从效率上来说HAProxy更会比Nginx有更出色的负载均衡速度。 六、HAProxy能够对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。 九、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie) 十、不能作Web服务器即Cache。
一、简单的架构图python
二、环境(说明):mysql
HAProxy:192.168.1.157
web1:192.168.1.110
web2:192.168.1.184linux
三、安装步骤web
(1)关闭Selinux和防火墙redis
(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 #重启
一、haproxy 配置中分红五部份内容,分别以下:
二、haproxy 经过acl功能根据http请求报文中的请求方法实现对后端不一样服务器组的调度
read 定义acl的名字, method匹配请求报文中首部Request Method的值
if write 若是匹配write这条acl 就使用tag_engine这个服务器组(if后面若是给了多个名字则要同时知足)
三、动静态页面分离
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的默认页面
四、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
五、反向代理的配置
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 模式
五、正向向代理的配置
配置文件几乎跟反向代理差很少,修改下代理的ip的地址,前端为内部地址,后端为外部地址
六、 acl <aclname> <criterion> [flags] [operator] <value> .
#用法1、容许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
#用法2、将源IP为172.16.254.52的用户禁止、将403的错误重定向到其余服务器;
acl badguy src 172.16.254.52 block if badguy errorloc 403 http://www.afwing.com/
#用法3、当用户访问172.16.1.100时,重定向到http://www.afwing.com
acl dstipaddr hdr(Host) 172.16.1.100 redirect location http://www.afwing.com if dstipaddr
#用法4、限制某个IP访问的路径(文件)
acl badguy src 172.16.254.52 acl denyfile path /index.html http-request deny if denyfile badguy
#用法5、读写、动静分离(见上面)
参考文献
https://www.cnblogs.com/linuxboke/p/5425914.html
http://www.jb51.net/article/101996.htm