Redis3.2哨兵集群搭建
环境
window7 64 位
Redis-x64-3.2.100
主从机器配置
单机模拟1主1从
127.0.0.1:2379 从
127.0.0.1:2380 主
哨兵机器配置
单机模拟3哨兵集群
127.0.0.1:26379
127.0.0.1:26380
127.0.0.1:26381
redis文件结构
从节点redis2379.conf配置
增加以下内容:
requirepass "654321"
#关闭保护模式
protected-mode no
# 同步数据授权,节点的密码,主从节点密码一致
masterauth "654321"
# 是127.0.0.1 6380 的从服务器
slaveof 127.0.0.1 6380
主节点redis2380.conf配置
增加以下内容:
requirepass "654321"
#关闭保护模式
protected-mode no
# 同步数据授权,节点的密码,主从节点密码一致
masterauth "654321"
哨兵redis26379.conf配置
把redis.conf文件原内容清空后,写入以下内容:
port 26379
protected-mode no
# Sentinel monitor <name> <ip> <port> <quorum>
# name :redis主服务名称,可以自行命名
# quorum :表示要将这个主服务器判断为失效并下线至少需要2个sentinel同意
# 只需要配置master信息
sentinel monitor s1 127.0.0.1 6380 2
sentinel down-after-milliseconds s1 60000
sentinel auth-pass s1 654321
哨兵redis26380.conf配置
配置同26379,把端口改掉
port 26380
哨兵redis26381.conf配置
配置同26379,把端口改掉
port 26381
启动主从
redis-server.exe redis6380.conf
redis-server.exe redis6379.conf
启动哨兵集群
redis-server.exe redis26379.conf --sentinel
redis-server.exe redis26380.conf --sentinel
redis-server.exe redis26381.conf --sentinel
原理
当哨兵发现master2380节点掉线时,会动态修改从节点2379的配置文件,此时2379变成主节点
# 移除
slaveof 127.0.0.1 6380
当2380在次上线时,会动态修改节点2380的配置文件,此时2380变成从节点
# 增加
slaveof 127.0.0.1 6379
哨兵配置文件也会变化
提示主节点由6380 变成 6379