[转帖]Redis 7.0 三节点哨兵(Sentinel)高可用 环境搭建手册

redis,节点,哨兵,sentinel,可用,环境,搭建,手册 · 浏览次数 : 0

小编点评

## Redis sentinel configuration **Sentinel configuration file (redis.conf):** ``` node-id=mymaster primary-port=6379 secondary-port=6379 # Other parameters related to health checks and failover server-id=172.31.185.120 max-connections=1024 socket-keepalive=30 wait-time=15 failure-threshold=3 primary-read-timeout=30 # Master election parameters candidate-filter=match(node-id, 1); voting-period=30 max-votes=1 # Slave parameters slave-id=172.31.185.131 health-check-period=300 health-check-grace-period=30 max-slave-connections=1024 socket-keepalive=30 wait-time=15 failure-threshold=3 master-election-timeout=30 ``` **Key points:** * `node-id`: Unique identifier for the sentinel node. * `primary-port` and `secondary-port`: Ports used for primary and secondary connections. * `server-id`: Identifier for the master node. * `max-connections`: Maximum number of connections allowed to the node. * `socket-keepalive`: Keep connections alive between nodes. * `wait-time`: Time to wait for connections to establish. * `failure-threshold`: Number of failures before considering the node unhealthy. * `candidate-filter` and `voting-period`: How the node chooses a primary and secondary node. * `slave-id`: Identifier for the slave node. * `health-check-period`: Frequency of health checks. * `health-check-grace-period`: Time window after which health checks are performed. * `max-slave-connections`: Maximum number of connections allowed to the slave. * `socket-keepalive`: Keep connections alive between the node and its slaves. * `wait-time`: Time to wait for connections to establish. * `failure-threshold`: Number of failures before considering the slave unhealthy. **Action after killing the master node:** The slaves will automatically vote for the remaining slave to become the new master. The new master will then start a new Redis server instance on another port and join the existing cluster as a slave.

正文

 

2022-06-17 16:253480原创Redis

1 哨兵高可用架构说明


Redis 最早的高可用方案是主从复制,但这种方案存在一个问题,就是当主库宕机后,从库不会自动切成主库,需要人工干预。 所有在主从复制的基础上引入了哨兵模式高可用方案。

关于Redis 的主从架构说明,可以直接参考官方手册:

Redis replication
https://redis.io/docs/manual/replication/
High availability with Redis Sentinel
https://redis.io/docs/manual/sentinel/

哨兵模式是一种特殊的模式,哨兵是一个独立的进程,作为进程,它独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。

 

Redis的哨兵(sentinel) 系统用于管理多个 Redis 服务器,哨兵执行以下三个任务:

  1. 监控(Monitoring): 哨兵(sentinel) 会不断地检查你的Master和Slave是否正常运行。
  2. 提醒(Notification):当被监控的某个Redis出现问题时, 可以通过 API 向管理员或者其他应用程序发送通知。
  3. 自动故障迁移(Automatic failover):当一个Master不能正常工作时,哨兵(sentinel) 会开始一次自动故障迁移操作,它会将失效Master的其中一个Slave升级为新的Master, 并让失效Master的其他Slave改为复制新的Master; 当客户端试图连接失效的Master时,集群也会向客户端返回新Master的地址,使得集群可以使用新的Master代替失效Master。

当一个哨兵进程对Redis服务器进行监控,可能会出现问题,为此可以使用哨兵进行监控, 各个哨兵之间还会进行监控,这就形成了多哨兵模式。

2 搭建哨兵环境


2.1 安装单机Redis

我们这里在3台主机上进行测试:

[dave@www.cndba.cn_1 local]# cat /etc/hosts
127.0.0.1   localhost
172.31.185.120 mongodb1
172.31.185.165 mongodb2
172.31.185.131 mongodb3
[dave@www.cndba.cn_1 local]#

在三台主机上安装好单机Redis,具体操作参考之前的博客,如下:

Linux 7.8 平台 Redis 7 安装并配置开机自启动 操作手册
https://www.cndba.cn/dave/article/108061

2.2 搭建主从复制

哨兵是基于主从复制进行的,所以在配置sentinel之前,需要先配置主从复制。 这个很简单。

我们这这里规划:

172.31.185.120 mongodb1 : Master
172.31.185.165 mongodb2 : Slave
172.31.185.131 mongodb3 : Slave

在2个Slave 节点执行如下命令:

replicaof 172.31.185.120 6379
config set masterauth redis   # 这里因为我们Master启用了密码,所以必须设置


[dave@www.cndba.cn_3 ~]# redis-cli
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> replicaof 172.31.185.120 6379
OK
127.0.0.1:6379> config get masterauth
1) "masterauth"
2) ""
127.0.0.1:6379> config set masterauth redis
OK
127.0.0.1:6379> config rewrite
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.31.185.120
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
……

[dave@www.cndba.cn_2 etc]# redis-cli
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> replicaof 172.31.185.120 6379
OK
127.0.0.1:6379> config set masterauth redis
OK
127.0.0.1:6379> config rewrite
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.31.185.120
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
……

主库查看:
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.31.185.131,port=6379,state=online,offset=252,lag=1
slave1:ip=172.31.185.165,port=6379,state=online,offset=252,lag=0
master_failover_state:no-failover
master_replid:e07445bc0cc0e180c25c3e006af5b765943b2f6e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252
127.0.0.1:6379>

2.3 配置sentinel

Sentinel的配置文件模板在Redis 的安装目录下,我们复制到/etc 目录下:

[dave@www.cndba.cn_1 redis-7.0.1]# pwd
/root/redis-7.0.1
[dave@www.cndba.cn_1 redis-7.0.1]# ll *.conf
-rw-rw-r-- 1 root root 106547 Jun  8 17:56 redis.conf
-rw-rw-r-- 1 root root  13924 Jun  8 17:56 sentinel.conf
[dave@www.cndba.cn_1 redis-7.0.1]#
[dave@www.cndba.cn_1 redis-7.0.1]# cp sentinel.conf /etc/

修改sentinel.conf文件中的如下参数:

###普通配置

port 26379
# 保护模式关闭,这样其他服务起就可以访问此台redis
protected-mode no
# 哨兵模式是否后台启动,默认no,改为yes
daemonize yes
pidfile /var/run/redis-sentinel.pid
# log日志保存位置
logfile /usr/local/redis/sentinel/redis-sentinel.log
# 工作目录
dir /usr/local/redis/sentinel

###核心配置
# 核心配置。
# 第三个参数:哨兵名字,可自行修改。(若修改了,那后面涉及到的都得同步) 
# 第四个参数:master主机ip地址,我们这里以mongodb1 为主节点
# 第五个参数:redis端口号
# 第六个参数:哨兵的数量。比如2表示,当至少有2个哨兵发现master的redis挂了,
#               那么就将此master标记为宕机节点。
#               这个时候就会进行故障的转移,将其中的一个从节点变为master
sentinel monitor mymaster 172.31.185.120 6379 2
# master中redis的密码
sentinel auth-pass mymaster redis
# 哨兵从master节点宕机后,等待多少时间(毫秒),认定master不可用。
# 默认30s,这里为了测试,改成10s
sentinel down-after-milliseconds mymaster 10000
# 当替换主节点后,剩余从节点重新和新master做同步的并行数量,默认为 1
sentinel parallel-syncs mymaster 1
# 主备切换的时间,若在3分钟内没有切换成功,换另一个从节点切换
sentinel failover-timeout mymaster 180000

2.4 在3个节点分别启动哨兵进程

[dave@www.cndba.cn_1 redis]# redis-sentinel /etc/sentinel.conf

[dave@www.cndba.cn_2 etc]# redis-sentinel /etc/sentinel.conf

[dave@www.cndba.cn_3 redis-7.0.1]# redis-sentinel /etc/sentinel.conf

[dave@www.cndba.cn_1 redis]# ps -ef|grep redis
root      5738     1  0 15:11 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root      6081 32434  0 15:13 pts/0    00:00:00 grep --color=auto redis
root     27023     1  0 Jun16 ?        00:02:06 /usr/local/redis/bin/redis-server 0.0.0.0:6379
[dave@www.cndba.cn_1 redis]#

2.5 查看哨兵状态

Master 节点:

[dave@www.cndba.cn_1 redis]# redis-cli
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:8815ccfaffbed8d45123a6f5e7426d146c497134
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>

3 验证哨兵环境


查看Master 状态:

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.31.185.131,port=6379,state=online,offset=9825,lag=0
slave1:ip=172.31.185.165,port=6379,state=online,offset=9825,lag=1
master_failover_state:no-failover
master_replid:e07445bc0cc0e180c25c3e006af5b765943b2f6e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:9968
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:9968
127.0.0.1:6379>

Kill 掉主库:

[dave@www.cndba.cn_1 ~]# ps -ef|grep redis
root      9914 32434  0 15:39 pts/0    00:00:00 redis-cli
root     10348     1  0 15:41 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root     10606 10286  0 15:43 pts/1    00:00:00 grep --color=auto redis
root     27023     1  0 Jun16 ?        00:05:15 /usr/local/redis/bin/redis-server 0.0.0.0:6379
[dave@www.cndba.cn_1 ~]# kill -9 27023

从库查看,并没有切换:

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.31.185.120
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:29256
slave_repl_offset:29256
master_link_down_since_seconds:159
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:e07445bc0cc0e180c25c3e006af5b765943b2f6e
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:29256
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:29242
127.0.0.1:6379>

后来将哨兵的投票数改成1,切换成功:

sentinel monitor mymaster 172.31.185.120 6379 1

这里哨兵数量大于的情况下设置为:N/2-1, 3个以内设置为1。 否则不会发生切换。

我这里kill 掉Master 主节点后,切换到了mongodb2节点:

[dave@www.cndba.cn_2 ~]# redis-cli
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.31.185.131,port=6379,state=online,offset=87057,lag=0
master_failover_state:no-failover
master_replid:89e8552ec67362ea3c1a4d07244111757a42b259
master_replid2:afaf2753e116b73f2c9f5b02996d7583cf125ad4
master_repl_offset:87200
second_repl_offset:76526
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1311
repl_backlog_histlen:85890
127.0.0.1:6379>

重启原来的Master,可以看到原Master 也变成了Slave:

[dave@www.cndba.cn_1 redis]# systemctl start redis
[dave@www.cndba.cn_1 redis]# redis-cli
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.31.185.165
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:162073
slave_repl_offset:162073
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:89e8552ec67362ea3c1a4d07244111757a42b259
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:162073
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:159320
repl_backlog_histlen:2754
127.0.0.1:6379>

与[转帖]Redis 7.0 三节点哨兵(Sentinel)高可用 环境搭建手册相似的内容:

[转帖]Redis 7.0 三节点哨兵(Sentinel)高可用 环境搭建手册

2022-06-17 16:253480原创Redis 本文链接:https://www.cndba.cn/dave/article/108088 1 哨兵高可用架构说明 Redis 最早的高可用方案是主从复制,但这种方案存在一个问题,就是当主库宕机后,从库不会自动切成主库,需要人工干预。 所有在主

[转帖]Redis 7 参数 修改 说明

2022-06-16 14:491800原创Redis 本文链接:https://www.cndba.cn/dave/article/108066 在之前的博客我们介绍了Redis 7 的安装和配置,如下: Linux 7.8 平台 Redis 7 安装并配置开机自启动 操作手册https://ww

[转帖]Redis各版本特性汇总

redis4redis5redis6redis6.2重大特性1.模块系统 2.PSYNC2 3.LFU淘汰策略 4.混合RDB-AOF持久化 5.LAZY FREE延迟释放 6.MEMORY内存分析命令 7.支持NAT/DOCKER 8.主动碎片整理 1.新增Stream数据类型 2.新增Redis

[转帖]Redis各版本特性汇总

redis4redis5redis6redis6.2重大特性1.模块系统 2.PSYNC2 3.LFU淘汰策略 4.混合RDB-AOF持久化 5.LAZY FREE延迟释放 6.MEMORY内存分析命令 7.支持NAT/DOCKER 8.主动碎片整理 1.新增Stream数据类型 2.新增Redis

[转帖]Redis 备份与恢复(RDB/AOF) 说明

2022-06-16 20:364580原创Redis 本文链接:https://www.cndba.cn/dave/article/108068 1 RDB 方式 1.1 RDB 备份恢复说明 Redis 的备份恢复有两种方法:RDB和AOF。 其中RDB 文件是一个经过压缩的二进制文件,有两个R

[转帖]Redis 性能优化的 13 条军规!史上最全

https://zhuanlan.zhihu.com/p/118532234 Redis性能优化实战方案 Redis 是基于单线程模型实现的,也就是 Redis 是使用一个线程来处理所有的客户端请求的,尽管 Redis 使用了非阻塞式 IO,并且对各种命令都做了优化(大部分命令操作时间复杂度都是 O

[转帖]Redis性能调优万字总结,面试必问!

https://zhuanlan.zhihu.com/p/541745804 于哥你好,最近面试挺多的,尤其是在问到java面试题,Redis被问的特别多,比如 Redis的内存模型? Redis的底层数据结构是怎么的? Redis的多线程模型 Redis的集群原理 Redis的雪崩,击穿,穿透怎么

[转帖]Redis连接未释放,造成TCP连接数过多

https://segmentfault.com/a/1190000022704886 早上看到服务器告警通知,TCP连接数比较高,达到5000多,我设置的阈值是5000,正常TCP连接不会这么高,这样的一个阈值我可以提前知道有问题早点解决,不至于后面引起一系列问题,甚至拖垮服务器。 排查 登陆服务

[转帖]Redis客户端Jedis、Lettuce、Redisson

https://www.jianshu.com/p/90a9e2eccd73 在SpringBoot2.x之后,原来使用的jedis被替换为了lettuce Jedis:采用的直连,BIO网络模型 Jedis有一个问题:多个线程使用一个连接的时候线程不安全。 解决思路是: 使用连接池,为每个请求创建

[转帖]Redis 的 Keys 和 Values 都是 512 M的限制

Redis 的 Keys 和 Values 都是 512 M的限制 官网介绍:https://redis.io/topics/data-types-intro