Redis Sentinel(哨兵模式)配置

前言:

     redis在主从模式下,如果master节点出现故障,redis不会进行切换主节点。如果要测试redis sentinel,首先要启动redis主从,看我的另一篇博客。

     redis sentinel主要功能:监控redis的运行状态,若master节点出现问题,能够选举一个slave为主节点,并修改其他从节点的主节点地址。单个sentinel是不可靠的,sentinel也是支持集群的,因为50%以上投票才生效,所以至少要3个sentinel节点。

配置:

port端口号

bind 192.168.94.151    和redis的bind一样

daemonize yes 后台运行

sentinel monitor mymaster 192.168.94.151 7051 2     mymaster为自己起的集群的名字,192.168.94.151 master的ip   7051master的端口号  2 当有两个sentinel节点认为主节点出问题是,就可以发起failover,如果50%以上节点同意,就切换主节点成功

sentinel auth-pass mymaster xuhaixing  redis主节点的密码

sentinel down-after-milliseconds <master-name> <milliseconds>  在一定的时间内,没有PING通master节点,这个节点就认为master已经down,将其标记为主观下线

sentinel failover-timeout <master-name> <milliseconds>  在该时间内未完成failover,则failover失败

sentinel parallel-syncs <master-name> <numslaves>  在执行故障转移时最多可以有多少个从节点同步新的主master

sentinel notification-script <master-name> <script-path> 在异常时可以执行调用脚本

启动redis(主从部署看我另一篇文章)

[root@localhost redis-replicate]# redis-cli -h 192.168.94.151 -p 7051 -a xuhaixing
192.168.94.151:7051> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.94.151,port=7052,state=online,offset=56,lag=1
slave1:ip=192.168.94.151,port=7050,state=online,offset=56,lag=1
master_replid:dd924d2fb0001b41eca962f2030aa4694d572235
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:56
192.168.94.151:7051> 

启动sentinel

[root@localhost redis-replicate]# cat sentinel-start.sh 
cd /usr/local/redis-replicate/7050
./redis-sentinel ./sentinel.conf
cd /usr/local/redis-replicate/7051
./redis-sentinel ./sentinel.conf
cd /usr/local/redis-replicate/7052
./redis-sentinel ./sentinel.conf
[root@localhost redis-replicate]# sh sentinel-start.sh 
1764:X 26 Jul 06:08:53.076 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1764:X 26 Jul 06:08:53.076 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=1764, just started
1764:X 26 Jul 06:08:53.076 # Configuration loaded
1766:X 26 Jul 06:08:53.099 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1766:X 26 Jul 06:08:53.099 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=1766, just started
1766:X 26 Jul 06:08:53.099 # Configuration loaded
1771:X 26 Jul 06:08:53.122 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1771:X 26 Jul 06:08:53.122 # Redis version=4.0.0, bits=64, commit=00000000, modified=0, pid=1771, just started
1771:X 26 Jul 06:08:53.122 # Configuration loaded
[root@localhost redis-replicate]# ps -ef|grep redis
root       1005      1  0 04:44 ?        00:00:07 /usr/local/redis/bin/redis-server 0.0.0.0:6379
root       1741      1  0 06:06 ?        00:00:00 ./redis-server 192.168.94.151:7050
root       1746      1  0 06:06 ?        00:00:00 ./redis-server 192.168.94.151:7051
root       1751      1  0 06:06 ?        00:00:00 ./redis-server 192.168.94.151:7052
root       1765      1  0 06:08 ?        00:00:00 ./redis-sentinel 192.168.94.151:27050 [sentinel]
root       1770      1  0 06:08 ?        00:00:00 ./redis-sentinel 192.168.94.151:27051 [sentinel]
root       1775      1  0 06:08 ?        00:00:00 ./redis-sentinel 192.168.94.151:27052 [sentinel]
root       1783   1537  0 06:10 pts/0    00:00:00 grep --color=auto redis
[root@localhost redis-replicate]# 
[root@localhost redis-replicate]# redis-cli -h 192.168.94.151 -p 27051 -a xuhaixing
192.168.94.151:27051> info sentinel
# 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=192.168.94.151:7051,slaves=2,sentinels=3

可以通过kill -9 进程号,干掉redis主节点,等一会,再重新查集群信息,会发现主节点已经变了

如果因为网络中断,会出现两个master的原因,可通过该redis配置

min-slaves-to-write 1
min-slaves-max-lag 10

min-slaves-to-write表示至少一个从库时主库才可以写,否则返回错误

min-slaves-max-lag表示允许从库失去连接的时间

猜你喜欢

转载自blog.csdn.net/u012326462/article/details/81210447