Redis Sentinel 架构配置!!!

之前我们讲了主从复制,可以有效的提高Redis 的性能,Slave 节点可以备份 Master 节点的数据,可以对请求进行分流,读写分离,减轻主节点的压力。

主从复制是否真的高可用了?

主从复制都是建立在主节点的,万一 Master 挂掉了,那么从节点就不能实时更新数据了。因此当我们主节点挂掉了,我们会怎么处理了:

  1. 需要把其中的一个Slave节点,升级为 Master。
  2. 需要把 Slave 挂载到 新的 Master 的IP 上。
  3. 需要把新的Master 节点 和 Slave 的节点IP ,通知给客户端。
  4. 客户端链接新的 更改好的对应的 主从节点。

从而得出结论主从复制也不是很靠谱,因此 Redis 推出的 Sentinel 架构。

Redis Sentinel 架构

在这里插入图片描述

  1. 主从复制的架构还是 一个 Master,2个 Slave。
  2. Sentinel 并不是一个,而是多个,他们每个都会去监听每个Redis 的健康状态。
  3. 客户端将不再和Redis直接链接,而是通过Sentinel 来获取信息了。

Redis Sentinel 故障转移流程

  1. 多个Sentinel 发现并确认 Master 有问题。
  2. 选举一个Sentinel 为领导。
  3. 选举一个Slave 为新的 Master。执行 slave no one。
  4. 通知其他的slave ,成为新的Master 的 slave。
  5. 通知链接的客户端主从的变化。
  6. 等待老的Master 复活,成为新的Master 的 Slave。

Redis Sentinel 安装

在这里插入图片描述

  1. 配置节点的Config,redis_7000.conf redis_7001.conf redis_7002.conf,这里的3个配置用的都是极简的配置
root@iZ28x8osxzxZ:/opt/redis/ect# cat redis_7000.conf 
port 7000
daemonize yes
pidfile /var/run/redis_7000.pid
logfile "/home/redisRdbLog/7000.log"
dir /home/redisRdbLog/
root@iZ28x8osxzxZ:/opt/redis/ect# cat redis_7001.conf 
port 7001
daemonize yes
pidfile /var/run/redis_7001.pid
logfile "/home/redisRdbLog/7001.log"
dir /home/redisRdbLog
replicaof 127.0.0.1 7000
root@iZ28x8osxzxZ:/opt/redis/ect# cat redis_7002.conf 
port 7002
daemonize yes
pidfile /var/run/redis_7002.pid
logfile "/home/redisRdbLog/7002.log"
dir /home/redisRdbLog
replicaof 127.0.0.1 7000
  1. Sentinel 的配置,Redis 文件目录下有个 sentinel.conf
    在这里插入图片描述
//查看 sentinel 的配置文件,忽略注释和空格
root@iZ28x8osxzxZ:/opt/redis# cp sentinel.conf /opt/redis/ect/
root@iZ28x8osxzxZ:/opt/redis/ect# cat sentinel.conf | grep -v "#" | grep -v "^$"
port 26379  // 启动sentinel 端口号
daemonize no //是否以守护进程的方式启动
pidfile /var/run/redis-sentinel.pid //启动的PID
dir /tmp //保存的工作目录
logfile "" //日志文件
sentinel monitor mymaster 127.0.0.1 6379 2 //sentinel 监控主节点的名字mymaster,ip 127.0.0.1 端口6379,2的意思是 当有几个 sentinel 觉得这个master 有问题,就进行故障转移,
sentinel down-after-milliseconds mymaster 30000 // ping mymaster 30000 毫秒后,还ping不同,这个就确定有问题
sentinel parallel-syncs mymaster 1 //当有新的master,老的slave会对新的进行复制,1相当于每次只能复制1个
sentinel failover-timeout mymaster 180000 //故障转移的时间
sentinel deny-scripts-reconfig yes //不允许使用SENTINEL SET设置notification-script和client-reconfig-script

配置Sentinel config 文件

root@iZ28x8osxzxZ:/opt/redis/ect# cat sentinel.conf | grep -v "#" | grep -v "^$" > redis-sentinel-26379.conf
root@iZ28x8osxzxZ:/opt/redis/ect# vim redis-sentinel-26379.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# cat redis-sentinel-26379.conf 
port 26379
daemonize yes
dir /home/redisRdbLog/
pidfile /var/run/redis-sentinel-26379.pid
logfile "redis-sentinel-26379.log"
sentinel monitor mymaster 127.0.0.1 7000 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

配置另外2个 redis-sentinel,使用sed 命令 sed “s/替换值/替换后的值”

root@iZ28x8osxzxZ:/opt/redis/ect# sed "s/26379/26380/g" redis-sentinel-26379.conf > redis-sentinel-26380.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# sed "s/26379/26381/g" redis-sentinel-26379.conf > redis-sentinel-26381.conf 

依次检查 26380,26381 的文件配置

root@iZ28x8osxzxZ:/opt/redis/ect# cat redis-sentinel-26380.conf 
port 26380
daemonize yes
dir /home/redisRdbLog/
pidfile /var/run/redis-sentinel-26380.pid
logfile "redis-sentinel-26380.log"
sentinel monitor mymaster 127.0.0.1 7000 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
root@iZ28x8osxzxZ:/opt/redis/ect# cat redis-sentinel-26381.conf 
port 26381
daemonize yes
dir /home/redisRdbLog/
pidfile /var/run/redis-sentinel-26381.pid
logfile "redis-sentinel-26381.log"
sentinel monitor mymaster 127.0.0.1 7000 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

启动 redis-server: 端口为7000 ,7001 ,7002,执行 info replication 看出主从节点的信息

root@iZ28x8osxzxZ:/opt/redis/ect# redis-server redis_7000.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# redis-server redis_7001.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# redis-server redis_7002.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# redis-cli -p 7000 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=7001,state=online,offset=28,lag=1
slave1:ip=127.0.0.1,port=7002,state=online,offset=28,lag=0
master_replid:a448e9c6b7786ece06c3f49e5f0b1d35691a8be2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

启动redis-sentinel,端口为 26379,26380,26381

root@iZ28x8osxzxZ:/opt/redis/ect# redis-sentinel redis-sentinel-26379.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# redis-sentinel redis-sentinel-26380.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# redis-sentinel redis-sentinel-26381.conf 
root@iZ28x8osxzxZ:/opt/redis/ect# cat redis-sentinel-26379.conf 
port 26379
daemonize yes
dir "/home/redisRdbLog"
pidfile "/var/run/redis-sentinel-26379.pid"
logfile "redis-sentinel-26379.log"
sentinel myid b933b102f5d7dc472a14e60b9b72931a262c73fb
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 7000 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
protected-mode no
sentinel known-replica mymaster 127.0.0.1 7001
sentinel known-replica mymaster 127.0.0.1 7002
sentinel known-sentinel mymaster 127.0.0.1 26381 76cd0f4664dc063c307070322f554433725dabff
sentinel known-sentinel mymaster 127.0.0.1 26380 bc6d19b66b8633fa7b9663be8358841cad689da8
sentinel current-epoch 0

我们依次启动了3个sentinel ,我们查看sentinel 端口为26379,发现和我们配置的不一样,还是加入了从节点的信息,这个是因为redis帮我们去掉了一些配置,执行了 info replication 获取了从节点的信息,帮我们加入了。

通过 info sentinel 信息查看是否成功

root@iZ28x8osxzxZ:/opt/redis/ect# redis-cli -p 26379 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=127.0.0.1:7000,slaves=2,sentinels=3

可以看到 name 为 mymaster, 主节点master : 127.0.0.1:7000,slave 有2个,sentinel 有3个,说明配置成功了。

之前准备学习的时候写博客,可是都没有坚持下去,希望这次可以有始有终。
Redis 坚持第一天 :为什么要使用 redis ?
Redis 坚持第二天 :Redis 的安装与启动
Redis 坚持第三天 :Redis 使用配置文件启动,常见配置学习。
Redis 坚持第四天 :

  1. Redis 五种常见的数据结构:String
  2. Redis 五种常见的数据结构:Hash
  3. Redis 五种常见的数据结构:List
  4. Redis 五种常见的数据结构:Set
  5. Redis 五种常见的数据结构:zset

Redis 坚持第五天 :Redis 客户端:Jredis 和 spring-data-redis 整合。
Redis 坚持第六天 :Redis 慢查询日志。
Redis 坚持第七天 :Redis pipeline。
Redis 坚持第八天 :Redis 发布订阅。
Redis 坚持第九天 :Redis bitmp。
Redis 坚持第十天 :Redis HyperLogLogs。
Redis 坚持第十一天 :Redis GEO。
Redis 坚持第十二天 :Redis 持久化:RDB。
Redis 坚持第十三天 :Redis 持久化 :AOF。
Redis 坚持第十四天 :Redis :主从复制。
Redis 坚持第十五天 :Redis Sentinel 架构配置!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java小小星星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值