redis----主二从三哨兵环境配置

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: redis----主二从三哨兵环境配置

Sentinel(哨兵)是用于监控redis集群中Master状态的工具,是Redis的高可用性解决方案,sentinel哨兵模式已经被集成在redis2.4之后的版本中。sentinel是redis高可用的解决方案,sentinel系统可以监视一个或者多个redis master服务,以及这些master服务的所有从服务;当某个master服务下线时,自动将该master下的某个从服务升级为master服务替代已下线的master服务继续处理请求。

 

 

 

sentinel可以让redis实现主从复制,当一个集群中的master失效之后,sentinel可以选举出一个新的master用于自动接替master的工作,集群中的其他redis服务器自动指向新的master同步数据。一般建议sentinel采取奇数台,防止某一台sentinel无法连接到master导致误切换。

 

 

 

Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都没有实现自动进行主备切换,而Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自动切换。Sentinel由一个或多个Sentinel 实例 组成的Sentinel系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。

 

 

 

Sentinel工作方式(每个Sentinel实例都执行的定时任务)

 

1)每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个PING命令。

 

2)如果一个实例(instance)距离最后一次有效回复PING命令的时间超过 own-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的主观下线状态就会被移除。

 

###主重模式只是复制数据不会选举新的节点###

------使用命令---------

#主从复制

01-192.168.31.134(主)

02-192.168.31.251(从)

03-192.168.31.163(从)

 

#在从机器执行

02-192.168.31.251(从)

127.0.0.1:6379> slaveof 192.168.31.134 6379

 

#在从机器执行

03-192.168.31.163(从)

127.0.0.1:6379> slaveof 192.168.31.134 6379

 

#取消主重,就是不想成为任何机器的从节点:

#取消 02-192.168.31.251 slaveof no one

127.0.0.1:6379> slaveof no one

OK

 

#取消 03-192.168.31.163 slaveof no one

127.0.0.1:6379> slaveof no one

OK

------使用配置---------

1.01-192.168.31.134的redis.conf配置文件不变

2.分别在redis.conf进行配置

02-192.168.31.251

03-192.168.31.163

slaveof 192.168.31.134 6379

slave-read-only yes

 

3.格式如下:

slaveof IP port

slave-read-only yes

 

 

###哨兵会选取新的节点###

#配置哨兵需要先配置主重,然后配置哨兵。

1.主从配置按照以上的配置就可以

主节点:

 

vi redis.conf

 

bind 127.0.0.1 192.168.31.134

 

protected-mode no

 

daemonize yes

 

slave-priority 100

 

appendonly yes

 

dir /data/usr/redis/data

 

requirepass "Redis2019!"

 

masterauth "Redis2019!"

 

logfile "/data/usr/redis/logs/redis.log"

 

两个从节点 192.168.31.251:

 

bind 127.0.0.1 192.168.31.251

 

protected-mode no

 

daemonize yes

 

slave-priority 90

 

appendonly yes

 

dir /data/usr/redis/data

 

slaveof 192.168.31.134 6379

 

requirepass "Redis2019!"

 

masterauth "Redis2019!"

 

logfile "/data/usr/redis/logs/redis.log"

 

 

两个从节点 192.168.31.163:

bind 127.0.0.1 192.168.31.163

 

protected-mode no

 

daemonize yes

 

slave-priority 90

 

appendonly yes

 

dir /data/usr/redis/data

 

slaveof 192.168.31.134 6379

 

requirepass "Redis2019!"

 

masterauth "Redis2019!"

 

logfile "/data/usr/redis/logs/redis.log"

配置哨兵(192.168.31.134 )

 

vi sentinel.conf

 

protected-mode no

 

daemonize yes

 

logfile "/data/usr/redis/logs/sentinel.log"

#设置 主名称 ip地址 端口号 参入选举的哨兵数

sentinel monitor mymaster 192.168.31.134 6379 2

 

sentinel auth-pass mymaster Redis2019!

 

 

启动

 

将redis服务启动:先启动master再启动slave

 

/data/usr/redis/bin/redis-server /data/usr/redis/conf/redis.conf

 

查看redis服务状态

 

/data/usr/redis/bin/redis-cli -h 192.168.121.121 -p 6379 -a Redis2019! info replication

 

/data/usr/redis/bin/redis-cli -h 192.168.121.122 -p 6379 -a Redis2019! info replication

 

/data/usr/redis/bin/redis-cli -h 192.168.121.123 -p 6379 -a Redis2019! info replication

 

启动sentinel服务:先启动redis服务再启动sentinel服务,关闭则是反过来的,先关闭sentinel服务,再关闭redis服务。

 

/data/usr/redis/bin/redis-sentinel /data/usr/redis/conf/sentinel.conf

 

查看哨兵服务状态

 

/data/usr/redis/bin/redis-cli -h 192.168.121.121 -p 26379 info sentinel

 

/data/usr/redis/bin/redis-cli -h 192.168.121.122 -p 26379 info sentinel

 

/data/usr/redis/bin/redis-cli -h 192.168.121.123 -p 26379 info sentinel

 

4、测试

 

4.1、同步测试

 

主节点:

 

192.168.121.121:6379> set name kkk

 

 

从节点:

 

192.168.121.122:6379> keys *

 

1) "name"

 

192.168.121.122:6379> get name

 

"kkk"

 

192.168.121.123:6379> keys *

 

1) "name"

 

192.168.121.123:6379> get name

 

"kkk"

 

 

 

4.2、从库只读测试

 

192.168.121.122:6379> set bname yyy

 

(error) READONLY You can't write against a read only slave.

 

 

 

192.168.121.123:6379> set bname yyy

 

(error) READONLY You can't write against a read only slave.

 

 

 

4.3、冗余测试

 

测试停止主redis,发现其中一个从变为主,启动停止的服务后发现为从服务,主服务不变。

 

 

 

停止一个节点sentinel服务,之后再停止主服务,发现其中一个从服务升级为主服务

 

停止哨兵服务命令

 

/data/usr/redis/bin/redis-cli -h 192.168.121.121 -p 26379 shutdown

 

停止redis服务命令

 

/data/usr/redis/bin/redis-cli -h 192.168.121.122 -p 6379 -a Redis2019 shutdown

 

 

 

停止第二个哨兵服务,再停止redis主服务,发现剩下的redis服务中并没有产生主服务,说明哨兵服务最少要有两个,最好是三个及以上。

 

 

 

5、问题处理

 

5.1、执行 make PREFIX=/data/usr/redis install 报错

 

 

 

cc: error: ../deps/hiredis/libhiredis.a: No such file or directory

 

cc: error: ../deps/lua/src/liblua.a: No such file or directory

 

 

 

cd deps/

 

ls

 

hiredis  jemalloc linenoise  lua Makefile  README.md update-jemalloc.sh

 

 

 

cd hiredis

 

make

 

 

 

cd linenoise

 

make

 

 

 

cd lua

 

make

 

 

 

cd lua

 

make

 

编译报错

 

Please do

 

  make PLATFORM

 

where PLATFORM is one of these:

 

  aix ansi bsd freebsd generic linux macosx mingw posix solaris

 

See INSTALL for complete instructions.

 

[root@slave1 lua]# make PLATFORM

 

make: *** No rule to make target `PLATFORM'. Stop.

 

 

 

执行下面命令编译

 

make generic

 

 

 

5.2、启动哨兵出错

 

./redis-sentinel sentinel.conf

 

 

 

*** FATAL CONFIG FILE ERROR ***

 

Reading the configuration file, at line 92

 

>>> 'sentinel auth-pass mymaster Redis2019'

 

No such master with specified name.

 

 

 

出现错误:

 

sentinel monitor mymaster 192.168.121.123 6379 2

 

sentinel auth-pass mymaster Redis2019

 

 

 

上边的配置要按照顺序,错误是因为上边一条的配置在下面造成的。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2天前
|
NoSQL Redis Sentinel
【怒怼大厂面试官】听说你精通Redis?说说Redis哨兵
面试官:Redis哨兵知道吧?知道的,Sentinel哨兵本质是一个运行在特殊模式下的Redis服务器。面试官:嗯然后呢?它的主要作用是通过检测Redis主从服务器的下线状态,选举出新Redis主服务器,也就是故障转移,来保证Redis的高可用性。
80 4
【怒怼大厂面试官】听说你精通Redis?说说Redis哨兵
|
2天前
|
NoSQL Java Redis
SpringBoot2.0整合Redis高可用之Sentinel哨兵
本篇博文分享的是一主二从三哨兵模式。至于为什么用三个哨兵,同第一段。本文是模拟环境,都是一个服务器上面。
86 0
|
2天前
|
存储 NoSQL Redis
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群(下)
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群
19 1
|
2天前
|
监控 NoSQL Redis
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群(上)
Redis源码、面试指南(5)多机数据库、复制、哨兵、集群
34 0
|
2天前
|
存储 监控 NoSQL
Redis哨兵&分片集群
Redis哨兵&分片集群
20 0
|
2天前
|
监控 NoSQL 算法
Redis 搭建哨兵集群
Redis 搭建哨兵集群
24 1
|
6月前
|
存储 监控 NoSQL
redis主从模式,redis哨兵模式,redis集群模式
redis主从模式,redis哨兵模式,redis集群模式
redis主从模式,redis哨兵模式,redis集群模式
|
2天前
|
存储 监控 NoSQL
Redis 架构深入:主从复制、哨兵到集群
大家好,我是小康,今天我们来聊下 Redis 的几种架构模式,包括主从复制、哨兵和集群模式。
Redis 架构深入:主从复制、哨兵到集群
|
2天前
|
NoSQL Redis Docker
关于redis的一主三从三哨兵的实现
关于redis的一主三从三哨兵的实现
28 0
|
2天前
|
监控 NoSQL 程序员
Redis 高可用篇:你管这叫 Sentinel 哨兵集群原理
Redis 高可用篇:你管这叫 Sentinel 哨兵集群原理
79 5