Redis Sentinel
是Redis 的高可用性解决方案,由一个或多个Sentinel
(哨兵)实例组成。它可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,
并在被监视的主服务器进入下线状态时,自动将某个从服务器升级为新的主服务器,它的主要功能如下:
- 监控(Monitoring):
Sentinel
会不断地检查你的主服务器和从服务器是否运作正常;
- 通知(Notification):当被监控的某个 Redis 服务器出现问题时,
Sentinel
可以通过API向管理员或者其他应用程序发送通知。 - 故障迁移:当主服务器不能正常工作时,
Sentinel
会自动进行故障迁移,也就是主从切换。 - 统一的配置管理:连接者询问
sentinel
取得主从的地址。
哨兵的配置主要就是修改sentinel.conf
配置文件中的参数
1. 复制sentinel.conf 文件到/usr/local/replica/master 目录下:
# cp /root/tools/redis-5.0.7/sentinel.conf /usr/local/replica/master
2.编辑 sentinel.conf
主要修改4个地方:
1)修改哨兵实例运行的端口
port 26379
2) 是否设置后台启动
daemonize:yes
3) 配置哨兵sentinel的日志文件
logfile "/usr/local/sentinel/26379.log"
4)配置哨兵sentinel监控的redis主节点的端口和地址, 2 代表两个哨兵都监听到了
sentinel monitor mymaster 127.0.0.1 6379 2
从节点:
1. 复制sentinel.conf 文件到/usr/local/replica/slave1 目录下:
# cp /root/tools/redis-5.0.7/sentinel.conf /usr/local/replica/slave1
2.编辑 sentinel.conf
主要修改4个地方:
1)修改哨兵实例运行的端口
port 26380
2) 是否设置后台启动
daemonize:yes
3) 配置哨兵sentinel的日志文件
logfile "/usr/local/sentinel/26380.log"
4)配置哨兵sentinel监控的redis主节点的端口和地址, 2 代表两个哨兵都监听到了
sentinel monitor mymaster 127.0.0.1 6379 2
同理配置 slave2
测试:
首先启动Redis主从:
cd /usr/local/replica/master ./redis-server redis.conf
cd /usr/local/replica/slave1 ./redis-server redis.conf
cd /usr/local/replica/slave2 ./redis-server redis.conf
启动3个哨兵:
cd /usr/local/replica/master ./redis-sentinel sentinel.conf
cd /usr/local/replica/slave1 ./redis-sentinel sentinel.conf
cd /usr/local/replica/slave2 ./redis-sentinel sentinel.conf
查Redis 进程:
# ps axu|grep redis

三个Redis和三个哨兵都已启动。
进入slave1的客户端查询:
# cd /usr/local/replica/slave1 redis-cli -p 6380
# info replication
当前角色为salve;
杀掉主节点,
# kill -9 26430
再次查看slave1的角色:
可以看到,由于master停止服务,slave1 自动切换到主节点,Sentinel 会自动进行故障迁移,实现主从切换。