1.准备机器,需要至少准备2台机器
3.配置mysql双机master-master实现,不再赘述,参见另一篇mysql mmm的博文
2.安装lvs和keepalived和配置keepalived,参见http://www.keepalived.org/sery-lvs-cluster.pdf,注意lvs的最新版本是1.26,对应的是linux2.6.28以上内核版本,版本需要一致,
否则编译不通过,查看内核版本命令: uname -r
3. 操作说明:
1). keepalived启动后会自动绑定虚ip到物理网卡上,比如eth0。如果主机不同时是mysql主机不需要realserver脚本。
2). mysql主机,即不需要keepalived服务的主机,只需要安装lvs模块,并且需要配置realserver脚本,并且需要启动此脚本。
realserver里配置的虚ip月keepalived里配置的虚ip保持一致,虚ip一定要被绑定到lo上。这是由lvs/dr模式的处理方式决定的。(使用VS/DR方法:调度器和服务 器组都必须在物理上有一个网卡通过不分断的局域网相连,如通过交换机或者高速的HUB相连。VIP地址为调度器和服务器组共享,调度器配置的VIP地址是 对外可见的,用于接收虚拟服务的请求报文;所有的服务器把VIP地址配置在各自的Non-ARP网络设备上,它对外面是不可见的,只是用于处理目标地址为 VIP的网络请求。在VS/DR中,调度器根据各个服务器的负载情况,动态地选择一台服务器,不修改也不封装IP报文,而是将数据帧的MAC地址改为选出 服务器的MAC地址,再将修改后的数据帧在与服务器组的局域网上发送。因为数据帧的MAC地址是选出的服务器,所以服务器肯定可以收到这个数据帧,从中可 以获得该IP报文。当服务器发现报文的目标地址VIP是在本地的网络设备上,服务器处理这个报文,然后根据路由表将响应报文直接返回给客户。 )
3).keepalived需要安装1.2.1版本,否则sorry server的切换会有问题
4).自动切换原理是:keepalived realserver列表只配置一台mysql主机,sorry server配置另一台主机,这样在realserver正常情况下,sorry server不会被切换,
只充当备机,只有realserver出故障时才切换到sorry server,keepalived的sorry server天生具有这个特性。
5). iptables防火墙配置,keepalived的master与backup之间需要保证心跳,这样才能保证其相互切换,如果iptables不进行特殊配置,则不会正常切换。配置如下:
-I INPUT -i eth0 -d 224.0.0.0/8 -j ACCEPT
-A INPUT -p 112 -i eth0 -j ACCEPT
-A OUTPUT -p 112 -o eth0 -j ACCEPT
4. 具体操作:
1) keepalived master主机和mysql master主机:10.3.1.24
keepalived backup主机: 10.3.1.18
mysql slave 主机:10.3.1.23
虚ip:10.3.1.56
2) keepalived master(10.3.1.24)配置:
global_defs {
lvs_id LVS_HA_MASTER
}
vrrp_sync_group DBGROUP {
group {
VI_MYSQL
}
}
vrrp_instance VI_MYSQL {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 59
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.3.1.56
}
}
virtual_server 10.3.1.56 3306 {
delay_loop 3
lb_algo wlc
lb_kind DR
# persistence_timeout 20
protocol TCP
sorry_server 10.3.1.23 3306
real_server 127.0.0.1 3306 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
3) mysql master realserver(10.3.1.24)脚本:
#!/bin/bash
#desciription : start realserver
MYSQLHA=10.3.1.56
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $MYSQLHA broadcast $MYSQLHA netmask 255.255.255.255 up
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
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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 "Usage: $0 {start|stop}"
exit 1
esac
4) keepalived backup(10.3.1.18)配置:
global_defs {
lvs_id LVS_HA_BACKUP #注意这儿与master不同
}
vrrp_sync_group DBGROUP {
group {
VI_MYSQL
}
}
vrrp_instance VI_MYSQL {
state BACKUP #注意这儿,与master不同
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 59
priority 100 #注意这儿,与master不同
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.3.1.56
}
}
virtual_server 10.3.1.56 3306 {
delay_loop 3
lb_algo wlc
lb_kind DR
# persistence_timeout 20
protocol TCP
sorry_server 10.3.1.23 3306
real_server 10.3.1.24 3306 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
5) mysql slave realserver(10.3.1.23)脚本:
#!/bin/bash
#desciription : start realserver
MYSQLHA=10.3.1.56
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $MYSQLHA broadcast $MYSQLHA netmask 255.255.255.255 up
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
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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 "Usage: $0 {start|stop}"
exit 1
esac
6). 防火墙配置:
vi /etc/sysconfig/iptables
add:
-I INPUT -i eth0 -d 224.0.0.0/8 -j ACCEPT
-A INPUT -p 112 -i eth0 -j ACCEPT
-A OUTPUT -p 112 -o eth0 -j ACCEPT
保存后重启:service iptables restart
7).启动
keepalive master: service keepalived start
mysql master: /usr/local/bin/realserver start
调用ip add 命令会看到:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 10.3.1.56/32 brd 10.3.1.56 scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether f4:ce:46:b4:35:40 brd ff:ff:ff:ff:ff:ff
inet 10.3.1.24/8 brd 10.255.255.255 scope global eth0
inet 10.3.1.56/32 scope global eth0
inet6 fe80::f6ce:46ff:feb4:3540/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
link/ether f4:ce:46:b4:35:44 brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
keepalived backup启动: service keepalived start
调用ip add 命令会看到:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether f4:ce:46:89:63:98 brd ff:ff:ff:ff:ff:ff
inet 10.3.1.18/8 brd 10.255.255.255 scope global eth0
inet6 fe80::f6ce:46ff:fe89:6398/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
link/ether f4:ce:46:89:63:9c brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
注意不会绑定任何ip
msyql slave: /usr/local/bin/realserver start
调用ip add 命令会看到:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet 10.3.1.56/32 brd 10.3.1.56 scope global lo:0
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether f4:ce:46:b4:31:58 brd ff:ff:ff:ff:ff:ff
inet 10.3.1.23/8 brd 10.255.255.255 scope global eth0
inet6 fe80::f6ce:46ff:feb4:3158/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
link/ether f4:ce:46:b4:31:5c brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0