PS:在使用前请先安装好KeepAlived Redis等相关软件,需要设置为系统服务,但不要设置成为开机启动。
#!/bin/sh
# redis-to-master.sh
rediscli="/home/redis/redis-3.2.0/src/redis-cli"
$rediscli slaveof no one
perl -pi -e 's/^slaveof.*/slaveof no one/' /home/redis/redis-3.2.0/redis.conf
echo $(date "+%Y-%m-%d %H:%M:%S") "the redis is to be the master." >> /home/redis/redis_rdb/redis-keepalived.log
#!/bin/sh
# redis-to-slave.sh
rediscli="/home/redis/redis-3.2.0/src/redis-cli"
PEER_HOST="192.168.80.132"
PEER_PORT=6379
$rediscli slaveof $PEER_HOST $PEER_PORT
perl -pi -e "s/^slaveof.*/slaveof $PEER_HOST $PEER_PORT/" /home/redis/redis-3.2.0/redis.conf
echo $(date "+%Y-%m-%d %H:%M:%S") "the redis is to be the slave." >> /home/redis/redis_rdb/redis-keepalived.log
#!/bin/sh
# redis-check.sh
rediscli="/home/redis/redis-3.2.0/src/redis-cli"
logfile="/home/redis/redis_rdb/redis-keepalived.log"
# echo $(date "+%Y-%m-%d %H:%M:%S") keepaliced checking >> $logfile
for TRIES in `seq 1 3`
do
RESULT=`$rediscli ping`
if [ "${RESULT}" = "PONG" ]; then
exit 0
fi
echo $(date "+%Y-%m-%d %H:%M:%S") "ping failed ${TRIES}" >> $logfile
sleep 1
done
echo $(date "+%Y-%m-%d %H:%M:%S") "redis server was down. shutdown keepalived." >> $logfile
systemctl stop keepalived.service
exit 1
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server localhost
smtp_connect_timeout 30
router_id NodeA
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 100
nopreempt
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.80.120
}
notify_master /home/redis/redis-shell/redis-to-master.sh
notify_backup /home/redis/redis-shell/redis-to-slave.sh
#notify_fault /home/keepshell/notify_fault.sh
#notify_stop /home/keepshell/notify_stop.sh
}
virtual_server 127.0.0.1 16379 {
delay_loop 5
lb_algo rr
real_server 127.0.0.1 6379 {
MISC_CHECK {
misc_path "/home/redis/redis-shell/redis-check.sh"
misc_timeout 30
}
}
}
2016-09-09 17:20:00 the redis is to be the slave.
2016-09-09 17:20:19 the redis is to be the master.
可以看到先执行了 notify_backup 这是因为主服务器上的KeepAlived也设置成了BACKUP模式,keep启动后会先成为BACKUP。然后由于只有它自己,所以它接下来会接管VIP,变成Master。然后执行notify_master
2016-09-09 17:28:11 the redis is to be the slave.
可以看到备服务器启动后KeepAlived执行了 notify_backup 设置redis为备,同时去主服务器同步数据。
package com;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.ClusterNodeInformationParser;
public class RedisDemo {
public static void main(String[] args) throws Exception {
JedisPoolConfig cfg = new JedisPoolConfig();
final JedisPool pool = new JedisPool(cfg, "192.168.80.120", 6379, 8000);
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 0,
TimeUnit.SECONDS, new LinkedBlockingQueue());
for (int i = 0; i < 1000; i++) {
Jedis jedis = null;
try {
jedis = pool.getResource();
System.out.println(jedis.keys("*"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
Thread.sleep(2000);
}
pool.destroy();
}
}
2016-09-09 17:20:00 the redis is to be the slave.
2016-09-09 17:20:19 the redis is to be the master.
2016-09-09 17:34:14 ping failed 1
2016-09-09 17:34:15 ping failed 2
2016-09-09 17:34:16 ping failed 3
2016-09-09 17:34:17 redis server was down. shutdown keepalived.
2016-09-09 17:28:11 the redis is to be the slave.
2016-09-09 17:34:23 the redis is to be the master.