简单介绍
HAProxy是一款提供高可用性、负载均衡以及基于TCP和HTTP应用的代理软件,HAProxy是全然免费的、借助HAProxy能够高速而且可靠的提供基于TCP和HTTP应用的代理解决方式。HAProxy适用于那些负载较大的web网站,这些网站通常又须要会话保持或七层处理。 HAProxy能够支持数以万计的并发连接,而且HAProxy的执行模式使得它能够非常easy安全的整合进架构中,同一时候能够保护webserver不被暴露到网络上。
安装与管理
1) 安装步骤參考例如以下,将软件上传到/opt目录下
cd /opt
tar zxvf haproxy-1.4.9.tar.gz
cd haproxy-1.4.9
make TARGET=linux26 PREFIX=/opt
make install PREFIX=/opt
2) Haproxy管理
若haproxy已经配置完成,參考例如以下命令,启动
cd /opt/haproxy/sbin
./haproxy -f haproxy.cfg
输入usernamepassword,在例如以下页面进行状态监控
http://x.x.x.x:1080/haproxy-stats
加入开机自启服务。将haproxy启动命令加入到/etc/rc.local文件里。如
/opt/haproxy/sbin/haproxy -f haproxy.cfg
配置优化
1) Haproxy配置优化说明
參数配置演示样例
规则说明
maxconn 32768
最大连接数
daemon
推荐使用守护进程模式启动
nbproc 8
负载均衡的并发进程数
retries 3
重试次数
2) Haproxy配置(haproxy.cfg)演示样例
创建配置文件
cd /opt/haproxy
touch haproxy.cfg
配置演示样例
global
log 127.0.0.1 local0
maxconn 4096
chroot /opt/haproxy
# uid www
# gid www
uid 0
gid 0
daemon
nbproc 2
pidfile logs/haproxy.pid
#debug
#quiet
defaults
log 127.0.0.1 local3
mode http
option httplog
option httpclose
option dontlognull
option forwardfor
option redispatch
retries 2
maxconn 2000
balance roundrobin
stats enable
stats uri /haproxy-stats
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web_proxy 0.0.0.0:1080
option httpchk GET /ping.jsp
server s1 192.168.15.110:8080 weight 3 check
server s3 192.168.15.111:8080 weight 3 check
listen mysql_proxy 0.0.0.0:3306
server s1 192.168.15.110:3306 weight 3 check
server s3 192.168.15.111:3306 weight 3 check
写在前面的话。《Haproxy配置文件具体解释》文档部分信息来自网络,同一时候參考过官方的架构指南,在此很感谢zero提供的文档。以及在学习Haproxy过程中的帮助。
样例
mysql
1. 加入监控MySQL状态的port
# vi /etc/services
mysqlcheck 6033/tcp # MySQL status check
2. 使用xinetd守护进程执行MySQL状态检測
# cat /etc/xinetd.d/mysqlchk
service mysqlcheck
{
disable = no
flags = REUSE
socket_type = stream
port = 6033
wait = no
user = root
server = /usr/local/haproxy/sbin/mysqlchk_status.sh
log_on_failure += USERID
}
3. 状态检測脚本
# vi /usr/local/haproxy/sbin/mysqlchk_status.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#/bin/bash
MYSQL_HOST="localhost"
MYSQL_PORT="3306"
MYSQL_USERNAME="root"
MYSQL_PASSWORD="123456"
ERROR_MSG=/usr/bin/mysql --host=$MYSQL_HOST --port=$MYSQL_PORT --user=$MYSQL_USERNAME --password=$MYSQL_PASSWORD -e "show databases;"
if [ "$ERROR_MSG" != "" ]
then
# mysql is fine, return http 200
/bin/echo -e "HTTP/1.1 200 OK\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is running.\r\n"
/bin/echo -e "\r\n"
else
# mysql is fine, return http 503
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
/bin/echo -e "Content-Type: Content-Type: text/plain\r\n"
/bin/echo -e "\r\n"
/bin/echo -e "MySQL is *down*.\r\n"
/bin/echo -e "\r\n"
fi
# chown haproxy.haproxy /usr/local/haproxy/sbin/mysqlchk_status.sh
4. HAproxy日志
# touch /var/log/haproxy.log
# chown haproxy.haproxy /var/log/haproxy.log
编辑/etc/syslog.conf文件,加入例如以下语句
local0.* /var/log/haproxy.log
5. HAProxy配置文件
[root@localhost htdocs]# cat /usr/local/haproxy/conf/haproxy.conf
global
maxconn 4096
daemon
pidfile /usr/local/haproxy/run/haproxy.pid
#debug
#quiet
user haproxy
group haproxy
defaults
log global
mode http
option httplog
option dontlognull
log 127.0.0.1 local0
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen admin_stats 192.168.0.1:80
mode http
stats uri /dbs
stats realm Global\ statistics
stats auth test:123456
listen proxy-mysql 0.0.0.0:23306
mode tcp
balance roundrobin
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
server db01 192.168.0.1:3306 weight 1 check port 6033 inter 1s rise 2 fall 2
server db02 192.168.0.2:3306 weight 1 check port 6033 inter 1s rise 2 fall 2
option tcpka
6. HAProxy启动脚本
# cat /etc/init.d/haproxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /bin/sh
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/conf/$PROGNAME.conf
PIDFILE=$PROGDIR/run/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -n "Starting $DESC: $PROGNAME"
$DAEMON -f $CONFIG
echo "."
}
stop()
{
echo -n "Stopping $DESC: $PROGNAME"
haproxy_pid=cat $PIDFILE
kill $haproxy_pid
echo "."
}
restart()
{
echo -n "Restarting $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
手机扫一扫
移动阅读更方便
你可能感兴趣的文章