[root@server12 ~]# sh test.sh
[root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep
'used_memory_peak_human|db1:keys'
used_memory_peak_human:26.78M
db1:keys=249925,expires=0
九:主实例角色的恢复过程,使用shell脚本自动恢复
[root@server11 ~]# ssh-keygen
[root@server11 ~]# cd .ssh/
[root@server11 .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.1.113
[root@server11 ~]# cat /usr/local/scripts/recover_mastart.sh
#!/bin/sh
ALIVE=$(/usr/local/redis2/bin/redis-cli -h 192.168.1.113 -p 6379 -a 123 PING)
MDB=/usr/local/redis2/master_dump.rdb
SDB=/usr/local/redis2/slave_dump.rdb
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
scp root@192.168.1.113:$SDB $MDB
else
echo $ALIVE
exit 1
fi
/usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
/usr/local/keepalived/sbin/keepalived -D -f
/usr/local/keepalived/etc/keepalived/keepalived.conf
[root@server11 ~]# chmod +x /usr/local/scripts/recover_mastart.sh
[root@server11 ~]# sh /usr/local/scripts/recover_mastart.sh
十:验证数据完整性和主从角色恢复情况
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep 'used_memory_peak_human|db1:keys'
used_memory_peak_human:26.78M
db1:keys=249925,expires=0
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |grep -A 3 'Replication'
# Replication
role:master
connected_slaves:1
slave0:192.168.1.113,6379,online
[root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 info |grep -A 3 'Replication'
# Replication
role:slave
master_host:192.168.1.112
master_port:6379
[root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.120 -a 123 info |egrep 'used_memory_peak_human|db1:keys'
used_memory_peak_human:26.78M
db1:keys=249925,expires=0
主实例keepalive日志:
[root@server11 ~]# tail -f /var/log/messages
Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Script(Monitor_redis) succeeded
Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Transition to MASTER STATE
Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Received higher prio advert
Dec 12 10:08:13 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Entering BACKUP STATE
Dec 12 10:08:15 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) forcing a new MASTER election
Dec 12 10:08:16 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Transition to MASTER STATE
Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Entering MASTER STATE
Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) setting protocol VIPs.
Dec 12 10:08:17 server11 Keepalived_healthcheckers[20230]: Netlink reflector reports IP 192.168.1.120 added
Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
Dec 12 10:08:17 server11 Keepalived_vrrp[20231]: Netlink reflector reports IP 192.168.1.120 added
Dec 12 10:08:17 server11 avahi-daemon[4519]: Registering new address record for 192.168.1.120 on eth0.
[root@server11 ~]# ip a |grep 192
inet 192.168.1.112/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.120/32 scope global eth0
从实例keepalive日志:
[root@server12 ~]# tail -f /var/log/messages
Dec 12 09:56:01 server12 last message repeated 4 times
Dec 12 10:08:13 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Received lower prio advert, forcing new election
Dec 12 10:08:13 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Sending gratuitous ARPs on eth0 for 192.168.1.120
Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Received higher prio advert
Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) Entering BACKUP STATE
Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: VRRP_Instance(VI_1{) removing protocol VIPs.
Dec 12 10:08:15 server12 Keepalived_healthcheckers[3106]: Netlink reflector reports IP 192.168.1.120 removed
Dec 12 10:08:15 server12 Keepalived_vrrp[3108]: Netlink reflector reports IP 192.168.1.120 removed
Dec 12 10:08:15 server12 avahi-daemon[2921]: Withdrawing address record for 192.168.1.120 on eth0.
从实例角色转换日志:
[root@server12 ~]# tail -f /usr/local/redis2/var/keepalived-redis-state.log
[backup]
Wed Dec 12 10:08:15 CST 2012
Being slave....
Run SLAVEOF cmd ...
OK
下面关于Redis的文章您也可能喜欢,不妨参考下: