redis主从集群搭建eclipse_Redis主从加Sentinel模式部署

本文介绍了Redis一主二从三哨兵集群的搭建与验证。首先进行总体部署,分配IP地址,接着分别设置一主二从的redis.conf配置文件和三个哨兵的sentinel.conf配置文件,然后依次启动主、从和哨兵,最后通过redis-cli工具登录哨兵进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总体部署

一主二从三哨兵

ip地址分配分别为

主 127.0.0.1:6379

从 127.0.0.1:6389

从 127.0.0.1:6399

哨兵 127.0.0.1:26379

哨兵 127.0.0.1:26389

哨兵 127.0.0.1:26399

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

主的保持默认的不变

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

找到设置端口的地方

分别设置为 6389 和6399

# Accept connections on the specified port, default is 6379 (IANA #815344).

# If port0 isspecified Redis will not listen on a TCP socket.

port6389

# Accept connections on the specified port, default is 6379 (IANA #815344).

# If port0 isspecified Redis will not listen on a TCP socket.

port6399

然后都在

# slaveof

一行的下面增加

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) 64bit

.-`` .-```. ```\/ _.,_ ''-._

(', .-` | `, ) 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.000seconds25496: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 forsynchronization25496: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 forSYNC with target: disk25496:M 18 Feb 21:43:21.197 * Background saving started by pid 25498

25498:C 18 Feb 21:43:21.198 *DB saved on disk25496:M 18 Feb 21:43:21.198 *Background saving terminated with success25496:M 18 Feb 21:43:21.198 * Synchronization with slave 127.0.0.1:6389succeeded25496:M 18 Feb 21:43:28.979 * Slave 127.0.0.1:6399 asks forsynchronization25496: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 forSYNC with target: disk25496:M 18 Feb 21:43:28.980 * Background saving started by pid 25500

25500:C 18 Feb 21:43:28.986 *DB saved on disk25496:M 18 Feb 21:43:29.044 *Background saving terminated with success25496: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) 64bit

.-`` .-```. ```\/ _.,_ ''-._

(', .-` | `, ) 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.000seconds25497: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 started25497: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 frommaster25497:S 18 Feb 21:43:21.198 * MASTER SLAVE sync: Flushing old data25497:S 18 Feb 21:43:21.198 * MASTER SLAVE sync: Loading DB inmemory25497: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) 64bit

.-`` .-```. ```\/ _.,_ ''-._

(', .-` | `, ) 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.000seconds25499: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 started25499: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 frommaster25499:S 18 Feb 21:43:29.045 * MASTER SLAVE sync: Flushing old data25499:S 18 Feb 21:43:29.045 * MASTER SLAVE sync: Loading DB inmemory25499: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) 64bit

.-`` .-```. ```\/ _.,_ ''-._

(', .-` | `, ) Running in sentinel mode

|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379

| `-._ `._ / _.-'| PID: 25501

`-._ `-._ `-./ _.-'_.-'

|`-._`-._ `-.__.-'_.-'_.-'|

| `-._`-._ _.-'_.-' | http://redis.io

`-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-'_.-'_.-'|

| `-._`-._ _.-'_.-' |`-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-'_.-'`-._ _.-' `-.__.-'

25501:X 18 Feb 21:43:35.301 # Sentinel ID is6f3360c06cafe7807e2c1ae00fe7c791354b8b2425501: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 mymaster1) "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"

参考

插播个广告

### 如何搭建配置 Redis 主从复制集群 #### 配置环境准备 为了确保主从复制能够正常工作,需要先准备好运行环境。如果本地已安装了 Redis,则需拷贝 Redis 的配置文件至指定位置[^3]。 ```bash echo 6001 6002 6003 | xargs -t -n 1 cp /usr/local/etc/redis.conf ``` 此命令会将 `/usr/local/etc/redis.conf` 文件分别拷贝到端口为 `6001`, `6002`, 和 `6003` 对应的目录下,以便后续修改这些副本作为不同实例的基础配置。 #### 修改配置文件 创建三个不同的 Redis 实例来模拟主服务器和两个从服务器的情况。对于每一个实例都需要单独编辑其对应的配置文件: - 将 `port` 参数设置成不同的值(比如分别为 6001, 6002, 6003),以区分各个服务进程; - 设置 `pidfile` 及 `logfile` 使用各自独立的日志记录路径; - 关键在于为主节点设定 `save` 命令保存策略并开启 RDB 功能;而对所有从节点则要添如下行指明它们所跟随的主机地址与端口号: ```properties slaveof 127.0.0.1 6001 ``` 以上操作通过配置参数方式完成主从关系建立[^2]。 #### 启动多个 Redis 实例 依次启动这三个经过特别配置后的 Redis 进程。可以利用后台执行的方式让程序持续监听各自的端口而不阻塞终端输入输出流。 ```bash redis-server ./6001/redis.conf & redis-server ./6002/redis.conf & redis-server ./6003/redis.conf & ``` 此时应该能看到三台相互关联的服务正在稳定提供访问接口。 #### 测试主从同步效果 借助客户端工具如 Navicat 或者直接采用命令行形式向主节点写入测试数据,并观察其他两处是否能及时接收到相同的信息更新[^5]。 例如,在主节点上执行 SET 操作之后立即切换到任意一个从属设备查询相同的 key 是否存在预期 value 即可验证整个体系结构的有效性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值