Haproxy + Mycat
集群搭建指南
1.1 环境描述
mysql5
OS: Oracle Linux Server release 6.3
Mycat server1:10.0.30.134:8806
Mycat server2:10.0.30.139:8806
Haproxy server:10.0.30.139: 8098
前期未启用 VIP,所以先用 Mycat server2 的 8098 端口作为 haproxy 的对外接口
1.2 Mycat 安装
在 Mycat server1 及 Mycat server2 上进行安装 Mycat
Linux(Unix)下,建议放在/usr/local/MyCAT 目录下,如下面类似的:
useradd mycat
chown –R mycat.mycat /usr/local/mycat
启动 mycat
/usr/local/mycat/bin/mycat start
1.3 Haproxy 的安装
useradd haproxy
#wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.25.tar.gz
# tar zcvf haproxy-1.3.20.tar.gz
# cd haproxy-1.3.20
# make TARGET=linux26 PREFIX=/usr/local/haprpxy ARCH=x86_64
# make install
安装完毕后,进入安装目录创建配置文件
# cd /usr/local/haproxy
#chown –R haproxy.haproxy *
# vi haproxy.cfg
global
##记日志的功能
local0
log 127.0.0.1
maxconn 4096
chroot /usr/local/haproxy
user haproxy
group haproxy
daemon
defaults
dontlognull
3
log global
option
retries
option redispatch
maxconn 2000
contimeout
clitimeout
srvtimeout
5000
50000
50000
##由于没有启用 VIP,暂时用其中一台的 IP 和新端口
##统计页面
listen
admin_stats 10.0.30.139:48800
stats uri /admin-status
stats auth
mode
option
admin:admin
http
httplog
listen
allmycat 10.0.30.139:8098
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance
server mycat_134 10.0.30.134:8066 check port 48700 inter 5s rise 2 fall 3
server mycat_139 10.0.30.139:8066 check port 48700 inter 5s rise 2 fall 3
srvtimeout 20000
roundrobin
默认 haproxy 是不记录日志的,为了记录日志还需要配置 syslog 模块,在 oracle linux 下是
rsyslogd 服务,yum –y install rsyslog 先安装 rsyslog,然后
#vi /etc/rsyslog.d/haproxy.conf
加入以下内容
$ModLoad imudp
$UDPServerRun 514
local0.* /var/log/haproxy.log ##对应 haproxy.cfg 的日志记录选项
保存,重启
service rsyslog restart
现在你就可以看到日志了
在 Mycat server1 Mycat server2 上都需要添加检测端口 48700 的脚本,为此需要用到 xinetd
首先在 xinetd 目录下面增加脚本与端口的映射配置文件
#vim /etc/xinetd.d/mycat_status
service mycat_status
{
= REUSE
= stream
= 48700
= no
= nobody
= /usr/local/bin/mycat_status
flags
socket_type
port
wait
user
server
log_on_failure += USERID
disable
= no
}
再增加/usr/local/bin/mycat_status 用于检测 mycat 是否运行的脚本
#vim /usr/local/bin/mycat_status
#!/bin/bash
#/usr/local/bin/mycat_status.sh
# This script checks if a mycat server is healthy running on localhost. It will
# return:
#
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly)
#
# "HTTP/1.x 503 Internal Server Error\r" (else)
mycat=`/usr/local/mycat/bin/mycat status | grep 'not running' | wc -l`
if [ "$mycat" = "0" ];
then
/bin/echo -e "HTTP/1.1 200 OK\r\n"
else
/bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n"
fi
我是根据 mycat status 返回的状态来判定 mycat 是否在运行的,也可以直接通过 mysql
–P8806 –e”select user()” 等直接执行 sql 的形式来检测
重启 xinetd 服务
#service xinetd restart
查看 48700 端口是否监听了
#netstat -antup|grep 48700
如上图则端口的配置正确了
启动 haproxy
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg
为了使用方便可以增加一个启动,停止 haproxy 的脚本
启动脚本 starthap 内容如下
#!/bin/sh
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg &
停止脚本 stophap 内容如下
#!/bin/sh
ps -ef | grep sbin/haproxy | grep -v grep |awk '{print $2}'|xargs kill -s 9
分别赋予启动权限
chmod +x starthap
chmod +x stophap
启动后可以通过 http://10.0.30.139:48800/admin-status (用户名密码都是 admin haproxy.cnfg 配
置的)
配置完成