一、主从 + Sentinel 哨兵模式
Redis Sentinel是Redis官方的高可用性解决方案。
Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。
虽然 Redis Sentinel 释出为一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器。
此种模式下,客户端要访问的 服务 IP 不是主节点,而是 sentiner 服务器的 IP。
架构图:


1、在一台虚拟机ip: 10.0.104.163上配置一主两从
主库
vim master-6380.conf
port 6380
protected-mode no
daemonize yes
pidfile "/var/run/redis-6380.pid"
logfile "/redis/data/redis-6380.log"
dir "/redis/data"
从库1
vim slave-6381.conf
port 6381
protected-mode no
daemonize yes
pidfile "/var/run/redis-6381.pid"
logfile "/redis/data/redis-6381.log"
dir "/redis/data"
slaveof 172.16.153.180 6380
从库2
vim slave-6382.conf
port 6382
protected-mode no
daemonize yes
pidfile "/var/run/redis-6382.pid"
logfile "/redis/data/redis-6382.log"
dir "/redis/data"
slaveof 172.16.153.180 6380
启动服务
[root@d-163 test-2]# redis-server master-6380.conf
[root@d-163 test-2]# redis-server slave-6381.conf
[root@d-163 test-2]# redis-server slave-6382.conf
2、在10.0.104.124上配置哨兵
[root@d-163 test-2]# vim sentinel-27000.conf
port 27000
dir "/redis/data"
daemonize yes
protected-mode no
logfile "27000.log"
sentinel monitor mymaster 10.0.104.163 6380 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
[root@d-163 test-2]# vim sentinel-27001.conf
port 27001
dir "/redis/data"
daemonize yes
protected-mode no
logfile "27001.log"
sentinel monitor mymaster 10.0.104.163 6380 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
[root@d-163 test-2]# vim sentinel-27002.conf
port 27002
dir "/redis/data"
daemonize yes
protected-mode no
logfile "27002.log"
sentinel monitor mymaster 10.0.104.163 6380 2
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
启动哨兵
[root@mycat-e-124 test-2]# redis-sentinel sentinel-27000.conf
[root@mycat-e-124 test-2]# redis-sentinel sentinel-27001.conf
[root@mycat-e-124 test-2]# redis-sentinel sentinel-27002.conf
查看哨兵
[root@mycat-e-124 test-2]# redis-cli -p 27000 info sentinel
查看主服务器信息
[root@d-163 test-2]# redis-cli -p 6380 info replication
[root@d-163 test-2]# redis-cli -p 6381
127.0.0.1:6381> shutdown #使此服务停止
网友评论