当前位置: > Ubuntu >

Ubuntu 12.04上HAProxy安装和配置

时间:2016-02-06 15:17来源:linux.it.net.cn 作者:IT
一、安装

adam@hnn:~$ uname -a

Linux hnn 3.2.0-35-generic-pae #55-Ubuntu SMP Wed Dec 5 18:04:39 UTC 2012 i686 i686 i386 GNU/Linux

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.22.tar.gz

tar xvf haproxy-1.4.22.tar.gz

cd haproxy-1.4.22

make TARGET=linux32 PREFIX=/home/adam/haproxy

make install PREFIX=/home/adam/haproxy

安装apache

sudo apt-get install apache2

二、基本配置

vi /home/adam/haproxy/haproxy.cfg
global      
daemon
        maxconn 256
        log 127.0.0.1 local2
defaults
        log global
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

frontend http-in
        bind *:80
        option httplog
        option httpclose
        log global
        default_backend servers

backend servers
        server server2 192.168.0.130 maxconn 32
        server server1 192.168.0.129 maxconn 32

listen stats :8888
        stats enable
        stats uri       /admin
        stats auth      adam:1234

三、日志配置

sudo vi /etc/rsyslog.d/50-default.conf

增加以下一行

local2.*        /var/log/haproxy.log

sudo vi /etc/rsyslog.conf

找到下面两行,把注释去掉

#$ModLoad imudp

#$UDPServerRun 514

重启rsyslog服务

sudo service rsyslog restart

四、测试并查看日志文件

ab -n 10 -c 20 http://192.168.0.128/index.html

查看haproxy的日志

cat /var/log/haproxy.log

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60587 [23/Dec/2012:20:44:53.229] http-in servers/server1 1/0/7/5/21 200 478 – - —- 9/9/9/4/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60589 [23/Dec/2012:20:44:53.229] http-in servers/server1 1/0/7/3/21 200 478 – - —- 8/8/8/3/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60581 [23/Dec/2012:20:44:53.229] http-in servers/server1 1/0/7/13/28 200 478 – - —- 7/7/7/2/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60582 [23/Dec/2012:20:44:53.229] http-in servers/server2 1/0/7/13/28 200 478 – - —- 6/6/6/4/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60583 [23/Dec/2012:20:44:53.229] http-in servers/server1 1/0/7/13/28 200 478 – - —- 5/5/5/1/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60584 [23/Dec/2012:20:44:53.229] http-in servers/server2 1/0/7/13/28 200 478 – - —- 4/4/4/3/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60585 [23/Dec/2012:20:44:53.229] http-in servers/server1 1/0/7/13/28 200 478 – - —- 3/3/3/0/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60586 [23/Dec/2012:20:44:53.229] http-in servers/server2 1/0/7/13/28 200 478 – - —- 2/2/2/2/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60588 [23/Dec/2012:20:44:53.229] http-in servers/server2 1/0/7/13/28 200 478 – - —- 1/1/1/1/0 0/0 “GET /index.html HTTP/1.0″

Dec 23 20:44:53 localhost haproxy[2628]: 192.168.0.129:60590 [23/Dec/2012:20:44:53.229] http-in servers/server2 1/0/20/5/32 200 478 – - —- 0/0/0/0/0 0/0 “GET /index.html HTTP/1.0″

查看apache的日志

cat /var/log/apache2/

192.168.0.128 – - [23/Dec/2012:20:44:52 +0800] “GET /index.html HTTP/1.0″ 200 478 “-” “ApacheBench/2.3″

192.168.0.128 – - [23/Dec/2012:20:44:52 +0800] “GET /index.html HTTP/1.0″ 200 478 “-” “ApacheBench/2.3″

192.168.0.128 – - [23/Dec/2012:20:44:52 +0800] “GET /index.html HTTP/1.0″ 200 478 “-” “ApacheBench/2.3″

192.168.0.128 – - [23/Dec/2012:20:44:52 +0800] “GET /index.html HTTP/1.0″ 200 478 “-” “ApacheBench/2.3″

五、2013-04-30,在redhat安装注意事项

日志配置:/etc/sysconfig/syslog ,把

SYSLOGD_OPTIONS=”-m 0″

改为 SYSLOGD_OPTIONS=”-m 0 -r”

另外web server使用HTTP 1.1访问时,必须增加配置项: option httpclose,否则在日志中可以看到Haproxy只在创建新的TCP连接的时候转发请求。

2013-05-06补充,http 1.0默认是一个http请求对应一个tcp连接,除非http heaer中有Connection: Keepalive。而http 1.1默认tcp请求不会主动关闭,除非http header中有Connection: Close。


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容