平台haproxy构建20130510
下载:
wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.23.tar.gz
- linux2628 for Linux 2.6.28 and above (enables splice and tproxy)
splice是linux内核2.6.25之后提供的一个功能,能在网卡之间基于内核之间传输数据而不需要在拷贝数据,减少拷贝数据的cpu占用
# uname -a
Linux haproxy30 2.6.32-358.6.1.el6.x86_64 #1 SMP Tue Apr 23 19:29:00 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
解压、编译、安装
# tar xvzf haproxy-1.4.23.tar.gz
# cd haproxy-1.4.23
# make TARGET=linux2628 PREFIX=/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
mkdir /usr/local/haproxy/logs/
启动命令:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
重启命令:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -sf $(cat /usr/local/haproxy/haproxy.pid)
分配置文件启动命令:
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg -f /usr/local/haproxy/haproxy.cfg.pingtai.web
# ulimit -n
65535
配置haproxy发送http log
# more /usr/local/haproxy/haproxy.cfg
defaults
log global
mode http
option httplog
option dontlognull
log 127.0.0.1 local0
retries 3
日志服务器接收haproxy发送的日志
# more /etc/rsyslog.d/haproxy_log.conf
# Enable UDP port 514 to listen to incoming log messages from haproxy
$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%msg%\n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy.admin;Haproxy
# don't log anywhere else
local0.* ~
按日切割日志
# more /etc/logrotate.d/haproxy
/var/log/haproxy.log {
missingok
notifempty
sharedscripts
rotate 14
daily
compress
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
}
附:配置文件
# more /usr/local/haproxy/haproxy.cfg
global
maxconn 20000
chroot /usr/local/haproxy
uid 99
gid 99
daemon
quiet
nbproc 2
pidfile /usr/local/haproxy/haproxy.pid
defaults
log global
mode http
option httplog clf
option dontlognull
log 127.0.0.1 local0
retries 3
option redispatch
maxconn 20000
contimeout 5000
clitimeout 30000
srvtimeout 30000
###haproxy statistics monitor
listen statics :8889
mode http
transparent
stats refresh 60s
stats uri / haproxy-stats
stats realm Haproxy \ statistic
stats auth l2212:tes21t201
###以下是mysql slave haproxy的备份配置
listen pool_mysql_slave_backup 192.168.0.6:3306
mode tcp
maxconn 8000
balance roundrobin
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
server sdb1_192.168.0.9 192.168.0.9:3306 check port 8822 inter 2s rise 2 fall 2
server sdb2_192.168.0.10 192.168.0.10:3306 check port 8822 inter 2s rise 2 fall 2
option tcpka
###前端配置
frontend wby_pt_http_frontend
mode http
bind *:80
bind *:8080
option httpclose
option httpchk
option forwardfor
option redispatch
capture request header Host len 60
capture request header Referrer len 60
##C
acl domain_c hdr(host) -i qu.xxyyzz.com
##将c老域名跳转
acl domain_c_old hdr_beg(host) -i plat.xxyyzz.com
redirect location http://qu.xxyyzz.com if domain_c_old
##C Login登录授权 http://qu.xxyyzz.com/Login => http://192.168.0.5:80809
acl domain_c_login_url url_beg /Login/
##根据规则选择backend
use_backend server_pool_c_login if domain_c_login_url domain_c
use_backend server_pool_c if domain_c
###后端server pool配置
##C
backend server_pool_c
mode http
balance roundrobin
server C11 192.168.10.11:80 check weight 1 check inter 10s rise 2 fall 3
server C12 192.168.10.12:80 check weight 1 check inter 10s rise 2 fall 3
##C登录授权
backend server_pool_c_login
mode http
balance roundrobin
##重写到login的header,否则 login页无样式
reqrep ^([^\ :]*)\ /Login/(.*) \1\ /\2
server Login_auth53 192.168.0.5:8089 check weight 1 check inter 10s rise 2 fall 3