@delight
2015-01-10T06:29:19.000000Z
字数 3238
阅读 3564
haproxy push websocket cometdUbuntu14.04直接用apt安装就是最新的稳定版,其他旧版本Ubuntu需要使用ppa获得:
sudo add-apt-repository ppa:vbernat/haproxy-1.5
然后update,install即可。
(PS: 如果连add-apt-repository都不能用,先执行sudo apt-get install python-software-properties)
理论上直接用sudo service haproxy start|stop|restart|status|reload就可以,不过ubuntu直接安装后这个命令是没法用的…需要编辑/etc/init.d/haproxy,然后把ENABLED=0改成ENABLED=1,然后删除
if [ -e /etc/default/haproxy ]; then. /etc/default/haproxyfi
这几行。当然,也可以直接使用sudo haproxy -f /etc/haproxy/haproxy.conf来启动,加上-d参数可以在前台运行调试。
如果同时使用两种transport(websocket和http),需要注意long-polling的session保持问题。如果只使用websocket,需要注意的只有timeout的设置问题。
典型配置如下[1],默认路径为/etc/haproxy/haproxy.conf:
globallog 127.0.0.1 local0 #see /etc/rsyslog.d/haproxy.confchroot /var/lib/haproxypidfile /var/run/haproxy.piduid 99gid 99daemon #run as servicenbproc 1 #only one instance allowedmaxconn 120000defaultsmode httplog globaloption httplog #log http infooption http-server-close #Don't keepalive between haproxy and serveroption redispatch #if health check failed, dispath new serveroption forwardfor #for server get Ip of clientretries 3 #connect to server fail max times before redispatchtimeout connect 10s #timeout tcp connection between haproxy and backend serverstimeout client 50s #timeout client inactivitytimeout server 50s #timeout for server to process the requesttimeout queue 30s #timeout for request in queue when server reach max connectiontimeout http-keep-alive 2stimeout http-request 15sdefault-server inter 5s rise 2 fall 3stats uri /statsstats refresh 10sstats auth baina:P@55wordfrontend ft_webbind *:80timeout client 10mtimeout client-fin 5smaxconn 120000option http-pretend-keepalive #without this, handshake can't be establisheddefault_backend cometdbackend cometdtimeout server 10mtimeout tunnel 10mbalance roundrobin# balance sourceoption httpchk GET /cometd HTTP/1.1\r\nHost:\ \r\nConnection:\ upgrade\r\nUpgrade:\ websockethttp-check expect status 101cookie SERVERID insertappsession SERVERID len 25 timeout 15m #for Long-polling keep sessionserver bayuex-srv1 10.232.2.118:80 maxconn 40000 weight 10 cookie bayuex-srv1 checkserver bayeux-srv2 10.235.30.6:80 maxconn 40000 weight 10 cookie bayeux-srv2 checkserver bayeux-srv3 10.45.160.213:80 maxconn 40000 weight 10 cookie bayexu-srv3 check
存活检测通过httpchk选项来完成,cometd要求必须使用http1.1,因此header中必须要有Host,这里留空。
这里通过插入cookie来满足long polling的session保持需求,这要求client每次post都必须携带server发给client的cookie。当然这个问题也可以通过balance source hash ip来解决,但是后者可能会导致负载均衡度不高。
注意,如果使用long polling,切记加上option http-pretend-keepalive,不然server会把Connection: close发给client,握手直接被终结.
上述配置中,log 127.0.0.1 local0这句是用来配置log的,如果使用syslog,在/etc/syslog.conf中添加
local0.* /var/log/haproxy/haproxy.log
即可指定到具体文件。
ubuntu使用rsyslog,因此对应的配置方法如下[2],默认路径/etc/rsyslog.d/haproxy.conf:
$ModLoad imudp$UDPServerRun 514$template Haproxy,"%msg%\n"local0.* -/var/log/haproxy.log;Haproxy& ~
为使配置生效,请将文件名改为49-haproxy.conf
日志的格式[3]可以通过配置$template参数来完成,这里写了最简单的一种输出格式。
日志滚动通过配置/etc/logrotate.d/haproxy来实现,默认有一个按日滚动的策略,一般够用了。
其格式如下:
/var/log/haproxy.log {daily #按日滚动rotate 10 #保留10个missingoknotifemptycompressdelaycompresspostrotateinvoke-rc.d rsyslog rotate >/dev/null 2>&1 || trueendscript}
haproxy可以用来做ddos防范,具体可参见:
http://blog.sina.com.cn/s/blog_704836f40101f4qh.html