哨兵模式:用于监听主库,当确定主库宕机之后,从备库(从库)中选举一个转备为主。如果要使用哨兵模式,首先要实现三个redis实例之间的主从配置。

一、配置哨兵模式

创建并开启三个哨兵,拷贝sentinel.conf文件三份到新创建的sentinelconf文件夹中;sentinel-26380.conf  sentinel-26381.conf  sentinel-26382.conf;

#创建sentinelconf文件夹
[root@localhost redis-5.0.7]# mkdir sentinelconf

#复制sentinel.conf 文件对其进行去除注释行和空格行
[root@localhost sentinelconf]# cat sentinel.conf | grep -v "#" | grep -v "^$"> sentinelconf/sentinel-26380.conf

修改sentinel-26380.conf 文件;修改后的文件内容如下:

redis哨兵状态查看 redis 哨兵命令_sed

 将修改后的sentinel-26380.conf文件将26380改成26381、26382复制两份

[root@localhost sentinelconf]# sed 's/26380/26381/g' sentinel-26380.conf>sentinel-26381.conf


[root@localhost sentinelconf]# sed 's/26380/26381/g' sentinel-26380.conf>sentinel-26382.conf

修改这三个哨兵的配置文件,设置哨兵监听

sentinel down-after-milliseconds mymaster 30000

redis哨兵状态查看 redis 哨兵命令_配置文件_02

 二、测试哨兵模式

使用指令‘redis-server /usr/local/redis-5.0.7/msconf/redis-slave1.conf
’开启三个redis实例通过三个主从配置文件

redis哨兵状态查看 redis 哨兵命令_配置文件_03

使用指令 ‘redis-sentinel /usr/local/redis-5.0.7/sentinelconf/sentinel-26382.conf
’ 启动三个sentinel哨兵

redis哨兵状态查看 redis 哨兵命令_sed_04

关闭redis-master主实例, 30s可以看到哨兵开始工作,选取6381作为主实例了,即配置完成

redis哨兵状态查看 redis 哨兵命令_redis_05