#!/bin/bash #description: Config realserver VIP=192.168.244.10 /etc/rc.d/init.d/functions case "$1" in start) /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP /sbin/route add -host $VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) /sbin/ifconfig lo:0 down /sbin/route del $VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
[root@mycat01 ~]# sh realserver.sh start
[root@mycat02 ~]# sh realserver.sh start
执行ip addr查看VIP是否绑定成功
五、分别在keepalived01和keepalived02上启动Keepalived服务
[root@keepalived01 keepalived]# service keepalived start
[root@keepalived02 keepalived]# service keepalived start
通过ipvsadm -L命令查看VIP是否能成功映射到后端mycat服务。如果失败了,可通过/var/log/messages日志定位keepalived启动失败的原因。
[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
0
[root@keepalived02 keepalived]# 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 0
从输出的结果来看,VIP已成功映射到后端mycat服务,下面,具体测试一下
六、测试MyCAT高可用负载均衡集群的可用性。
测试MyCAT集群的负载均衡
通过mysql -h192.168.244.10 -P8066 -utest -ptest -DTESTDB -e 'select @@hostname'命令来判断当前访问的是哪个mycat节点。
但是很奇怪的时,连续执行三次,每次都是访问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 | +------------+ | 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 | +------------+ | 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 | +------------+ | mycat02 | +------------+
通过ipvsadm -L命令查看这三个连接确实都被转发到mycat02上了。