`
bit1129
  • 浏览: 1051284 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【Redis三】基于Redis sentinel的自动failover主从复制

 
阅读更多

在第二篇中使用2.8.17搭建了主从复制,但是它存在Master单点问题,为了解决这个问题,Redis从2.6开始引入sentinel,用于监控和管理Redis的主从复制环境,进行自动failover,即Master挂了后,sentinel自动从从服务器选出一个Master使主从复制集群仍然可以工作,如果Master醒来再次加入集群,只能以从服务器的形式工作。

 

什么是Sentinel

Redis Sentinel is a system designed to help managing Redis instances. It performs the following four tasks
  • Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.
  • Notification. Sentinel can notify the system administrator, or another computer program, via an API, that something is wrong with one of the monitored Redis instances.
  • Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.
  • Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.

 

Redis主从复制搭建步骤

 

Redis搭建主从复制的步骤十分简单,以下在Windows上操作。

 

 

1. 解压Redis压缩包至E:\devsoftware\redis-2.8.17

2.在Redis目录中复制两份redis.windows.conf,分别命名为redis.windows.conf.slave1,redis.windows.conf.slave2

3.修改redis.conf.slave1文件,

  • port 6379 改为 port 6380
  • 打开slaveof指令,改为slaveof 127.0.0.1 6379
  • 将快照文件名改为dbfilename dump.slave1.rdb
  • 如果使用AOF方式持久化,则需要将appendfilename appendonly.aof改为appendfilename appendonly.slave1.aof

4. 修改redis.conf.slave2文件

  • port 6379 改为 port 6381
  • 打开slaveof指令,改为slaveof 127.0.0.1 6379
  • 将快照文件名改为dbfilename dump.slave2.rdb
  • 如果使用AOF方式持久化,则需要将appendfilename appendonly.aof改为appendfilename appendonly.slave2.aof

5. 启动三个Redis服务器,形成一主两从的主从复制副本集

 

通过上面的启动过程,可见只需要redis-server一个启动脚本即可,它读取不同的配置文件,来完成初始化

 

 

  • Master启动时的日志输出

 

 

[21684] 25 Nov 20:54:16 * Slave ask for synchronization
[21684] 25 Nov 20:54:16 * Starting BGSAVE for SYNC
[21684] 25 Nov 20:54:16 * Foregroud saving started by pid 21684
[21684] 25 Nov 20:54:17 * DB saved on disk
[21684] 25 Nov 20:54:17 * Background saving terminated with suc
[21684] 25 Nov 20:54:17 * Synchronization with slave succeeded

 

  •  Slave1启动时的日志输出
[21068] 25 Nov 20:54:15 * Server started, Redis version 2.4.5
[21068] 25 Nov 20:54:15 * DB loaded from disk: 0 seconds
[21068] 25 Nov 20:54:15 * The server is now ready to accept connections on port
[21068] 25 Nov 20:54:16 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[21068] 25 Nov 20:54:16 - 0 clients connected (0 slaves), 1180024 bytes in use
[21068] 25 Nov 20:54:16 * Connecting to MASTER...
[21068] 25 Nov 20:54:16 * MASTER <-> SLAVE sync started
[21068] 25 Nov 20:54:16 * Non blocking connect for SYNC fired the event.
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: receiving 19 bytes from master
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Loading DB in memory
[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Finished with success

 

Redis sentinel 搭建步骤

 

  • 创建sentinel服务器的配置文件,sentinel.conf文件,内容如下
port 26389
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
 特别注意,在2.6版本还需要额外设置一个属性sentinel can-failover mymaster yes,在2.8版本无需设置这个属性,设置了反而不能启动sentinel服务器

 1. down-after-milliseconds

     master被当前sentinel实例认定为“失效”(SDOWN)的间隔时间

 2. failover-timeout

     failover过期时间,当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel  将会认为此次failoer失败

3. parallel-syncs

    当新master产生时,同时进行slaveof到新master并进行同步复制的slave个数,也就是同时几个slave进行同步。因为在salve执行salveof与新master同步时,将会终止客户端请求,因此这个值需要权衡。此值较大,意味着“集群”终止客户端请求的时间总和和较大,此值较小,意味着“集群”在故障转移期间,多个salve向客户端提供服务时仍然使用旧数据

 

  • 启动sentinel服务器

     使用如下命令启动sentinel服务器

 

 

redis-server.exe sentinel.conf --sentinel
 

 

  • 经过数据同步后,sentinel监测到当前的系统当前有一主两从环境,使用如下命令可以观察到sentinel的状态

 

redis-cli.exe -h 127.0.0.1 -p 26389 info sentinel

 

 

 得到如下结果,结果表明,当前的master监听端口是6381

 

E:\devsoftware\redis-2.8.17>redis-cli.exe -h 127.0.0.1 -p 26389 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6381,slaves=2,sentinels=1
 

 

主服务器写入从服务器读取数据测试

  •  往主服务器上写入一个数据,检查从服务器是否同步
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6379
redis 127.0.0.1:6379> set  abc 1
OK
redis 127.0.0.1:6379> get abc
"1"
redis 127.0.0.1:6379>Ctrl + C
 

 

  • 两个从服务器的读取操作:

 

 

E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6380
redis 127.0.0.1:6380> get abc
"1"
redis 127.0.0.1:6380> ^C
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6381
redis 127.0.0.1:6381> get abc
"1"
redis 127.0.0.1:6381>
 

 

从服务器写入,其它服务器读取数据测试


系统提示,
(error) READONLY You can't write against a read only slave.
 
只读slave不能写入数据,有只读的slave,就应该有可读写的slave的存在。这个看看配置文件,配置文件里有如下指令
slave-read-only yes
 配置文件对这个指令的注释是,

Since Redis 2.6 by default slaves are read-only.
Note: read only slaves are not designed to be exposed to untrusted clients on the internet. It's just a protection layer against misuse of the instance. Still a read only slave exports by default all the administrative commands
such as CONFIG, DEBUG, and so forth. To a limited extend you can improve security of read only slaves using 'rename-command' to shadow all the  administrative / dangerous commands.
 

主服务器挂了,是否会自动的从Slave中重新选择Master

当Master挂了之后,sentinel服务器会自动的监测到主服务器挂了,然后开始启动新Master选举工作,sentinel的工作如此重要,所以实际上sentinel本身应该是运行在集群环境中。当新的Master选举出来后,Slave重写配置文件sentinel.conf文件

 

port 26389
sentinel monitor mymaster 127.0.0.1 6381 1
sentinel down-after-milliseconds mymaster 10000
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1

# Generated by CONFIG REWRITE
dir "E:\\devsoftware\\redis-2.8.17"
sentinel known-slave mymaster 127.0.0.1 6379
sentinel known-slave mymaster 127.0.0.1 6380

sentinel current-epoch 1

 

 从上面可以看到,6381成为Master,尽管6379(原来的Master)已经挂了,但是还是在配置中作为已知的slave配置在sentinel.conf中,当6379启动后,将作为Slave加入到集群中。6379挂了,可以通过如下命令查看当前sentinel管理的从服务器信息

 

redis-cli -h 192.168.7.46 -p 26379 sentinel slaves mymaster 

 

如果6379永远下线,所以将这个信息继续保留在sentinel中,就不合理了,所以sentinel提供了删除无效服务器的信息,通过如下命令:

 

 

E:\devsoftware\redis-2.8.17>redis-cli.exe -h 127.0.0.1 -p 26389
127.0.0.1:26389> sentinel reset mymaster
(integer) 1

 

此时再执行

 

redis-cli -h 192.168.7.46 -p 26379 sentinel slaves mymaster 

 

可以看到之前挂掉的服务器将从sentinel中删除掉了。但是,sentenil.conf配置文件中,依然保存了6379是一个slave,也就是说,Sentinel重启后,又会加载配置文件,把6379作为slave加进来了,此时,需要手动的把6379从配置文件中删除掉。

 

最后一点,基于Sentinel的Redis主从复制不是Quarum based,也就是说,即使系统只剩下最后一台服务器,也会继续工作,而不像Zookeeper和MongoDB是Quarum Based,副本集必须保证超过半数的系统还在运行,否则,系统将不能继续工作。

 

 

 

Sentinel、Master和Slave自动发现

 

Sentinels and Slaves auto discovery

While Sentinels stay connected with other Sentinels in order to reciprocally check the availability of each other, and to exchange messages, you don't need to configure the other Sentinel addresses in every Sentinel instance you run, as Sentinel uses the Redis master Pub/Sub capabilities in order to discover the other Sentinels that are monitoring the same master.

This is obtained by sending Hello Messages into the channel named __sentinel__:hello.

Similarly you don't need to configure what is the list of the slaves attached to a master, as Sentinel will auto discover this list querying Redis.

  • Every Sentinel publishes a message to every monitored master and slave Pub/Sub channel __sentinel__:hello, every two seconds, announcing its presence with ip, port, runid.
  • Every Sentinel is subscribed to the Pub/Sub channel __sentinel__:hello of every master and slave, looking for unknown sentinels. When new sentinels are detected, they are added as sentinels of this master.
  • Hello messages also include the full current configuration of the master. If another Sentinel has a configuration for a given master that is older than the one received, it updates to the new configuration immediately.
  • Before adding a new sentinel to a master a Sentinel always checks if there is already a sentinel with the same runid or the same address (ip and port pair). In that case all the matching sentinels are removed, and the new added.

 

 

1. Q: 在Sentinel的配置文件中,我们看到sentinel只监控了Master,那么那些Slave是如何监测到的呢?
A:Sentinel通过监听Master,知道了Master的存在,那么Sentinel可以读取Master关于Slave的信息,然后交由Sentinel进行管理
2.Q:在Sentinel的配置文件中,我们看到sentinel只监控了Master,那么那些同一个集群中的Sentinel是如何发现的呢?
3. Q:当Master挂掉再重启后,将作为Slave加入到集群里,而Master的配置文件中,slaveof指令是没有配置的,也就是说,Master启动时,它自己认为自己将作为Master或者独立的Redis服务器运行,此时,Sentinel如何把它作为Slave加入到集群中的?
A: 老的Master挂掉后,新的Master产生,但是老的Master信息(IP:Port)已然存在于sentinel中,不过它已经不再是Master而是Slave。当Master启动后,Sentinel可以ping到它启动起来了,此时,Sentinel将它的配置文件加了一行,指示它已经成为别人的Slave,如
slaveof 127.0.0.1 6381
 然后通知Master,已经是Slave状态(只要修改用于指示Master/Slave的标示参数即可)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

分享到:
评论

相关推荐

    redis sentinel 自动安装配置

    实现了自动安装配置redis 已经测试过了,如果有问题,请留言 启动脚本需要 3个参数 serverIP masterIP redisType 例如, 作为master ./redis_create.sh 192.168.10.10 192.168.10.10 master 作为slave ./redis_...

    redis主从模式与哨兵模式例子

    redis主从模式与哨兵模式例子 # 当前Sentinel服务运行的端口 port 26378 # 哨兵监听的主服务器 sentinel myid 09a71e001825114de399e0b6214c4b6f5449ab3a # 3秒内mymaster没响应,则认为mymaster宕机 sentinel deny...

    redis-sentinel-集群.7z

    自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为...

    redis-sentinel-demo:redis-sentinel示例,实现高可用(Auto Failover

    redis-sentinel示例,实现高可用(Auto Failover:自动故障转移),主从自动切换.包含redis配置,redis-sentinel配置,以及Java示例代码. 参考原帖地址:http://blog.csdn.net/pi9nc/article/details/17735653

    redis-manager.zip

    Redis Manager 是 Redis 一站式管理平台,支持集群(cluster、master-replica、sentinel)的监控、安装(除sentinel)、管理、告警以及基本的数据操作功能 集群监控:支持监控 Memory、Clients 等 Redis 重要指标;...

    desc_sentinel:redis sentinel源码入门阅读指南

    sentinel 源码阅读指南以下内容sentinel简介sentinel主逻辑函数sentinel ...sentinel failover过程的各种pubsub message由来的详细介绍配合上面的所有pubsub message现有方案的做法slave down的现有方案redis24的master...

    Redis V3.0 中文文档

    第 1 章Redis 介绍. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 第 2 章数据类型初探. . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    Redis Sentinel服务配置流程(详解)

    1、Redis Sentinel服务配置 1.1简介 Redis 的 Sentinel 系统...自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务

    Redis云管理平台CacheCloud.zip

    一、CacheCloud是做什么的CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone、Redis Sentinel、Redis Cluster)自动部署、解决Redis实例碎片化现象、提供完善统计、监控、运维功能、减少开发人员的...

    Redis 3.0 中文版 - v1.1.pdf

    第 1 章Redis 介绍. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 第 2 章数据类型初探. . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    docker-rediscluster:用于设置一次性 redis 集群以测试哨兵故障转移的 Docker 脚本

    docker-rediscluster 设置一次性 redis 集群以测试哨兵故障转移。...docker pull joshula/redis-sentinel 跑步 provision_docker_redis_cluster.sh 删除集群 remove_docker_redis_cluster.sh 玩它 附加到哨兵 su

    redis-manager-master.zip

    Redis Manager 是 Redis 一站式管理平台,支持集群(cluster、master-replica、sentinel)的监控、安装(除sentinel)、管理、告警以及基本的数据操作功能,支持监控 Memory、Clients 等 Redis 重要指标;...

    resec:ReSeC- Redis服务领事

    它避免了Redis Sentinel记住复制群集中曾经出现过的所有哨兵和所有Redis服务器的问题。 Resec主节点选举基于提供单个Redis主节点实例。 Resec持续监视redis实例的状态,如果它还活着,它将启动以下两个过程: ...

    redis-manager:Redis 一站式管理平台,支持集群的监控、安装、管理、告警以及基本的数据操作

    Redis ManagerRedis Manager 是 Redis 一站式管理平台,支持集群(cluster、master-replica、sentinel)的监控、安装(除sentinel)、管理、告警以及基本的数据操作功能集群监控:支持监控 Memory、Clients 等 Redis...

    kvrocks:Kvrocks 是一个基于 RocksDB 的分布式键值 NoSQL 数据库,兼容 Redis 协议

    Redis协议,用户可以使用redis客户端访问kvrocks 命名空间,类似于 redis db,但每个命名空间使用令牌复制,异步复制使用像 MySQL 这样的 binlog 高可用,支持redis sentinel在master或slave故障时进行failover ...

    CacheCloud云管理平台-其他

    CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone、Redis Sentinel、Redis Cluster)自动部署、解决Redis实例碎片化现象、提供完善统计、监控、运维功能、减少开发人员的运维成本和误操作,提高...

Global site tag (gtag.js) - Google Analytics