哨兵:
原理:
在主从基础上再加上一个监控实时对服务器状态进行看管。
但主服务器宕机后再次上线则不能再变为主服务器,只能作为从服务器。
配置文件:
[root@rhel7 ~]# vim /etc/redis/sentinel.conf
#内容(只在主服务器上配置)
69行 sentinel monitor mymaster 192.168.110.138 6379 2
开启服务器:
[root@rhel7 ~]# redis-server /etc/redis/redis.conf
[root@redis2 ~]# redis-server /etc/redis/redis.conf
[root@redis3 ~]# redis-server /etc/redis/redis.conf
开启哨兵:
#主上开启:
[root@rhel7 ~]# redis-sentinel /etc/redis/sentinel.conf
在从上指定主服务器:
[root@redis2 ~]# redis-cli -h 192.168.110.140
192.168.110.140:6379> SLAVEOF 192.168.110.138 6379
OK
192.168.110.141:6379> SLAVEOF 192.168.110.138 6379
OK
主服务器上的变化
在主上设置键:
192.168.110.138:6379> set sent 'send msg'
OK
192.168.110.140:6379> keys *
1) "sent"
2) "words"
192.168.110.141:6379> keys *
1) "words"
2) "sent"
关闭主服务器验证哨兵:
192.168.110.138:6379> SHUTDOWN
192.168.110.140:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.141,port=6379,state=online,offset=49987,lag=1
master_replid:ded5e6de69c12d3358b2909153fc5cb4ce68a699
master_replid2:a714d964ddfc4d616b3a3027d4395b28b094bca5
master_repl_offset:50132
second_repl_offset:48487
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:42248
repl_backlog_histlen:7885
我们发现140变为了主服务器,那么哨兵正常工作。
192.168.110.140:6379> set name 140
OK
192.168.110.141:6379> keys *
1) "words"
2) "name"
3) "sent"