深入浅出Redis-redis哨兵集群
1、Sentinel 哨兵
Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。
例如:
在Server1 掉线后:
升级Server2 为新的主服务器:
2、Redis 主从分离
在讲解Sentinel 哨兵集群之前,我们先来搭建一个简单的主从分离(读写分离)。
首先,我们默认大家都已经安装了redis,然后我们将 redis.conf 拷贝多份,并且创建多个目录,用于区分多个redis 服务:
这里面,每个目录中都有自己的redis.conf 配置文件,接下来,我们先设置主服务器的配置文件。
一、配置Master
1、修改端口
# Accept connections on the specified port, default is (IANA #).
# If port is specified Redis will not listen on a TCP socket.
port
redis 的默认端口是6379,这里我们把主服务器的端口设置为6380
2、修改pidfile
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6380.pid
pidfile 是我们启动redis 的时候,linux 为我们分配的一个pid 进程号,如果这里不作修改,会影响后面redis服务的启动
3、启动 redis
启动redis,我们可以看到,redis已经占领了6380 端口
进入客户端
redis-cli -p
127.0.0.1:> info
...
# Replication
role:master
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
...
我们可以看到,redis 现在的角色是一个master 启动的服务。
二、配置Slave
和上面配置 master一样,我们需要修改端口号和pid 文件,在修改完之后,我们有两种方法配置从服务
1、在配置文件中配置从服务
################################# REPLICATION ################################# # Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# ) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of slaves.
# ) Redis slaves are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# ) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them.
#
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6380
我们可以在配置文件中直接修改 slaveof 属性,我们直接配置主服务器的ip 地址,和端口号,如果这里主服务器有配置密码
可以通过配置masterauth 来设置链接密码
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
启动redis 服务:
我们可以看到,现在有两个现在在运行,我们进入6381的客户端,看一下他的状态,
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
我们可以看到,现在的redis 是一个从服务的角色,连接着6380的服务。
2、在服务启动后设置
我们修改6382端口的服务器配置文件之后,启动服务
进入客户端,查看当前服务器的状态:
# Replication
role:master
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
我们可以看到,当前服务器的状态时作为一个主服务的角色在运行,我们接下来修改他的状态:
127.0.0.1:> slaveof 127.0.0.1 //修改后状态
# Replication
role:slave
master_host:127.0.0.1
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
3、总结
我们先看一下目前master 的状态:
# Replication
role:master
connected_slaves:
slave0:ip=127.0.0.1,port=,state=online,offset=,lag=
slave1:ip=127.0.0.1,port=,state=online,offset=,lag=
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
我们可以可以看到,两个从服务已经在连着主服务器,上面两种配置的区别在于,当salve 断线重连之后,
如果我们是修改类配置文件,重连之后会自己链接上去master,并且同步master 上面的数据,
如果我们是手动连接上去的主服务器,重连之后,从服务器会读取自己本地的 rdb 回复数据,而不会去自动链接主服务
我们如果需要设置读写分离,只需要在主服务器中设置:
# 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 extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only yes
3、Sentinel 哨兵
1、配置端口
在sentinel.conf 配置文件中, 我们可以找到port 属性,这里是用来设置sentinel 的端口,一般情况下,至少会需要三个哨兵对redis 进行监控,我们可以通过修改端口启动多个sentinel 服务。
# port <sentinel-port>
# The port that this sentinel instance will run on
port
2、配置主服务器的ip 和端口
我们把监听的端口修改成6380,并且加上权值为2,这里的权值,是用来计算我们需要将哪一台服务器升级升主服务器
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
#
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
#
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
# any way. Sentinel itself will rewrite this configuration file adding
# the slaves using additional configuration options.
# Also note that the configuration file is rewritten when a
# slave is promoted to master.
#
# Note: master name should not include special characters or spaces.
# The valid charset is A-z - and the three characters ".-_".
sentinel monitor mymaster 127.0.0.1
3、启动Sentinel
/sentinel$ redis-sentinel sentinel.conf
sentinel 启动之后,就会监视到现在有一个主服务器,两个从服务器
当我们把其中一个从服务器器关闭之后,我们可以看到日志:
:X Dec ::03.670 # +sdown slave 127.0.0.1: 127.0.0.1 @ mymaster 127.0.0.1
日志表示,6381这个从服务器已经从主服务器中脱离了出来,我们重新把6381 接回去。
:X Dec ::43.288 * +reboot slave 127.0.0.1: 127.0.0.1 @ mymaster 127.0.0.1
:X Dec ::43.365 # -sdown slave 127.0.0.1: 127.0.0.1 @ mymaster 127.0.0.1
4、关闭Master
我们手动关闭Master 之后,sentinel 在监听master 确实是断线了之后,将会开始计算权值,然后重新分配主服务器
我们可以看到,6380主服务器断了之后,sentinel 帮我们选了6382作为新的主服务器
我们进到6382的客户端,查看他的状态:
# Replication
role:master
connected_slaves:
slave0:ip=127.0.0.1,port=,state=online,offset=,lag=
master_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
我们可以看到 6382,重slave 荣升为master
127.0.0.1:> set name jaycekon
OK
原本的没有权限写,也得到了相应的权限
5、重连Master
大家可能会好奇,如果master 重连之后,会不会抢回属于他的位置,答案是否定的,就比如你被一个小弟抢了你老大的位置,他肯给回你这个位置吗。因此当master 回来之后,他也只能当个小弟
4、Sentinel 总结
一、Sentinel的作用:
A、Master 状态监测
B、如果Master 异常,则会进行Master-slave 转换,将其中一个Slave作为Master,将之前的Master作为Slave
C、Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换
二、Sentinel的工作方式:
1):每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个 PING 命令
2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sentinel 标记为主观下线。
3):如果一个Master被标记为主观下线,则正在监视这个Master的所有 Sentinel 要以每秒一次的频率确认Master的确进入了主观下线状态。
4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认Master的确进入了主观下线状态, 则Master会被标记为客观下线
5):在一般情况下, 每个 Sentinel 会以每 10 秒一次的频率向它已知的所有Master,Slave发送 INFO 命令
6):当Master被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave 发送 INFO 命令的频率会从 10 秒一次改为每秒一次
7):若没有足够数量的 Sentinel 同意 Master 已经下线, Master 的客观下线状态就会被移除。
若 Master 重新向 Sentinel 的 PING 命令返回有效回复, Master 的主观下线状态就会被移除。
深入浅出Redis-redis哨兵集群的更多相关文章
- Redis Sentinel哨兵集群
Redis Sentinel(哨兵集群)是一种高可用的redis部署方案.在集群中的redis-master服务挂掉时,无需人为干预,即可通过哨兵集群的自我调整,实现redis服务的持续可用. 哨兵集 ...
- redis的哨兵集群,redis-cluster
#主从同步redis主从优先1.保证数据安全,主从机器两份数据一主多从2.读写分离,缓解主库压力主redis,可读可写slave身份,只读 缺点1.手动主从切换假如主库挂了,得手动切换master ...
- redis:哨兵集群配置
最少配置1主2从3哨兵 一.引言 上一篇文章我们详细的讲解了Redis的主从集群模式,其实这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改,但是有一 ...
- 【Redis学习专题】- Redis主从+哨兵集群部署
集群版本: redis-4.0.14 集群节点: 节点角色 IP redis-master 10.100.8.21 redis-slave1 10.100.8.22 redis-slave2 10.1 ...
- Redis主从&哨兵集群搭建
主从集群 在搭建主从集群前,我们先把Redis安装起来: #解压Redis压缩包 [root@master lf]# tar -zxvf redis-6.2.1.tar.gz -- #安装gcc [r ...
- 三千字介绍Redis主从+哨兵+集群
一.Redis持久化策略 1.RDB 每隔几分钟或者一段时间会将redis内存中的数据全量的写入到一个文件中去. 优点: 因为他是每隔一段时间的全量备份,代表了每个时间段的数据.所以适合做冷备份. R ...
- 走进Redis:哨兵集群
为什么需要哨兵 在 Redis 的主从库模式中,如果从库发生了故障,用户的操作是可以继续进行的,因为写操作是只在主库中进行的.那么,如果主库发生了故障,用户的操作将会收到影响.这时候可能会需要选择一个 ...
- redis之哨兵集群
一.主从复制背景问题 Redis主从复制可将主节点数据同步给从节点,从节点此时有两个作用: 一旦主节点宕机,从节点作为主节点的备份可以随时顶上来. 扩展主节点的读能力,分担主节点读压力. 但是问题是: ...
- redis的哨兵集群,自动切换主从库
Redis-Sentinel是redis官方推荐的高可用性解决方案,当用redis作master-slave的高可用时,如果master本身宕机,redis本身或者客户端都没有实现主从切换的功能. 而 ...
- redis sentinels哨兵集群环境配置
# Redis configuration file example. # # Note that in order to read the configuration file, Redis mus ...
随机推荐
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- SQL Server内存遭遇操作系统进程压榨案例
场景: 最近一台DB服务器偶尔出现CPU报警,我的邮件报警阈(请读yù)值设置的是15%,开始时没当回事,以为是有什么统计类的查询,后来越来越频繁. 探索: 我决定来查一下,究竟是什么在作怪,我排查的 ...
- hadoop 2.7.3本地环境运行官方wordcount-基于HDFS
接上篇<hadoop 2.7.3本地环境运行官方wordcount>.继续在本地模式下测试,本次使用hdfs. 2 本地模式使用fs计数wodcount 上面是直接使用的是linux的文件 ...
- [APUE]标准IO库(下)
一.标准IO的效率 对比以下四个程序的用户CPU.系统CPU与时钟时间对比 程序1:系统IO 程序2:标准IO getc版本 程序3:标准IO fgets版本 结果: [注:该表截取自APUE,上表中 ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- HTML+CSS 项目总结
在过去的大概一个月的学习,基本掌握了HTML+CSS的用法和特性. 这个星期老师给我们布置了一个PC端的实战项目,并且要求在3-4天内完成,我不惜废寝忘食,在紧迫的时间内大致地完成了,但是有些效果不能 ...
- 移动应用App测试与质量管理一
测试工程师 基于Html的WebApp测试, 现在一些移动App混Html5 HTML5性能测试 兼容性 整理后的脑图 测试招聘 弱化大量技术考察 看重看问题的高度 看重潜力 测试经验 质量管理 专项 ...
- Android中的沉浸式状态栏效果
无意间了解到沉浸式状态栏,感觉贼拉的高大上,于是就是试着去了解一下,就有了这篇文章.下面就来了解一下啥叫沉浸式状态栏.传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别.这一样就在一定 ...
- Eclipse使用Git教程
A:点击Window--->Show view--->other..--->Git Repositories--->[OK] B:克隆码云上的代码仓库 C:选择对应目录存储你的 ...