Haproxy 配置详解
来源:互联网 发布:json数据转换成数组 编辑:程序博客网 时间:2024/05/19 13:14
http://www.07net01.com/linux/Haproxypeizhixiangjie_645322_1380518936.html
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作。
拓扑介绍:前端两台服务器提供高可用和haproxy,,后端有动态和静态服务器组,haproxy将动态和静态页面请求分发到不同的服务器上;在测试情况下有异常发生,当跟踪的脚本失败后,没有按照预定的动作执行;但有一点是可以肯定的,当一台服务器宕机后,服务可以正常提供;由MASTER上的keepalived提供VIP和后端服务器的网关;(在本例中用不到后端服务器网关,因为后端服务器只需与代理服务器交互,它们是在同一个网段的,不需要网关。本例中给出配置,以备不时之需)
Haproxy配置分析:
global log 127.0.0.1 local2 chroot /var/haproxy #chroot运行的路径,增加安全性 uid 99 #程序运行的用户id gid 99 #程序运行的用户组id daemon #以后台形式运行haproxy nbproc 1 #number of process进程数量,不建议修改 pidfile /var/run/haproxy.pid #haproxy的pid存放路径 maxconn 4000 #默认最大连接数defaults mode http #;工作模式 option dontlognull log global #;记录日志 option http-server-close #;启用服务器端关闭 option forwardfor except 127.0.0.0/8 #;传递客户端ip option redispatch #;当服务器组中的某台设备故障后,自动将请求重定向到组内其他主机。 retries 3 #;请求重试的次数 timeout http-request 10s #;http请求超时时间 timeout queue 1m #;一个请求在队列里的超时时间· timeout connect 10s #;连接服务器超时时间 timeout client 1m #;设置客户端侧最大非活动时间 timeout server 1m #;设置服务器侧最大非活动时间 timeout http-keep-alive 10s #;设置http-keep-alive的超时时间 timeout check 10s #;当一个连接建立之后, maxconn 3000 #;同时处理的最大连接数 #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#启用stats查看,认证等功能:#默认在/haproxy?statslisten stas #自定义监听名,任意取 bind 192.168.1.97:1099 #监听的地址和端口mode http #模式 stats enable #启用状态监控 stats hide-version #隐藏软件版本号 stats auth admin:admin #登陆用户名和密码 stats realm HAproxy\ stats #提示信息,空格之前加\ stats admin if TRUE #当通过认证才可管理 stats uri /stats #访问路径 stats refresh 5 #页面自动刷新间隔 stats refresh 20s --每隔20s刷新 stats hide-version --隐藏haproxy版本信息 stats uri /haproxy-stats --在域名后面添加/haproxy-stats可以查看haproxy监控状态 stats auth haproxy:system --用户名和密码 stats hide-version stats admin if TRUE --可以手动启动和停止服务#Haproxy 负载均衡实例:frontend webserver bind 192.168.1.97:80 default_backend webserversbackend webservers balance roundrobin server web1 192.168.1.100:80 check server web2 192.168.1.101:80 check#或者#listen webservers# bind 192.168.1.97:80# server web1 192.168.1.100:80 check# server web2 192.168.1.101:80 check
调度算法:
动态算法:
支持动态调整权重,可以在运行中调整而不用重启服务;
支持慢速启动(在刚开机时不起作用);
roundrobin 轮调
leastconn 最少连接
source
hash-type : consistent 一致性哈希算法,
静态算法:
支持权重,不支持动态调整,调整后需重启服务;
static-rr
source
hash-type :map-based:将source IP进行hash后,对后端服务器的个数取余算法;
uri 根据uri分配服务器,适用于后端是缓存服务器;也支持2种hash-type;同source算法;还支持参数,len # 和depth # ,用法如下
balance uri [len <len>][depth <depth>]
URL syntax:
<scheme>://<user>:<password>@<host>:<port>/path;<params>?<query>#<frag>
url_param
常用于跟踪用户id,将具有特定的用户标示的GET请求发往特定的服务器;默认是静态算法,用hash-type修改;用法如下;
balance url_param <param>[check_post [<max_wait>]]
balance url_paramuserid
balance url_param session_id check_post 64
hdr,根据请求头部中的字段进行调度;
balancehdr(User-Agent)
balance hdr(host)
balance hdr(Host) use_domain_only
frontend web bind 172.16.1.100:80 default_backend webservers log global log 127.0.0.1:514 local2 info option httplog定义local2的记录位置:启用UDP syslog[root@node1 ~]# vi /etc/rsyslog.conf # Provides UDP syslog reception $ModLoad imudp $UDPServerRun 514# Log anything (except mail) of level info or higher.# Don't log private authentication messages!,添加local2.none*.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages添加一行,自定义日志存放位置:local2.* /var/log/haproxy.log然后重启syslog服务;[root@node1 ~]# service rsyslog restart查看日志情况[root@node1 ~]# tail /var/log/haproxy.logSep 29 19:47:17 localhost haproxy[2847]: 172.16.254.52:53660 [29/Sep/2013:19:47:17.861] web webservers/node3.magedu.com 9/0/0/1/10 200 267 - - ---- 3/3/0/1/0 0/0 "GET /4.html HTTP/1.1"Sep 29 19:47:17 localhost haproxy[2847]: 172.16.254.52:53660 [29/Sep/2013:19:47:17.861] web webservers/node3.magedu.com 9/0/0/1/10 200 267 - - ---- 3/3/0/1/0 0/0 "GET /4.html HTTP/1.1"在后端real server上记录真实客户端IP地址:修改apache配置文件中日志格式,修改为:LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined效果为:172.16.254.52 - - [29/Sep/2013:14:30:44 +0800] "GET / HTTP/1.1" 200 50485 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/29.0.1547.62 Safari/537.36"Haproxy中ACL的使用:
acl <aclname> <criterion> [flags] [operator] <value> ...
dst <ip_address>dst_port <port>src <ip_address>src_port <port>e.g.#用法一、允许10.0.0.0/24的用户访问,其他用户将禁止acl goodguys src 10.0.0.0/24tcp-request content accept if goodguystcp-request content rejecttcp-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.52block if badguyerrorloc 403 http://www.afwing.com/#用法三、当用户访问172.16.1.100时,重定向到http://www.afwing.comacl dstipaddr hdr(Host) 172.16.1.100redirect location http://www.afwing.com if dstipaddr#用法四、读写分离:acl read method GETacl read method HEADacl write method PUTacl write method POSTuse_backend imgservers if readuse_backend uploadservers if write#用法五、限制某个IP访问的路径(文件)acl badguy src 172.16.254.52acl denyfile path /index.htmlhttp-request deny if denyfile badguy#用法六、动静分离acl url_static path_beg -i /static /images /Javascript /stylesheetsacl url_static path_end -i .jpg .gif .png .css .js#或者acl url_static path_end -i .jpg$ .gif$ .png$ .css$ .js$#或者acl url_static hdr_beg(host) -i wwwacl url_static hdr_beg(host) -i news. video. download. ftp.use_backend static if url_staticdefault_backend appbackend static balance roundrobin server static 192.168.10.1:80 check maxconn 6000 server static 192.168.10.2:80 check maxconn 6000backend app balance roundrobin server app1 192.168.10.3:80 check maxconn 1000 server app2 192.168.10.4:80 check maxconn 1000#Additional examplesacl invalid_src src 0.0.0.0/7 224.0.0.0/3acl invalid_src src_port 0:1023acl local_dst hdr(host) -i localhostMove the login URL>server和default-server options:backup :当后端服务器都发生故障时,由backup服务器发送错误页面:在haproxy服务器上启动http服务以apache为例,监听在127.0.0.1的某个端口:[root@node1 haproxy]# vi /etc/httpd/conf/httpd.confListen 127.0.0.1:8888[root@node1 haproxy]# service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ][root@node1 haproxy]# vi /var/www/html/index.html<h1>Sorry,our site is not in service.Please try again later.</h1>[root@node1 haproxy]# vi /etc/haproxy/haproxy.cfg frontend web bind 172.16.1.100:80 default_backend webservers log 127.0.0.1:514 local2 info option httplog option forwardfor except 127.0.0.0/8 rspadd x-via:\ 172.16.1.1 backend webservers balance uri hash-type map-based server node3.magedu.com 192.168.10.3:80 check weight 3 server node4.magedu.com 192.168.10.4:80 check weight 1 server backup.magedu.com 127.0.0.1:8888 backup
global log 127.0.0.1 local2 chroot /var/lib/haproxy #chroot运行的路径,增加安全性 uid 99 #程序运行的用户id gid 99 #程序运行的用户组id daemon #以后台形式运行haproxy nbproc 1 #number of process进程数量,不建议修改 pidfile /var/run/haproxy.pid #haproxy的pid存放路径 maxconn 4000 #默认最大连接数defaults mode http #;工作模式 option dontlognull log global #;记录日志 option http-server-close #;启用服务器端关闭 option forwardfor except 127.0.0.0/8 #;传递客户端ip option redispatch #;当服务器组中的某台设备故障后,自动将请求重定向到组内其他主机。 retries 3 #;请求重试的次数 timeout http-request 10s #;http请求超时时间 timeout queue 1m #;一个请求在队列里的超时时间· timeout connect 10s #;连接服务器超时时间 timeout client 1m #;设置客户端侧最大非活动时间 timeout server 1m #;设置服务器侧最大非活动时间 timeout http-keep-alive 10s #;设置http-keep-alive的超时时间 timeout check 10s #;当一个连接建立之后, maxconn 3000 #;同时处理的最大连接数 #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#启用stats查看,认证等功能:#默认在/haproxy?statslisten stas #自定义监听名,任意取 bind 192.168.1.97:1099 #监听的地址和端口 mode http #模式 stats enable #启用状态监控 stats hide-version #隐藏软件版本号 stats auth admin:admin #登陆用户名和密码 stats realm HAproxy\ stats #提示信息,空格之前加\ stats admin if TRUE #当通过认证才可管理 stats uri /stats #访问路径 stats refresh 5 #页面自动刷新间隔#Haproxy 负载均衡实例:frontend webserver bind 192.168.1.97:80 default_backend webservers log global log 127.0.0.1:514 local2 info option httplog ##################将源IP为192.168.1.13的用户禁止、将403的错误重定向到其他服务器; #acl badguy src 192.168.1.13 #http-request deny if badguy #errorloc 403 http://www.baidu.com ################# # 限制某个IP访问的路径(文件) # acl badguy src 192.168.1.13 # acl denyfile path /1.html # http-request deny if badguy denyfile ############### #当用户访问192.168.1.97时,重定向到http://www.baidu.com #acl dstipaddr hdr(Host) 192.168.1.97 #redirect location http://www.baidu.com if dstipaddr ######################## #动静分离 #acl static path_end -i .html .jpg .png.jpeg .gif .swf .css .xml .txt .pdf # use_backend jingtai if static #default_backend dongtai #backend jingtai # server web1 192.168.1.100:80 check #backend dongtai # server web2 192.168.1.101:80 checkbackend webservers balance roundrobin server web1 192.168.1.100:80 check server web2 192.168.1.101:80 check#或者#listen webservers# bind 192.168.1.97:80# server web1 192.168.1.100:80 check# server web2 192.168.1.101:80 check
- haproxy配置详解
- HAProxy日志配置详解
- haproxy配置详解
- HAproxy 配置详解
- Haproxy 配置详解
- haproxy配置详解
- haproxy配置详解
- HAProxy安装配置详解
- HAProxy日志配置详解
- HAProxy安装配置详解
- HAProxy安装配置详解
- HAproxy 配置详解
- haproxy.cfg配置详解
- HAProxy安装配置详解
- HAProxy安装配置详解
- Haproxy的详解和配置
- HAproxy指南之haproxy配置详解1(理论篇)
- HAproxy指南之haproxy配置详解2(理论篇)
- 中断定时器
- LB 负载均衡的层次结构
- 多重部分和问题
- 这么巧妙思路(二)Longest Ordered Subsequence
- 树莓派(2代)内核定制及调试准备
- Haproxy 配置详解
- 1265: 青年歌手大奖赛_评委会打分
- 深入Java核心 Java内存分配原理精讲
- Fresco(Facebook开源的android项目)图片缓存框架的eclipse整合与使用探索(较详细配置说明)
- sizeof()计算一个类的对象大小和sizeof(string)的问题
- jsp中文乱码问题
- 解决centos 6.6 更换yum 163源报错
- 多次运行,端口被占用错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099; nested exception
- 什么是set