masterA配置
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER-A
}
vrrp_script mysql {
script "/opt/mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql
}
virtual_ipaddress {
192.168.200.254
}
}
[root@localhost ~]# cat /opt/mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
[root@localhost ~]# chmod +x /opt/mysql.sh
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.254/32 scope global ens32
[root@localhost ~]# tail -f /var/log/messages
Feb 12 17:23:23 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) setting protocol iptable drop rule
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: Sending gratuitous ARP on ens32 for 192.168.200.254
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.200.254
Feb 12 17:23:24 localhost Keepalived_vrrp[70087]: Sending gratuitous ARP on ens32 for 192.168.200.254
masterB配置
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER-B
}
vrrp_script mysql {
script "/opt/mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
mysql
}
virtual_ipaddress {
192.168.200.254
}
}
[root@localhost ~]# cat /opt/mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
[root@localhost ~]# chmod +x /opt/mysql.sh
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# tail -f /var/log/messages
Feb 12 17:28:02 localhost Keepalived_healthcheckers[70075]: WARNING - script '/root/shutdown.sh' is not executable for uid:gid 0:0 - disabling.
Feb 12 17:28:02 localhost Keepalived_healthcheckers[70075]: SECURITY VIOLATION - check scripts are being executed but script_security not enabled.
Feb 12 17:28:02 localhost kernel: ip_set: protocol 6
Feb 12 17:28:02 localhost kernel: IPVS: [wrr] scheduler registered.
Feb 12 17:28:02 localhost Keepalived_healthcheckers[70075]: Activating healthchecker for service [192.168.200.254]:3306
Feb 12 17:28:02 localhost Keepalived_vrrp[70076]: VRRP_Instance(VI_1) removing protocol VIPs.
Feb 12 17:28:02 localhost Keepalived_vrrp[70076]: VRRP_Instance(VI_1) removing protocol iptable drop rule
Feb 12 17:28:02 localhost Keepalived_vrrp[70076]: Using LinkWatch kernel netlink reflector...
Feb 12 17:28:02 localhost Keepalived_vrrp[70076]: VRRP_Instance(VI_1) Entering BACKUP STATE
Feb 12 17:28:02 localhost Keepalived_vrrp[70076]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
1、 测试VIP转移
masterA配置
[root@localhost ~]# systemctl stop mariadb
[root@localhost ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.200.111/24 brd 192.168.200.255 scope global ens32
[root@localhost ~]# ps aux | grep mysql
root 83651 0.0 0.0 112720 980 pts/2 S+ 14:30 0:00 grep --color=auto mysql
masterB配置
[root@localhost ~]# ip a | grep ens32
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.200.112/24 brd 192.168.200.255 scope global ens32
inet 192.168.200.254/32 scope global ens32
[root@localhost ~]# tail -f /var/log/messages
Feb 13 14:30:02 localhost Keepalived_vrrp[79095]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 13 14:30:02 localhost Keepalived_vrrp[79095]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 13 14:30:02 localhost Keepalived_vrrp[79095]: Sending gratuitous ARP on ens32 for 192.168.200.254
Feb 13 14:30:02 localhost Keepalived_vrrp[79095]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens32 for 192.168.200.254
2、 在远程客户端测试
mysql服务器授权
[root@localhost ~]# mysql -uroot
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.200.%' identified by '123456';
MariaDB [(none)]> flush privileges;
通过vip登陆测试
[root@localhost ~]# mysql -uroot -p123456 -h 192.168.200.254
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 5796
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test01 |
| test02 |
+--------------------+
6 rows in set (0.00 sec)