1、下载haproxy:
haproxy的官网地址为http://www.haproxy.org/,写这篇文章的时候需要翻墙才可以访问,
当前的最新版本为1.6.3,是在2015年12月27号发布的,下载地址为http://www.haproxy.org/download/1.6/src/haproxy-1.6.3.tar.gz
2.编译安装:
tar xvf haproxy-1.6.3.tar.gz
cd haproxy-1.6.3
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
默认没有haproxy.cfg文件,需要手动创建:
global
maxconn 51200
chroot /usr/local/haproxy
uid 99
gid 99
daemon
#quiet
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
defaults
mode http
#retries 2
option redispatch
option abortonclose
timeout connect 5000ms
timeout client 30000ms
timeout server 30000ms
#timeout check 2000
log 127.0.0.1 local0 info #[err warning info debug]
balance roundrobin
listen admin_stats
bind 0.0.0.0:8888
option httplog
stats refresh 30s
stats uri /stats
stats realm Haproxy Manager
stats auth admin:admin
stats hide-version
frontend http-in
bind *:8889
mode http
option httplog
log global
default_backend httpserver
backend httpserver
balance roundrobin
server 1 127.0.0.1:80 cookie 1 weight 1 check inter 2000 rise 2 fall 3
3.haproxy启动命令: /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
4.开启日志记录:
编辑 vim /etc/rsyslog.conf:
13 $ModLoad imudp #将注释去除
14 $UDPServerRun 514 #将注释去除
local3.* /var/log/haproxy.log #最下方添加
local0.* /var/log/haproxy.log #最下方添加
5、编辑haproxy.cfg配置文件:
defaults
mode http
#retries 2
option redispatch
option abortonclose
timeout connect 5000ms
timeout client 30000ms
timeout server 30000ms
#timeout check 2000
log 127.0.0.1 local0 info #添加这一行,日志有很多记录的级别
6、重启rsyslog和haproxy即可在/var/log/haproxy.log,格式如下:
Jan 16 20:22:18 localhost haproxy[5975]: 10.211.55.2:56413 [16/Jan/2016:20:22:16.794] http-in httpserver/1 2172/0/0/0/2172 304 150 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
|