标签:haproxy
一、环境说明:
实验环境
192.168.133.136 proxy
192.168.133.137 web1
192.168.133.130 web2
官方地址:http://haproxy.1wt.eu/
下载地址:http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
二、软件安装
tar xzvf haproxy-1.4.24.tar.gz
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
三、创建配置文件
useradd haproxy -u 500
vim /etc/haproxy.cfg
global
log 127.0.0.1 local3
maxconn 20480
chroot /usr/local/haproxy
uid 500 #1004为haproxy 用户的uid ,haproxy用户需要自己手动创建
gid 500
daemon
quiet
nbproc 1
pidfile /var/run/haproxy.pid
defaults
log global
mode http
maxconn 20480
option httplog
option httpclose
option forwardfor
option dontlognull
option redispatch
retries 3
balance roundrobin
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_poll 192.168.133.136:80
mode http
option httplog
option dontlognull
option logasap
option forwardfor
option httpclose
# option httpchk GET /index.html
server web1 192.168.133.137:80 cookie 1 check inter 2000 rise 3 fall 3
server web2 192.168.133.130:80 cookie 1 check inter 2000 rise 3 fall 3
listen status 192.168.133.136:8080
stats enable
stats uri /stats
stats auth admin:123456
stats realm (Haproxy\ statistic)
四、添加日志
vim /etc/rsyslog.conf
添加:
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
local0.* /var/log/haproxy.log
vim /etc/sysconfig/rsyslog
修改:
SYSLOGD_OPTIONS="-c 2 -r -m 0"
/etc/init.d/rsyslog restart
五、创建haproxy启动脚本
vim /etc/init.d/haproxy
#!/bin/bash
#
# haproxy
#
# chkconfig: 35 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
config="/etc/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $config
}
start() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $config -p /var/run/$prog.pid
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $config
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $config -p /var/run/$prog.pid -sf $(cat /var/run/$prog.pid)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
checkconfig)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|checkconfig|restart|try-restart|reload|force-reload}"
exit 2
esac
chmod +x /etc/init.d/haproxy
六、启动
报错:
Starting haproxy: [ALERT] 177/105503 (18602) : Starting proxy cacti: cannot bind socket
产生这个错误可能有两个原因:
1)没有加入内核参数 (net.ipv4.ip_nonlocal_bind=1)
2)端口冲突
vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1 (最后一行添加)
sysctl -p(重新加载)
查看状态页面
http://192.168.133.136:8080/stats
测试:
打开http:192.168.133.136
可以轮询到两个web服务器上。
haproxy 配置文件说明
来源http://www.linuxidc.com/Linux/2012-07/65350.htm
原文档地址:http://blog.chinaunix.net/uid-24250828-id-3778032.html
本文出自 “高路川” 博客,请务必保留此出处http://luchuangao.blog.51cto.com/11311715/1761075
标签:haproxy
原文地址:http://luchuangao.blog.51cto.com/11311715/1761075