http://pkgs.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.7.9.tar.gz
也可以直接用yum安装
yum install -y haproxy
tar zxf haproxy-1.7.9.tar.gz
cd haproxy-1.7.9
make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
#参数说明
TARGET=linux26 #内核版本,使用uname -r查看内核,如:2.6.18-371.el5,此时该参数就为linux26;kernel 大于2.6.28的用:TARGET=linux2628
ARCH=x86_64 #系统位数
PREFIX=/usr/local/haprpxy #/usr/local/haprpxy为haprpxy安装路径
vi /etc/init.d/haproxy
#!/bin/sh
#
# haproxy
#
# chkconfig: - 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/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
exec="/usr/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
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 $cfgfile -p $pidfile $OPTIONS
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 $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac
chkconfig --add haproxy
chkconfig haproxy on
chmod +x /etc/init.d/haproxy
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
log global
log 127.0.0.1 local3
mode http
option tcplog
option dontlognull
retries 10
option redispatch
maxconn 2000
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
listen mysql
bind 0.0.0.0:7306
mode tcp
balance roundrobin
server mysql1 192.168.1.78:3306
server mysql2 192.168.1.77:3306
listen stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s
stats uri /stats
stats realm XingCloud\ Haproxy
stats auth admin:admin #用这个账号登录,可以自己设置
stats auth Frank:Frank
stats hide-version
stats admin if TRUE
(1).vim /etc/rsyslog.conf
# Provides UDP syslog reception #去掉下面两行注释,开启UDP监听
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log #添加日志
(2).vi /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=""
改为 SYSLOGD_OPTIONS="-r -m 2 -c 2"
(3).创建日志文件
touch /var/log/haproxy.log
service haproxy start
service rsyslog restart
netstat -plantu | grep 7306 --查看端口7306
GRANT ALL ON *.* TO 'haproxy'@'192.168.1.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
yum install -y mysql (若没有mysql客户端,安装)
mysql -uhaproxy -p123456 -h 192.168.1.78
mysql -uhaproxy -p123456 -h 192.168.1.77
在其他的服务器上输入(haproxy的服务器地址192.168.88):
mysql -uhaproxy -p123456 -h 192.168.88 -P 7306
是否能连接到数据库
7306是在配置文件中设置的端口,通过haproxy的7306端口访问mysql的3306端口
浏览器输入http://192.168.1.88:1080/stats
出现下图所示:
证明成功。