Redis主从加Sentinel模式部署

总体部署

  • 一主二从三哨兵
  • ip地址分配分别为
  1. 主 127.0.0.1:6379
  2. 从 127.0.0.1:6389
  3. 从 127.0.0.1:6399
  4. 哨兵 127.0.0.1:26379
  5. 哨兵 127.0.0.1:26389
  6. 哨兵 127.0.0.1:26399

一主二从的配置文件 redis.conf设置

主的保持默认的不变

二个从的分别打开配置文件

找到设置端口的地方

分别设置为 6389 和6399

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6389
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6399

然后都在

# slaveof <masterip> <masterport>

一行的下面增加

slaveof 127.0.0.1 6379

这样主从的配置就ok了

三个哨兵的sentinel.conf配置

分别打开sentinel.conf配置文件

修改他们的端口为

26379 26389 26399

sentinel monitor mymaster 127.0.0.1 6379 2

配置中的这一行表示 我们要监视的主的ip和端口,mymaster是主的名称,因为我们的主是127.0.0.1:6379,所以三个哨兵中的这个地方都不用修改。

然后所有的配置就ok了,本地测试环境貌似需要改动的地方不多。

启动

进入src目录

./redis-server ../redis.conf

25496:M 18 Feb 21:43:12.616 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6379
 |    `-._   `._    /     _.-‘    |     PID: 25496
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25496:M 18 Feb 21:43:12.618 # Server started, Redis version 3.2.8
25496:M 18 Feb 21:43:12.618 * DB loaded from disk: 0.000 seconds
25496:M 18 Feb 21:43:12.618 * The server is now ready to accept connections on port 6379
25496:M 18 Feb 21:43:21.196 * Slave 127.0.0.1:6389 asks for synchronization
25496:M 18 Feb 21:43:21.196 * Full resync requested by slave 127.0.0.1:6389
25496:M 18 Feb 21:43:21.196 * Starting BGSAVE for SYNC with target: disk
25496:M 18 Feb 21:43:21.197 * Background saving started by pid 25498
25498:C 18 Feb 21:43:21.198 * DB saved on disk
25496:M 18 Feb 21:43:21.198 * Background saving terminated with success
25496:M 18 Feb 21:43:21.198 * Synchronization with slave 127.0.0.1:6389 succeeded
25496:M 18 Feb 21:43:28.979 * Slave 127.0.0.1:6399 asks for synchronization
25496:M 18 Feb 21:43:28.979 * Full resync requested by slave 127.0.0.1:6399
25496:M 18 Feb 21:43:28.979 * Starting BGSAVE for SYNC with target: disk
25496:M 18 Feb 21:43:28.980 * Background saving started by pid 25500
25500:C 18 Feb 21:43:28.986 * DB saved on disk
25496:M 18 Feb 21:43:29.044 * Background saving terminated with success
25496:M 18 Feb 21:43:29.044 * Synchronization with slave 127.0.0.1:6399 succeeded
  • 从1

进入src目录

./redis-server ../redis.conf

25497:S 18 Feb 21:43:21.194 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6389
 |    `-._   `._    /     _.-‘    |     PID: 25497
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25497:S 18 Feb 21:43:21.196 # Server started, Redis version 3.2.8
25497:S 18 Feb 21:43:21.196 * DB loaded from disk: 0.000 seconds
25497:S 18 Feb 21:43:21.196 * The server is now ready to accept connections on port 6389
25497:S 18 Feb 21:43:21.196 * Connecting to MASTER 127.0.0.1:6379
25497:S 18 Feb 21:43:21.196 * MASTER <-> SLAVE sync started
25497:S 18 Feb 21:43:21.196 * Non blocking connect for SYNC fired the event.
25497:S 18 Feb 21:43:21.196 * Master replied to PING, replication can continue...
25497:S 18 Feb 21:43:21.196 * Partial resynchronization not possible (no cached master)
25497:S 18 Feb 21:43:21.197 * Full resync from master: b17cd8764974c68bedacb2e5927a04e9ff15f7df:1
25497:S 18 Feb 21:43:21.198 * MASTER <-> SLAVE sync: receiving 108 bytes from master
25497:S 18 Feb 21:43:21.198 * MASTER <-> SLAVE sync: Flushing old data
25497:S 18 Feb 21:43:21.198 * MASTER <-> SLAVE sync: Loading DB in memory
25497:S 18 Feb 21:43:21.199 * MASTER <-> SLAVE sync: Finished with success
  • 从2

进入src目录

./redis-server ../redis.conf

25499:S 18 Feb 21:43:28.975 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 6399
 |    `-._   `._    /     _.-‘    |     PID: 25499
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25499:S 18 Feb 21:43:28.977 # Server started, Redis version 3.2.8
25499:S 18 Feb 21:43:28.977 * DB loaded from disk: 0.000 seconds
25499:S 18 Feb 21:43:28.977 * The server is now ready to accept connections on port 6399
25499:S 18 Feb 21:43:28.979 * Connecting to MASTER 127.0.0.1:6379
25499:S 18 Feb 21:43:28.979 * MASTER <-> SLAVE sync started
25499:S 18 Feb 21:43:28.979 * Non blocking connect for SYNC fired the event.
25499:S 18 Feb 21:43:28.979 * Master replied to PING, replication can continue...
25499:S 18 Feb 21:43:28.979 * Partial resynchronization not possible (no cached master)
25499:S 18 Feb 21:43:28.980 * Full resync from master: b17cd8764974c68bedacb2e5927a04e9ff15f7df:15
25499:S 18 Feb 21:43:29.044 * MASTER <-> SLAVE sync: receiving 108 bytes from master
25499:S 18 Feb 21:43:29.045 * MASTER <-> SLAVE sync: Flushing old data
25499:S 18 Feb 21:43:29.045 * MASTER <-> SLAVE sync: Loading DB in memory
25499:S 18 Feb 21:43:29.045 * MASTER <-> SLAVE sync: Finished with success
  • 哨兵1

进入src目录

./redis-sentinel ../sentinel.conf

25501:X 18 Feb 21:43:35.300 * Increased maximum number of open files to 10032 (it was originally set to 4864).
                _._
           _.-``__ ‘‘-._
      _.-``    `.  `_.  ‘‘-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._
 (    ‘      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 26379
 |    `-._   `._    /     _.-‘    |     PID: 25501
  `-._    `-._  `-./  _.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io
  `-._    `-._`-.__.-‘_.-‘    _.-‘
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|
 |    `-._`-._        _.-‘_.-‘    |
  `-._    `-._`-.__.-‘_.-‘    _.-‘
      `-._    `-.__.-‘    _.-‘
          `-._        _.-‘
              `-.__.-‘

25501:X 18 Feb 21:43:35.301 # Sentinel ID is 6f3360c06cafe7807e2c1ae00fe7c791354b8b24
25501:X 18 Feb 21:43:35.301 # +monitor master mymaster 127.0.0.1 6379 quorum 2
  • 哨兵2和哨兵3启动过程相同

验证

新开一个命令行窗口进入redis的src目录,用redis-cli工具登录其中一个哨兵

./redis-cli -p 26379

连接成功后运行如下命令

sentinel master mymaster

结果如下

127.0.0.1:26379> sentinel master mymaster
 1) "name"
 2) "mymaster"
 3) "ip"
 4) "127.0.0.1"
 5) "port"
 6) "6379"
 7) "runid"
 8) "b17cd8764974c68bedacb2e5927a04e9ff15f7df"
 9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "703"
19) "last-ping-reply"
20) "703"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "1090"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "3253725"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

参考

https://redis.io/topics/sentinel

时间: 2024-10-23 22:02:11

Redis主从加Sentinel模式部署的相关文章

redis 主从同步&amp;哨兵模式&amp;codis

原文:redis 主从同步&哨兵模式&codis 主从同步 1.CPA原理 1. CPA原理是分布式存储理论的基石: C(一致性):   A(可用性):  P(分区容忍性); 2. 当主从网络无法连通时,修改操作无法同步到节点,所以"一致性"无法满足 3. 除非我们牺牲"可用性",也就是暂停分布式节点服务,不再提供修改数据功能,知道网络恢复 一句话概括CAP: 当网络分区发生时,一致性 和 可用性 两难全 2.redis主从同步介绍 1. 和MySQ

Redis高可用方案(redis主从+keepalived+sentinel)

架构:redis主从+keepalived+sentinel 三台机器,两台redis主从,一台配合选举sentinel leader机器. Master:  192.168.100.135    controller         部署redis+keepalived+sentinel Slave:  192.168.100.136     web-nb-136     部署redis+keepalived+sentinel 配合sentinel:  192.168.100.128    

Redis主从、sentinel故障自动切换

一.什么是redis主从复制? 主从复制,当用户往Master端写入数据时,通过Redis Sync机制将数据文件发送至Slave,Slave也会执行相同的操作确保数据一致:且实现Redis的主从复制非常简单. 二.redis主从复制特点 1.同一个Master可以拥有多个Slaves. 2.Master下的Slave还可以接受同一架构中其它slave的链接与同步请求,实现数据的级联复制,即Master->Slave->Slave模式: 3.Master以非阻塞的方式同步数据至slave,这将

Redis 主从+哨兵安装与部署

1 前言 redis集群常见的部署方式有: 采用主从复制 + sentinel(哨兵)监控的方式; 采用redis-cluster集群(去中心化分片集群)的方式 本文以笔者内部项目为例,采用主从复制+哨兵的方式部署. 2 版本说明 三台服务器(一主两从):192.168.1.60 为主服务器,192.168.1.59 和192.168.1.58 为从服务器 操作系统:CentOS Linux release 7.4.1708 (Core) JDK版本:jdk 1.8.0_171 REDIS版本:

配置redis主从复制和sentinel模式

测试环境: 机器:192.168.110.132 redis主端口:6379 redis从端口:6380 redis从端口:6381 sentinel端口:26379 操作系统版本:CentOS release 6.5 (Final) redis版本:3.2.6 Linux系统安装redis: 1.下载redis: 登陆redis官网https://redis.io/download,下载最新稳定版源码包redis-3.2.6.tar.gz.解压后进行编译即可. tar -zxvf redis-

redis主从分离讲解与部署

redis主从架构 -> 读写分离架构 -> 可支持水平扩展的读高并发架构 redis replication的核心机制 (1)redis采用异步方式复制数据到slave节点,不过redis 2.8开始,slave node会周期性地确认自己每次复制的数据量 (2)一个master node是可以配置多个slave node的 (3)slave node也可以连接其他的slave node (4)slave node做复制的时候,是不会block master node的正常工作的 (5)sl

Redis主从复制与sentinel模式

第1章 Redis主从复制: 基于RDB持久化的功能来实现主从复制的功能 1.1 redis复制特性: 1.      使用异步复制 2.      一个主服务器可以有多个从服务器 3.      从服务器也可以有自己的从服务器 4.      复制功能不会阻塞主服务器 5.      可以通过复制功能来让主服务器免于执行持久化操作,由从服务器执行持久化操作即可 1.1 主从复制原理: 1.      从服务器向主服务器发送sync命令 2.      街道sync命令的主服务器会调用bgsav

MySQL 主从同步(1) - 概念和原理介绍 以及 主从/主主模式 部署记录

Mysql复制概念Mysql内建的复制功能是构建大型高性能应用程序的基础, 将Mysql数据分布到多个系统上,这种分布机制是通过将Mysql某一台主机数据复制到其它主机(slaves)上,并重新执行一遍来实现的.复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置.从服务器接收从那时起

spring-data-redis,jedis和redis主从集成和遇到的问题

Redis主从加哨兵的部署详见http://www.cnblogs.com/dupang/p/6414365.html spring-data-redis和jedis集成代码总体结构 代码地址https://github.com/dupang/redistestwithspring pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XM