首先在搭建前分析,想用Keepalived实现MySQL高可用,那么先的对Keepalived+lvs跟MySQL主主复制有所了解。因为MySQL+Keepalived架构是在Keepalived+lvs跟MySQL主主复制的基础上实现的lvs+Keepalived跟MySQL主主复制,在前面的博文中已经介绍过了这里不啰嗦了。
我们的目标是,两台MySQL服务器如果其中有一台MySQL服务器挂掉后,另外一台能立马接替其进行工作。因此我们就必须保证两台MySQL数据库的数据完全一样,而且当挂掉的那一台重新启动的话,不再会被客户端继被访问,而是会充当备机跟现在工作的MySQL进行数据同步,一直到提供服务的那台挂掉后再接替其工作。如此周而复始的实现了MySQL的高可用。在通常情况下实现这种模式的Keepalived无疑是最好的选择。因为有虚拟IP的原因,如果有一台MySQL挂掉了,Keepalived会从服务器群中剔除,而客户端访问会被切换到另外一台接替其工作的机器上。
在搭建的时候一些注意事项,MySQL主主复制跟lvs+Keepalived搭建的注意事情前面博文已经提高这里就不再啰嗦。在搭建完毕后,因为arp的原因不能够实现自由切换。这就是要注意的重点。
vim /etc/sysctl.conf
#.............前面部分省去
#######以下是文件末尾添加的部分######
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
##这4行的主要作用是本地arp不作回应##
添加完后保存退出,并执行:sysctl -p使其生效
[root@mysql-ha1 ~]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
下面具体操作,首先有两台mysql机器已经搭建好了mysql的主主复制
vip:192.168.5.55
mysql-ha1:192.168.5.234
mysql-ha2:192.168.5.155
在两台mysql机器上都安装ipvsadm,keepalived,首先在mysql-ha1(192.168.5.234)安装
[root@mysql-ha1 src]# wget
[root@mysql-ha1 src]# wget
[root@mysql-ha1 src]# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
[root@mysql-ha1 src]# yum -y install kernel-devel make gcc openssl-devel libnl* popt*
[root@mysql-ha1 src]# ln -s /usr/src/kernels/2.6.32-220.13.1.el6.x86_64/ /usr/src/linux
[root@mysql-ha1 src]# tar -zxvf ipvsadm-1.26.tar.gz
[root@mysql-ha1 src]# cd ipvsadm-1.26/
[root@mysql-ha1 ipvsadm-1.26]# make
[root@mysql-ha1 ipvsadm-1.26]# make install
[root@mysql-ha1 ipvsadm-1.26]# cd ..
[root@mysql-ha1 src]# tar -zxvf keepalived-1.2.2.tar.gz
[root@mysql-ha1 src]# cd keepalived-1.2.2/
[root@mysql-ha1 keepalived-1.2.2]# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/
[root@mysql-ha1 keepalived-1.2.2]# make && make install
[root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@mysql-ha1 keepalived-1.2.2]# mkdir /etc/keepalived
[root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
在mysql-ha2(192.168.5.155)也同样进行安装
然后我们编辑keepalived配置文件,下面是mysql-ha1(192.168.5.234)上的配置文件内容
[root@mysql-ha1 ~]# >/etc/keepalived/keepalived.conf
[root@mysql-ha1 ~]# vim /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-ha1 #修改为自己的主机名
}
##################第一部分###################
vrrp_instance VI_1 {
state BACKUP #都修改成BACKUP
interface eth0
virtual_router_id 60 #默认51 主从都修改为60
priority 100 #在mysql-ha2上LVS上修改成80
advert_int 1
nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.55
}
}
##################第二部分###################
virtual_server 192.168.5.55 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.5.234 3306 {
weight 1
notify_down /usr/local/mysql/bin/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
connect_port 3306
}
}
}
这里需要注意的是,notify_down /usr/local/mysql/bin/mysql.sh这个选项,这个是是在keepalived检测不到mysql的时候要执行的脚本,从上面的配置文件来看real服务器只有本机。那么,keeaplived如果启动,客户端也只是访问本机的mysql。nopreempt这个选下也得注意,这个是不抢占资源在优先级高的机器上配置就可以。