Redis哨兵模式基本实现

哨兵模式:是基于主从模式的,所有主从的优点,哨兵模式都具有。主要作用是当master节点挂掉之后,哨兵会在其余的slave节点中选择一个成为新的master。这次仍然在上一篇Redis单机多实例集群部署的基础上进行配置:
redis的安装目录里有个sentinel.conf文件,这是哨兵的主要配置文件,复制到6379,7000,7001,7002的文件夹里,并修改内容:

port 26379  #哨兵端口号
daemonize yes #守护进程
pidfile "/usr/local/redis-cluster/6379/redis-sentinel.pid"
sentinel monitor mymaster 127.0.0.1 6379 2   #主节点信息,2两个slave认为master节点down了,将重新确定新的master,主节点命名为mymaster
sentinel down-after-milliseconds mymaster 5000 #哨兵程序每5秒检测一次Master是否正常
sentinel failover-timeout mymaster 18000 
protected-mode no

7000,7001,7002三个节点只需要把6379和26379改为各自的端口,其余的不用改。
启动四个节点及其哨兵,在各自的目录中执行命令:

redis-server redis.conf 
redis-sentinel sentinel.conf

通过26379的哨兵,查看当前几个节点的主从信息:

[root@localhost 6379]# redis-cli -c -p 26379
127.0.0.1:26379> info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=3,sentinels=4

由上面可已看出,当前的master节点为6379,slave节点共有三个:

[root@localhost 6379]# redis-cli -c -p 26379
127.0.0.1:26379> info
# Replication
role:master
connected_slaves:3
slave0:ip=127.0.0.1,port=7001,state=online,offset=15750927,lag=0
slave1:ip=127.0.0.1,port=7002,state=online,offset=15750927,lag=1
slave2:ip=127.0.0.1,port=7000,state=online,offset=15750927,lag=1
master_replid:3c35026ef13692ac51ac85b5cb02f17600cd89a0
master_replid2:4ee6988b0ef7c6a64353fb6c8bbff5c0fd87d41e
master_repl_offset:15750927
second_repl_offset:15218224
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15168427
repl_backlog_histlen:582501

测试哨兵是否生效:
将6379节点停掉,查看是否有新的master节点:

[root@localhost 6379]# redis-cli -c -p 6379
127.0.0.1:6379> shutdown
not connected>
#连接7000节点的哨兵
[root@localhost 6379]# redis-cli -c -p 27000
127.0.0.1:27000>info
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=127.0.0.1:7000,slaves=3,sentinels=4

由上可看到,主节点漂移到7000节点上。再连接7000节点:

[root@localhost 6379]# redis-cli -c -p 7000
127.0.0.1:7000>info
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=7001,state=online,offset=15825689,lag=0
slave1:ip=127.0.0.1,port=7002,state=online,offset=15825689,lag=1

由上可看到,7000节点的角色是master,slave节点由3个减少为2个。
再把6379节点拉起,并查看信息

[root@localhost 6379]# redis-cli -c -p 6379
127.0.0.1:6379>info
# Replication
role:slave
master_host:127.0.0.1
master_port:7000
master_link_status:up

可以看到6379节点已经成为slave。
以上,哨兵基本配置完成。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dustin.Hoffman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值