一、环境
Master(主机A):192.168.1.1
Slave(主机B) :192.168.1.2
W-VIP(写入) :192.168.1.3
R-VIP(读取) :192.168.1.4
Client(测试) :192.168.1.100
操作系统版本:CentOS release 6.4
MySQL数据库版本:5.6.14
keepalived版本:1.2.7
LVS版本:1.26
所有环境均为虚拟机
二、设计思路
1. 服务器A和B,通过mysql的slave进程同步数据。
2. 通过keepalived启用两个虚IP:W-VIP/R-VIP,一个负责写入,一个负责读取,实现读写分离。
3. A和B都存在时,W-VIP下将请求转发至主机A,R-VIP将请求转发给A和B,实现负载均衡。
4. 当主机A异常时,B接管服务,W-VIP/R-VIP此时漂到了主机B上,此时这两个虚IP下都是主机B,实现高可用
5. 当主机B异常时,R-VIP会将B踢出,其他不变
三、架构图
四、软件安装
主从两个主机都要装以下软件:
1. MySQL的安装(略)
2. keepalived安装
yum install keepalived
2. LVS安装
yum install ipvsadm
五、配置
1. 配置MySQL的主从复制(略)
2. 配置keepalived
Master上的配置
vi /etc/keepalived/keepalived.conf
-
! Configuration File for keepalived
-
-
global_defs {
-
router_id MySQL-ha
-
}
-
-
vrrp_instance VI_1 {
-
state BACKUP
-
interface eth1
-
virtual_router_id 90
-
priority 100
-
advert_int 1
-
notify_master "/usr/local/mysql/bin/remove_slave.sh"
-
nopreempt
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
192.168.1.3 label eth1:1
-
192.168.1.4 label eth1:2
-
}
-
}
-
-
virtual_server 192.168.1.3 6603 {
-
delay_loop 2
-
lb_algo wrr
-
lb_kind DR
-
persistence_timeout 60
-
protocol TCP
-
real_server 192.168.1.1 6603 {
-
weight 3
-
notify_down /usr/local/mysql/bin/mysql.sh
-
TCP_CHECK {
-
connect_timeout 10
-
nb_get_retry 3
-
delay_before_retry 3
-
connect_port 6603
-
}
-
}
-
}
-
-
virtual_server 192.168.1.4 6603 {
-
delay_loop 2
-
lb_algo wrr
-
lb_kind DR
-
persistence_timeout 60
-
protocol TCP
-
real_server 192.168.1.1 6603 {
-
weight 1
-
notify_down /usr/local/mysql/bin/mysql.sh
-
TCP_CHECK {
-
connect_timeout 10
-
nb_get_retry 3
-
delay_before_retry 3
-
connect_port 6603
-
}
-
}
-
real_server 192.168.1.2 6603 {
-
weight 3
-
TCP_CHECK {
-
connect_timeout 10
-
nb_get_retry 3
-
delay_before_retry 3
-
connect_port 6603
-
}
-
}
-
}
keepalived配置成服务并开机启动
-
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
-
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
-
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
-
chkconfig --add keepalived
-
chkconfig --level 345 keepalived on
vi /usr/local/mysql/bin/remove_slave.sh
-
#!/bin/bash
-
user=u1
-
password=12345
-
log=/usr/local/mysql/log/remove_slave.log
-
echo "`date`" >> $log
-
/usr/local/mysql/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log
-
/bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf
vi /usr/local/mysql/bin/mysql.sh
-
#!/bin/bash
-
/etc/init.d/keepalived stop
Slave上的配置
vi /etc/keepalived/keepalived.conf
-
! Configuration File for keepalived
-
-
global_defs {
-
router_id MySQL-ha
-
}
-
-
vrrp_instance VI_1 {
-
state BACKUP
-
interface eth1
-
virtual_router_id 90
-
priority 99
-
advert_int 1
-
notify_master "/usr/local/mysql/bin/remove_slave.sh"
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
192.168.1.3 label eth1:1
-
192.168.1.4 label eth1:2
-
}
-
}
-
-
virtual_server 192.168.1.3 6603 {
-
delay_loop 2
-
lb_algo wrr
-
lb_kind DR
-
persistence_timeout 60
-
protocol TCP
-
real_server 192.168.1.2 6603 {
-
weight 3
-
notify_down /usr/local/mysql/bin/mysql.sh
-
TCP_CHECK {
-
connect_timeout 10
-
nb_get_retry 3
-
delay_before_retry 3
-
connect_port 6603
-
}
-
}
-
}
-
-
virtual_server 192.168.1.4 6603 {
-
delay_loop 2
-
lb_algo wrr
-
lb_kind DR
-
persistence_timeout 60
-
protocol TCP
-
real_server 192.168.1.2 6603 {
-
weight 3
-
notify_down /usr/local/mysql/bin/mysql.sh
-
TCP_CHECK {
-
connect_timeout 10
-
nb_get_retry 3
-
delay_before_retry 3
-
connect_port 6603
-
}
-
}
-
}
keepalived配置成服务并开机启动
-
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
-
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
-
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
-
chkconfig --add keepalived
-
chkconfig --level 345 keepalived on
vi /usr/local/mysql/bin/remove_slave.sh
-
#!/bin/bash
-
user=u1
-
password=12345
-
log=/usr/local/mysql/log/remove_slave.log
-
echo "`date`" >> $log
-
/usr/local/mysql/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log
-
/bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf
vi /usr/local/mysql/bin/mysql.sh
-
#!/bin/bash
-
/etc/init.d/keepalived stop
3. 配置LVS
Master与Slave上的配置相同:
vi /usr/local/bin/lvs_real.sh
-
#!/bin/bash
-
# description: Config realserver lo and apply noarp
-
-
SNS_VIP=192.168.1.3
-
SNS_VIP2=192.168.1.4
-
source /etc/rc.d/init.d/functions
-
case "$1" in
-
-
start)
-
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
-
ifconfig lo:1 $SNS_VIP2 netmask 255.255.255.255 broadcast $SNS_VIP2
-
/sbin/route add -host $SNS_VIP dev lo:0
-
/sbin/route add -host $SNS_VIP2 dev lo:1
-
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
-
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
-
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
-
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
-
sysctl -p >/dev/null 2>&1
-
echo "RealServer Start OK"
-
;;
-
-
stop)
-
ifconfig lo:0 down
-
ifconfig lo:1 down
-
route del $SNS_VIP >/dev/null 2>&1
-
route del $SNS_VIP2 >/dev/null 2>&1
-
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
-
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
-
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
-
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
-
echo "RealServer Stoped"
-
;;
-
-
*)
-
-
echo "Usage: $0 {start|stop}"
-
exit 1
-
esac
-
exit 0
-
chmod 755 /usr/local/bin/lvs_real.sh
-
echo "/usr/local/bin/lvs_real.sh start" >> /etc/rc.local
五、Master和Slave的启动
1. 启动Master上的MySQL
service mysql start
2. 启动Slave上的MySQL
service mysql start
3. 启动Master上的realserver脚本
/usr/local/bin/lvs_real.sh start
4. 启动Slave上的realserver脚本
/usr/local/bin/lvs_real.sh start
5. 启动Master上的keepalived
service keepalived start
6. 启动Slave上的keepalived
service keepalived start
六、测试
1. 查看lvs能否进行负载均衡转发
在Master和Slave上分别执行:
ipvsadm -ln
2. 在Client上验证连通性:
ping 192.168.1.3
ping 192.168.1.4
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
3. 停掉Master上的MySQL,看写IP否自动切换到Slave,看读IP是否去掉了Master的MySQL
在Master和Slave上分别执行:
ipvsadm -ln
在Client上执行:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
4. 停掉Master上的keepalived,看读写VIP是否会迁移到Slave上。
在Master和Slave上分别执行:
ipvsadm -ln
在Client上执行:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
5. 停掉Slave上的MySQL,看读IP是否去掉了Slave的MySQL
在Master和Slave上分别执行:
ipvsadm -ln
在Client上执行:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.4 -e "show variables like 'server_id'"
6. 重启Master的系统,看看切换过程是否正常
七、参考
http://blog.chinaunix.net/uid-23500957-id-3781918.html
http://blog.chinaunix.net/uid-23500957-id-3781919.html
http://blog.chinaunix.net/uid-20639775-id-3337471.html