[root@keepalived01 ~]# ipvsadm -L IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.244.10:8066 rr persistent 50 -> 192.168.244.148:8066 Route 1 0 0 -> 192.168.244.149:8066 Route 1 0 3
原来,这个与配置文件中的persistence_timeout有关,指的是在一定的时间内来自同一IP的连接将会被转发到同一realserver中。而不是严格意义上的轮询。默认为50s,具体的失效时间还是比较复杂,可参考:LVS持久性工作原理和配置
在这里,为了观察到明显的轮询效果。将该值设为0。重启keepalived服务。
重新测试,结果如下
[root@mysql-server1 ~]# mysql -h192.168.244.10 -P8066 -utest -ptest -DTESTDB -e 'select @@hostname' Warning: Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mycat02 | +------------+ [root@mysql-server1 ~]# mysql -h192.168.244.10 -P8066 -utest -ptest -DTESTDB -e 'select @@hostname' Warning: Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mycat01 | +------------+ [root@mysql-server1 ~]# mysql -h192.168.244.10 -P8066 -utest -ptest -DTESTDB -e 'select @@hostname' Warning: Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mycat02 | +------------+ [root@mysql-server1 ~]# mysql -h192.168.244.10 -P8066 -utest -ptest -DTESTDB -e 'select @@hostname' Warning: Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mycat01 | +------------+
实现了严格意义上的轮询,ipvsadm -L的输出结果如下:
[root@keepalived01 ~]# ipvsadm -L IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.244.10:8066 rr -> 192.168.244.148:8066 Route 1 0 2 -> 192.168.244.149:8066 Route 1 0 2
每个节点对应2个连接,与上述通过mysql客户端得到的结果相符。
测试LVS的高可用
手动断开LVS-DR-Master节点的Keepalived服务
[root@keepalived01 ~]# service keepalived stop Redirecting to /bin/systemctl stop keepalived.service [root@keepalived01 ~]# ipvsadm -L IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
可以发现,mysql客户端依旧可以通过mycat连接到mysql服务端
[root@mysql-server1 ~]# mysql -h192.168.244.10 -P8066 -utest -ptest -DTESTDB -e 'select @@hostname' Warning: Using a password on the command line interface can be insecure. +------------+ | @@hostname | +------------+ | mycat02 | +------------+
重启LVS-DR-Master节点的Keepalived服务后,不难检测出keepalived01又重新作为了Master节点(因为BACKUP节点的persistence_timeout没有修改,所以可根据轮询的特点来判断哪个节点是master节点)。
测试Keepalived对于MyCAT服务的监控检测
手动关闭mycat01节点的mycat服务
[root@mycat01 ~]# cd /usr/local/mycat/ [root@mycat01 mycat]# bin/mycat stop Stopping Mycat-server... Stopped Mycat-server.
通过LVS-DR-Master节点节点上的ipvsadm -L可以看出VIP的映射关系中已剔除了mycat01节点。