看下这个脚本的内容:
[root@MySQL-ha1 ~]# vim /usr/local/mysql/bin/mysql.sh
#!/bin/bash
pkill keepalived
[root@mysql-ha1 ~]# chmod +x /usr/local/mysql/bin/mysql.sh
脚本内容就一条命令:pkill keepalived,主要作用是如果本机的mysql挂掉了,那么同时会杀死本机的keepalived,这样另外一台就会接替他工作,虚拟IP也会被另一台接管,如果不杀死keepalived虚拟IP不会被另一台接管,mysql访问也就不会切换过去。
测试下如果,mysql服务器挂掉,脚本是否可以杀死keepalived
[root@mysql-ha1 ~]# ps aux | grep keepalived
root 2518 0.0 0.0 41796 936 ? Ss 19:35 0:00 keepalived -D
root 2519 0.0 0.1 43900 2152 ? S 19:35 0:00 keepalived -D
root 2520 0.0 0.0 43900 1572 ? S 19:35 0:00 keepalived -D
root 2610 0.0 0.0 103240 860 pts/0 S+ 23:07 0:00 grep keepalived
[root@mysql-ha1 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2237/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd
tcp 0 0 :::22 :::* LISTEN 1041/sshd
tcp 0 0 ::1:25 :::* LISTEN 1416/master
[root@mysql-ha1 ~]# service mysqld stop
Shutting down MySQL... SUCCESS!
[root@mysql-ha1 ~]# ps aux | grep keepalived
root 2640 0.0 0.0 103240 856 pts/0 S+ 23:07 0:00 grep keepalived
[root@mysql-ha1 ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd
tcp 0 0 :::22 :::* LISTEN 1041/sshd
tcp 0 0 ::1:25 :::* LISTEN 1416/master
[root@mysql-ha1 ~]#
可以看到当mysqld服务器挂掉后,脚本生效同时杀死本机keepalived使得mysql故障转移。这时另一台keepalived会接替它工作。
同样看下mysql-ha2(192.168.5.155)上keepalived配置文件
[root@mysql-ha2 ~]# vim /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-ha1 #修改为自己的主机名
}
##################第一部分###################
vrrp_instance VI_1 {
state BACKUP #都修改成BACKUP
interface eth1
virtual_router_id 60 #默认51 主从都修改为60
priority 80 #在mysql-ha1上LVS上修改成100
advert_int 1
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.155 3306 {
weight 1
notify_down /usr/local/mysql/bin/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
connect_port 3306
}
}
}