快速学习哨兵模式redis主从复制

主从复制–读写分离

通过持久化功能,Redis 保证了即使在服务器重启的情况下也不会丢失(或少量丢失)数据,但是由于数据是存储在一台服务器上的,如果这台服务器出现故障,比如硬盘坏了,也会导致数据丢失。
为了避免单点故障,我们需要将数据复制多份部署在多台不同的服务器上,即使有一台服务器出现故障其他服务器依然可以继续提供服务。
这就要求当一台服务器上的数据更新后,自动将更新的数据同步到其他服务器上, 那该怎么实现呢?Redis的主从复制。
快速学习哨兵模式redis主从复制
Redis 提供了复制(replication)功能来自动实现多台 redis 服务器的数据同步(每天19 点新闻联播,基本从 cctv1-8,各大卫视都会播放)我们可以通过部署多台 redis,并在配置文件中指定这几台 redis 之间的主从关系,主负责写入数据,同时把写入的数据实时同步到从机器,这种模式叫做主从复制,即master/slave,并且 redis 默认 master 用于写,slave 用于读,向 slave 写数据会导致错误

Redis 主从复制实现(master/salve)

修改配置文件,启动时,服务器读取配置文件,并自动成为指定服务器的从服务器,从而构成主从复制的关系
实现步骤:
模拟多 Reids 服务器,在一台已经安装 Redis 的机器上,运行多个 Redis 应用模拟多个 Reids 服务器。一个 Master,两个 Slave.

1.和redis.conf同级目录下新建三个 Redis 的配置文件

如果 Redis 启动,先停止。
作为 Master 的 Redis 端口是 6380
作为 Slaver 的 Redis 端口分别是 6382 , 6384
vi redis6380.conf
vi redis6382.conf
vi redis6384.conf

2. 编辑 Master 配置文件

编辑 Master 的配置文件 redis6380.conf : 在空文件加入如下内容
include /usr/local/redis-4.0.13/redis.conf
daemonize yes
port 6380
pidfile /var/run/redis_6380.pid
logfile 6380.log
dbfilename dump6380.rdb
配置项说明:
include :包含原来的配置文件内容。/usr/local/ redis-4.0.13/redis.conf 按照自己的目录设置。
daemonize:yes 后台启动应用,相当于 ./redis-server & 的作用。
port : 自定义的端口号
pidfile : 自定义的文件,表示当前程序的 pid ,进程 id。
logfile:日志文件名
dbfilename:持久化的 rdb 文件名
3. 编辑 Slave 配置文件
编辑 Slave 的配置文件 redis6382.conf 和 redis6384.conf: 在空文件加入如下内容

①:redis6382.conf:
include /usr/local/redis-4.0.13/redis.conf
daemonize yes
port 6382
pidfile /var/run/redis_6382.pid
logfile 6382.log
dbfilename dump6382.rdb
slaveof 127.0.0.1 6380
配置项说明:
slaveof :表示当前 Redis 是谁的从。当前是 127.0.0.0 端口 6380 这个 Master 的从。
②:redis6384.conf:
include /usr/local/redis-4.0.13/redis.conf
daemonize yes
port 6384
pidfile /var/run/redis_6384.pid
logfile 6384.log
dbfilename dump6384.rdb
slaveof 127.0.0.1 6380

4. 启动服务器 Master/Slave 都启动

进入src目录
启动方式 ./redis-server 配置文件
启动 Redis,并查看启动进程
快速学习哨兵模式redis主从复制

5.配置Sentinel 哨兵

1)Sentinel 配置文件
复制三份sentinel.conf文件 ,sentinel.conf和redis.conf文件同级
快速学习哨兵模式redis主从复制
Sentinel 系统默认 port 是 26379 。三个配置 port 分别设置为 26380 , 26382 , 26384。
三个文件分别命名:
sentinel26380.conf
sentinel26382.conf
sentinel26384.conf
执行复制命令 cp sentinel.conf xxx.conf

快速学习哨兵模式redis主从复制
(2) 三份 sentinel 配置文件修改:
①、修改 port 26380、 port 26382、 port 26384
②、修改 sentinel monitor mymaster 127.0.0.1 6382 2
格式:sentinel monitor <Quorum 投票数>
Sentinel监控主(Master)Redis, Sentinel根据Master的配置自动发现Master的Slave,Sentinel
默认端口号为26379。
快速学习哨兵模式redis主从复制
快速学习哨兵模式redis主从复制
sentinel26380.conf 修改port 26380 , master的port 6382
sentinel26382.conf 修改port 26382 , master的port 6382
sentinel26384.conf 修改port 26384 , master的port 6382
(3) 启动主从(Master/Slave)Redis
启动 Reids
快速学习哨兵模式redis主从复制
查看 Master 的配置信息
连接到 6382 端口
快速学习哨兵模式redis主从复制
使用 info replication命令查看 Master/Slave
快速学习哨兵模式redis主从复制
(4) 启动 Sentinel
redis安装时make编译后就产生了redis-sentinel程序文件,可以在一个redis中运行多个sentinel进程。
启动一个运行在Sentinel模式下的Redis服务实例语法:
./redis-sentinel sentinel 配置文件
在 XShell 开启三个窗口分别执行:
执行以下三条命令,将创建三个监视主服务器的Sentinel实例:
第一个窗口:./redis-sentinel …/sentinel26380.conf
第二个窗口:./redis-sentinel …/sentinel26382.conf
第三个窗口:./redis-sentinel …/sentinel26384.conf
(5) 主 Redis 不能工作
让 Master 的 Redis 停止服务,执行 shutdown
先执行 info replication 确认 Master 的 Redis,再执行 shutdown

快速学习哨兵模式redis主从复制
查看当前 Redis 的进程情况
快速学习哨兵模式redis主从复制
(6) Sentinel 的起作用
在 Master 执行 shutdown 后,稍微等一会 Sentinel 要进行投票计算,从可用的 Slave 选举新的 Master。
查看 Sentinel 日志,三个 Sentinel 窗口的日志是一样的。
快速学习哨兵模式redis主从复制
查看新的 Master
快速学习哨兵模式redis主从复制
(7) 新的 Redis 加入 Sentinel 系统,自动加入 Master
重新启动 6382
快速学习哨兵模式redis主从复制
查看 6384 的信息
快速学习哨兵模式redis主从复制
8) 监控
①Sentinel 会不断检查 Master 和 Slave 是否正常
②如果 Sentinel 挂了,就无法监控,所以需要多个哨兵,组成 Sentinel 网络,一个健康的Sentinel 至少有 3 个 Sentinel 应用。彼此在独立的物理机器或虚拟机。
③监控同一个 Master 的 Sentinel 会自动连接,组成一个分布式的 Sentinel 网络,互相通信并交换彼此关于被监控服务器的信息
④当一个 Sentinel 认为被监控的服务器已经下线时,它会向网络中的其它 Sentinel 进行确认,判断该服务器是否真的已经下线
⑤如果下线的服务器为主服务器,那么 Sentinel 网络将对下线主服务器进行自动故障转移,通过将下线主服务器的某个从服务器提升为新的主服务器,并让其从服务器转移到新的主服务器下,以此来让系统重新回到正常状态
下线的旧主服务器重新上线,Sentinel 会让它成为从,挂到新的主服务器下

(9) 总结
主从复制,解决了读请求的分担,从节点下线,会使得读请求能力有所下降,Master 下线,写请求无法执行
Sentinel 会在 Master 下线后自动执行故障转移操作,提升一台 Slave 为 Master,并让其它Slave 成为新 Master 的 Slave