1)通过Mysql客户端通过VIP连接,看是否连接成功。 比如,在远程一台测试机上连接,通过vip地址可以正常连接(下面的连接权限要是在服务端提前授权的) [root@dev-new- test ~] # mysql -h182.148.15.236 -uroot -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 60 Server version: 5.6.35-log Source distribution Copyright (c) 2000, 2013, Oracle and /or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and /or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select * from huanqiu.haha; +----+-----------+ | id | name | +----+-----------+ | 1 | 王士博 | | 2 | 郭慧慧 | | 3 | 周正 | | 4 | 李敏 | +----+-----------+ 4 rows in set (0.00 sec) 2)默认情况下,vip是在master1上的。使用 "ip addr" 命令查看vip切换情况 [root@master1 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff inet 182.148.15.238 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /32 scope global eth0 // 这个32位子网掩码的vip地址表示该资源目前还在master1机器上 inet 182.148.15.236 /27 brd 82.48.115.255 scope global secondary eth0:0 inet6 fe80::5054:ff:fe3c:2542 /64 scope link valid_lft forever preferred_lft forever 停止master1机器上的mysql服务,根据配置中的脚本,mysql服务停了,keepalived也会停,从而vip资源将会切换到master2机器上。(mysql服务没有起来的时候,keepalived服务也无法顺利启动!) [root@master1 ~] # /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! [root@master1 ~] # ps -ef|grep mysql root 25812 21588 0 17:30 pts /0 00:00:00 grep mysql [root@master1 ~] # ps -ef|grep keepalived root 25814 21588 0 17:30 pts /0 00:00:00 grep keepalived [root@master1 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff inet 182.148.15.238 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /27 brd 82.48.115.255 scope global secondary eth0:0 inet6 fe80::5054:ff:fe3c:2542 /64 scope link valid_lft forever preferred_lft forever 如上结果,发现32位子网掩码的vip没有了,说明此时vip资源已不在master1机器上了 查看下master1的系统日志,如下,会发现vip资源已经切换走了 [root@master1 ~] # tail -f /var/log/messages Apr 15 17:17:43 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: VRRP_Instance(VI_1) Sending /queueing gratuitous ARPs on eth0 for 182.148.15.236 Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:17:48 localhost Keepalived_vrrp[23037]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:39 localhost Keepalived_healthcheckers[23036]: Stopped Apr 15 17:30:39 localhost Keepalived_vrrp[23037]: VRRP_Instance(VI_1) sent 0 priority Apr 15 17:30:39 localhost Keepalived_vrrp[23037]: VRRP_Instance(VI_1) removing protocol VIPs. 再到master2机器上,发现vip资源的确切换过来了 [root@master2 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff inet 182.148.15.237 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /32 scope global eth0 inet6 fe80::5054:ff:fe95:1f6d /64 scope link valid_lft forever preferred_lft forever 查看master2的系统日志 [root@master2 ~] # tail -f /var/log/messages Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Sending /queueing gratuitous ARPs on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 3)再次启动master1的mysql和keepalived服务。(注意:如果restart重启mysql,那么还要启动下keepalived,因为mysql重启,根据脚本会造成keepalived关闭) 注意:一定要先启动mysql服务,然后再启动keepalived服务。如果先启动keepalived服务,按照上面的配置,mysql没有起来,就会自动关闭keepalived。 [root@master1 ~] # /etc/init.d/mysql start Starting MySQL.. SUCCESS! [root@master1 ~] # /etc/init.d/keepalived start 正在启动 keepalived: [确定] 启动这两个服务器后,稍微等过一会儿,注意观察会发现vip资源再次从master2机器上切换回来了。 [root@master1 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff inet 182.148.15.238 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /32 scope global eth0 inet 182.148.15.236 /27 brd 82.48.115.255 scope global secondary eth0:0 inet6 fe80::5054:ff:fe3c:2542 /64 scope link valid_lft forever preferred_lft forever [root@master1 ~] # tail -f /var/log/messages Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:41 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: VRRP_Instance(VI_1) Sending /queueing gratuitous ARPs on eth0 for 182.148.15.236 Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:46 localhost Keepalived_vrrp[27002]: Sending gratuitous ARP on eth0 for 182.148.15.236 再看看master2机器,发现vip资源又被恢复后的master1抢过去了 [root@master2 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff inet 182.148.15.237 /27 brd 182.148.15.255 scope global eth0 inet6 fe80::5054:ff:fe95:1f6d /64 scope link valid_lft forever preferred_lft forever [root@master2 ~] # tail -f /var/log/messages Apr 15 17:30:41 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Sending /queueing gratuitous ARPs on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:30:46 localhost Keepalived_vrrp[8731]: Sending gratuitous ARP on eth0 for 182.148.15.236 Apr 15 17:40:41 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Received advert with higher priority 101, ours 99 Apr 15 17:40:41 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) Entering BACKUP STATE Apr 15 17:40:41 localhost Keepalived_vrrp[8731]: VRRP_Instance(VI_1) removing protocol VIPs. 4)同样,关闭master1机器的keepalived服务,vip资源会自动切换到master2机器上。当master1的keepalived服务恢复后,会将vip资源再次切回来。 [root@master1 ~] # /etc/init.d/keepalived stop 停止 keepalived: [确定] [root@master1 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff inet 182.148.15.238 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /27 brd 82.48.115.255 scope global secondary eth0:0 inet6 fe80::5054:ff:fe3c:2542 /64 scope link valid_lft forever preferred_lft forever 查看master2,发现vip切过来了 [root@master2 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff inet 182.148.15.237 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /32 scope global eth0 inet6 fe80::5054:ff:fe95:1f6d /64 scope link valid_lft forever preferred_lft forever 再次恢复master1的keepalived服务,发现vip资源很快油切回来了。 [root@master1 ~] # /etc/init.d/keepalived start 正在启动 keepalived: [确定] [root@master1 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:3c:25:42 brd ff:ff:ff:ff:ff:ff inet 182.148.15.238 /27 brd 182.148.15.255 scope global eth0 inet 182.148.15.236 /32 scope global eth0 inet 182.148.15.236 /27 brd 82.48.115.255 scope global secondary eth0:0 inet6 fe80::5054:ff:fe3c:2542 /64 scope link valid_lft forever preferred_lft forever 在此查看master2,发现vip资源被切走了 [root@master2 ~] # ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 52:54:00:95:1f:6d brd ff:ff:ff:ff:ff:ff inet 182.148.15.237 /27 brd 182.148.15.255 scope global eth0 inet6 fe80::5054:ff:fe95:1f6d /64 scope link valid_lft forever preferred_lft forever 以上在vip资源切换过程中,对于客户端连接mysql(使用vip连接)来说几乎是没有任何影响的。 |