由于在生产环境使用了mysqlcluster,需要实现高可用负载均衡,选用keepalived+haproxy来实现。
1.Mysql Cluster安装
参考 CentOS 6.3 安装 mysql-cluster-gpl-7.4.7
http://maosheng.iteye.com/blog/2249106
2.haproxy安装(haproxy-1.3.15.10)
参考CentOS 6.3 安装 HAProxy
http://maosheng.iteye.com/blog/2256676
haproxy配置文件:
# vi /etc/haproxy/haproxy.conf
global
maxconn 4096
pidfile /var/run/haproxy.pid
daemon
nbproc 1
defaults
log global
mode http
retries 3
option redispatch
option httplog
option httpclose
option abortonclose
maxconn 4096
timeout connect 50000
timeout client 50000
timeout server 50000
listen mysql_proxy
bind 172.16.10.75:3366
mode tcp
option tcpka
option httpchk
balance roundrobin
server mysqldb1 172.16.10.70:3306 weight 1
server mysqldb1 172.16.10.71:3306 weight 1
listen stats
bind 172.16.10.75:8888
mode http
option httpclose
option httplog
stats refresh 5s
balance roundrobin
stats uri /
stats realm Haproxy\ Statistics
stats auth admin:admin
3.keepalived安装
参考CentOS 6.3 安装 Keepalived
http://maosheng.iteye.com/blog/2238747
参考Keepalived+Nginx 实现双机热备
http://maosheng.iteye.com/blog/2242594
keepalived配置文件:
Master:
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAProxy_DEVEL_1
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
172.16.10.75
}
}
Backup:
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAProxy_DEVEL_2
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
172.16.10.75
}
}
配置侦测haproxy状态脚本:
# vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/haproxy-1.3.15.10/sbin/haproxy -f /etc/haproxy/haproxy.conf
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
4.haproxy检查
http://172.16.10.75:8888/