Redis Sentinel集群部署

Redis Sentinel集群部署
原文:https://blog.csdn.net/sunbocong/article/details/85252071

期间涉及的目录文件夹 可以自己创建修改或者照着新建
实验环境

操作系统: Ubuntu 18.04.1 LTS
redis版本: redis 5.0.2

机器规划

role ip port
Master 127.0.0.1 6379
Slave1 127.0.0.1 6380
Slave2 127.0.0.1 6381
Sentinel1 127.0.0.1 26379
Sentinel2 127.0.0.1 26380
Sentinel3 127.0.0.1 26381

注意:
生产环境中建议Redis Sentinel的所有节点应该分布在不同的物理机上。

原理说明

主从复制存在的问题

Redis的主从复制模式可以将主节点的数据改变同步给从节点,这样从
节点就可以起到两个作用:第一,作为主节点的一个备份,一旦主节点出了
故障不可达的情况,从节点可以作为后备“顶”上来,并且保证数据尽量不丢
失(主从复制是最终一致性)。第二,从节点可以扩展主节点的读能力,一
旦主节点不能支撑住大并发量的读操作,从节点可以在一定程度上帮助主节
点分担读压力。
但是主从复制也带来了以下问题:
·一旦主节点出现故障,需要手动将一个从节点晋升为主节点,同时需
要修改应用方的主节点地址,还需要命令其他从节点去复制新的主节点,整
个过程都需要人工干预。

Redis Sentinel方案

Redis Sentinel是一个分布式架构,其中包含若干个Sentinel节点和Redis
数据节点,每个Sentinel节点会对数据节点和其余Sentinel节点进行监控,当
它发现节点不可达时,会对节点做下线标识。如果被标识的是主节点,它还
会和其他Sentinel节点进行“协商”,当大多数Sentinel节点都认为主节点不可
达时,它们会选举出一个Sentinel节点来完成自动故障转移的工作,同时会
将这个变化实时通知给Redis应用方。整个过程完全是自动的,不需要人工
来介入,所以这套方案很有效地解决了Redis的高可用问题。

安装部署

1.配置并启动Master节点

创建data目录:

root@sbc-VirtualBox:~# mkdir -p /opt/soft/redis/data
1
创建配置文件:

root@sbc-VirtualBox:~# vim /etc/redis/redis-6379.conf

port 6379
daemonize yes
logfile “6379.log”
dbfilename “dump-6379.rdb”
dir “/opt/soft/redis/data/”
或者用aof启动
daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379
dbfilename dbmp.rdb
#requirepass redhat 密码redhat
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec

启动Master节点:

root@sbc-VirtualBox:~# redis-server /etc/redis/redis-6379.conf
1
验证Master节点状态:

root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6379 ping
PONG
1
2
2.配置并启动两个Slave节点

创建配置文件:

slave1:

root@sbc-VirtualBox:~# vim /etc/redis/redis-6380.conf

port 6380
daemonize yes
logfile “6380.log”
dbfilename “dump-6380.rdb”
dir “/opt/soft/redis/data/”
slaveof 127.0.0.1 6379
1
2
3
4
5
6
7
8
slave2:

root@sbc-VirtualBox:~# vim /etc/redis/redis-6381.conf

port 6381
daemonize yes
logfile “6381.log”
dbfilename “dump-6381.rdb”
dir “/opt/soft/redis/data/”
slaveof 127.0.0.1 6379
1
2
3
4
5
6
7
8
启动2个Slave节点:

root@sbc-VirtualBox:~# redis-server /etc/redis/redis-6380.conf
root@sbc-VirtualBox:~# redis-server /etc/redis/redis-6381.conf
1
2
验证:

root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6380 ping
PONG
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6381 ping
PONG
1
2
3
4
3.验证主从关系

查看Master节点replication信息:

root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6379 info replication

Replication

role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=126,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=126,lag=1
master_replid:1ca0bf14ca2cd79941df7285bf413aa6b8ae921d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126
1
2
3
4
5
6
7
8
9
10
11
12
13
14
可观察到connected_slaves有2个,分别为:

slave0:ip=127.0.0.1,port=6380,state=online,offset=126,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=126,lag=1
1
2
同样在Slave节点也可查看到replication信息:

root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6380 info replication

Replication

role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:252
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:1ca0bf14ca2cd79941df7285bf413aa6b8ae921d
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
可观察到Master信息,连接状态正常:

master_host:127.0.0.1
master_port:6379
master_link_status:up
1
2
3
部署Sentinel节点

1.配置Sentinel1节点

创建conf文件:

root@sbc-VirtualBox:~# vim /etc/redis/redis-sentinel-26379.conf

port 26379
daemonize yes
logfile “26379.log”
dir /opt/soft/redis/data
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
1
2
3
4
5
6
7
8
9
10
Sentinel节点本身就是独立的Redis节点,只不过它们有一些特殊,它们不存储数据,只支持部分命令。

2.启动Sentinel1节点

root@sbc-VirtualBox:~# redis-sentinel /etc/redis/redis-sentinel-26379.conf
1
3.确认Sentinel1节点Sentinel状态

在Sentinel1节点查看Sentinel状态:

root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -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:6379,slaves=2,sentinels=1
1
2
3
4
5
6
7
8
配置并启动Sentinel2和Sentinel3节点

生成配置文件:
Sentinel2:

root@sbc-VirtualBox:~# vim /etc/redis/redis-sentinel-26380.conf

port 26380
daemonize yes
logfile “26380.log”
dir /opt/soft/redis/data
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
1
2
3
4
5
6
7
8
9
10
Sentinel3:

root@sbc-VirtualBox:~# vim /etc/redis/redis-sentinel-26381.conf

port 26381
daemonize yes
logfile “26381.log”
dir /opt/soft/redis/data
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
1
2
3
4
5
6
7
8
9
10
启动Sentinel2和Sentinel3:

root@sbc-VirtualBox:~# redis-sentinel /etc/redis/redis-sentinel-26380.conf
root@sbc-VirtualBox:~# redis-sentinel /etc/redis/redis-sentinel-26381.conf
1
2
验证Sentinel状态

连接任意Sentinel节点:

root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -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:6379,slaves=2,sentinels=3
1
2
3
4
5
6
7
8
可观察到master地址,端口正确。slave节点,sentinel节点数量正确。

sentinel monitor mymaster127.0.0.163792配置代表sentinel-1节点需要监控127.0.0.1:6379这个主节点,2代表判断主节点失败至少需要2个Sentinel节点同意,mymaster是主节点的别名

Sentinel 配置说明

当所有节点启动后,配置文件中的内容发生了变化,体现在三个方面:

Sentinel节点自动发现了从节点、其余Sentinel节点。
去掉了默认配置,例如parallel-syncs、failover-timeout参数。
添加了配置纪元相关参数。
下面以Sentinel1节点为例:

Sentinel1节点的conf文件变为了

port 26379
daemonize yes
logfile “26379.log”
dir “/opt/soft/redis/data”
sentinel myid b6983fbf6b4527811b724ce5ef0ea08388056e34
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0

Generated by CONFIG REWRITE

protected-mode no
sentinel leader-epoch mymaster 0
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6380
sentinel known-sentinel mymaster 127.0.0.1 26381 4d77ba4fbfd1696db44e6302340988f771bbb62a
sentinel known-sentinel mymaster 127.0.0.1 26380 75cd9743a892825573793c4417e7e8465d65b616
sentinel current-epoch 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
port,daemonize,logfile和dir

port表示Sentinel节点的端口;
daemonize表示守护进程,开启则Sentinel将在后台运行;
logfile表示Sentinel节点的日志路径;
dir表示Sentinel节点的工作目录;

sentinel monitor mymaster

sentinel monitor
1
Sentinel节点会定期监控主节点,所以从配置上必然也会有所体现,本
配置说明Sentinel节点要监控的是一个名字叫做,ip地址和端
口为的主节点。代表要判定主节点最终不可达所需要的票数。一
般建议将其设置为Sentinel节点的一半加1。
同时还与Sentinel节点的领导者选举有关,至少要有max(quorum,num(sentinels)/2+1)个Sentinel节点参与选举,才能选出领导者Sentinel,从而完成故障转移。例如有5个Sentinel节点,quorum=4,那么至少要有max(quorum,num(sentinels)/2+1)=4个在线Sentinel节点才可以进行领导者选举
如:

sentinel monitor mymaster 127.0.0.1 6379 2
1
sentinel down-after-milliseconds

sentinel down-after-milliseconds
1
每个Sentinel节点都要通过定期发送ping命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过了down-after-milliseconds配置的时间且没有有效的回复,则判定节点不可达,(单位为毫秒)就是超时时间。这个配置是对节点失败判定的重要依据。

sentinel parallel-syncs

sentinel parallel-syncs
1
当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,parallel-syncs就是用来限制在一次故障转移之后,每次向新的主节点发起复制操作的从节点个数。如果这个参数配置的比较大,那么多个从节点会向新的主节点同时发起复制操作,尽管复制操作通常不会阻塞主节点,但是同时向主节点发起复制,必然会对主节点所在的机器造成一定的网络和磁盘IO开销。

sentinel failover-timeout

sentinel failover-timeout
1
failover-timeout通常被解释成故障转移超时时间,但实际上它作用于故
障转移的各个阶段:
a)选出合适从节点。
b)晋升选出的从节点为主节点。
c)命令其余从节点复制新的主节点。
d)等待原主节点恢复后命令它去复制新的主节点。

sentinel auth-pass

sentinel auth-pass
1
如果Sentinel监控的主节点配置了密码,sentinel auth-pass配置通过添加
主节点的密码,防止Sentinel节点对主节点无法监控。

sentinel notification-script

sentinel notification-script
1
sentinel notification-script的作用是在故障转移期间,当一些警告级别的Sentinel事件发生(指重要事件,例如-sdown:客观下线、-odown:主观下线)时,会触发对应路径的脚本,并向脚本发送相应的事件参数。

sentinel client-reconfig-script

sentinel client-reconfig-script
1
sentinel client-reconfig-script的作用是在故障转移结束后,会触发对应路
径的脚本,并向脚本发送故障转移结果的相关参数。

Sentinel的特点

Redis Sentinel具有以下几个功能:
·监控:Sentinel节点会定期检测Redis数据节点、其余Sentinel节点是否
可达。
·通知:Sentinel节点会将故障转移的结果通知给应用方。
·主节点故障转移:实现从节点晋升为主节点并维护后续正确的主从关
系。
·配置提供者:在Redis Sentinel结构中,客户端在初始化的时候连接的
是Sentinel节点集合,从中获取主节点信息

Redis Sentinel的优势:

·对于节点的故障判断是由多个Sentinel节点共同完成,这样可以有效地
防止误判。
·Sentinel节点集合是由若干个Sentinel节点组成的,这样即使个别Sentinel
节点不可用,整个Sentinel节点集合依然是健壮的。

原文:https://blog.csdn.net/sunbocong/article/details/85252071

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值