Redis主从加Sentinel模式部署

时间:2023-03-10 00:01:01
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  (IANA #).
# If port is specified Redis will not listen on a TCP socket.
port
# Accept connections on the specified port, default is  (IANA #).
# If port is specified Redis will not listen on a TCP socket.
port

然后都在

# slaveof <masterip> <masterport>

一行的下面增加

slaveof 127.0.0.1 

这样主从的配置就ok了

三个哨兵的sentinel.conf配置

分别打开sentinel.conf配置文件

修改他们的端口为

26379 26389 26399

sentinel monitor mymaster 127.0.0.1  

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

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

启动

进入src目录

./redis-server ../redis.conf

:M  Feb ::12.616 * Increased maximum number of open files to  (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 25496
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :M Feb ::12.618 # Server started, Redis version 3.2.
:M Feb ::12.618 * DB loaded from disk: 0.000 seconds
:M Feb ::12.618 * The server is now ready to accept connections on port
:M Feb ::21.196 * Slave 127.0.0.1: asks for synchronization
:M Feb ::21.196 * Full resync requested by slave 127.0.0.1:
:M Feb ::21.196 * Starting BGSAVE for SYNC with target: disk
:M Feb ::21.197 * Background saving started by pid
:C Feb ::21.198 * DB saved on disk
:M Feb ::21.198 * Background saving terminated with success
:M Feb ::21.198 * Synchronization with slave 127.0.0.1: succeeded
:M Feb ::28.979 * Slave 127.0.0.1: asks for synchronization
:M Feb ::28.979 * Full resync requested by slave 127.0.0.1:
:M Feb ::28.979 * Starting BGSAVE for SYNC with target: disk
:M Feb ::28.980 * Background saving started by pid
:C Feb ::28.986 * DB saved on disk
:M Feb ::29.044 * Background saving terminated with success
:M Feb ::29.044 * Synchronization with slave 127.0.0.1: succeeded
  • 从1

进入src目录

./redis-server ../redis.conf

:S  Feb ::21.194 * Increased maximum number of open files to  (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 25497
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :S Feb ::21.196 # Server started, Redis version 3.2.
:S Feb ::21.196 * DB loaded from disk: 0.000 seconds
:S Feb ::21.196 * The server is now ready to accept connections on port
:S Feb ::21.196 * Connecting to MASTER 127.0.0.1:
:S Feb ::21.196 * MASTER <-> SLAVE sync started
:S Feb ::21.196 * Non blocking connect for SYNC fired the event.
:S Feb ::21.196 * Master replied to PING, replication can continue...
:S Feb ::21.196 * Partial resynchronization not possible (no cached master)
:S Feb ::21.197 * Full resync from master: b17cd8764974c68bedacb2e5927a04e9ff15f7df:
:S Feb ::21.198 * MASTER <-> SLAVE sync: receiving bytes from master
:S Feb ::21.198 * MASTER <-> SLAVE sync: Flushing old data
:S Feb ::21.198 * MASTER <-> SLAVE sync: Loading DB in memory
:S Feb ::21.199 * MASTER <-> SLAVE sync: Finished with success
  • 从2

进入src目录

./redis-server ../redis.conf

:S  Feb ::28.975 * Increased maximum number of open files to  (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 25499
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :S Feb ::28.977 # Server started, Redis version 3.2.
:S Feb ::28.977 * DB loaded from disk: 0.000 seconds
:S Feb ::28.977 * The server is now ready to accept connections on port
:S Feb ::28.979 * Connecting to MASTER 127.0.0.1:
:S Feb ::28.979 * MASTER <-> SLAVE sync started
:S Feb ::28.979 * Non blocking connect for SYNC fired the event.
:S Feb ::28.979 * Master replied to PING, replication can continue...
:S Feb ::28.979 * Partial resynchronization not possible (no cached master)
:S Feb ::28.980 * Full resync from master: b17cd8764974c68bedacb2e5927a04e9ff15f7df:
:S Feb ::29.044 * MASTER <-> SLAVE sync: receiving bytes from master
:S Feb ::29.045 * MASTER <-> SLAVE sync: Flushing old data
:S Feb ::29.045 * MASTER <-> SLAVE sync: Loading DB in memory
:S Feb ::29.045 * MASTER <-> SLAVE sync: Finished with success
  • 哨兵1

进入src目录

./redis-sentinel ../sentinel.conf

:X  Feb ::35.300 * Increased maximum number of open files to  (it was originally set to ).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2. (/) bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port:
| `-._ `._ / _.-' | PID: 25501
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-' :X Feb ::35.301 # Sentinel ID is 6f3360c06cafe7807e2c1ae00fe7c791354b8b24
:X Feb ::35.301 # +monitor master mymaster 127.0.0.1 quorum
  • 哨兵2和哨兵3启动过程相同

验证

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

./redis-cli -p 

连接成功后运行如下命令

sentinel master mymaster

结果如下

127.0.0.1:> sentinel master mymaster
) "name"
) "mymaster"
) "ip"
) "127.0.0.1"
) "port"
) ""
) "runid"
) "b17cd8764974c68bedacb2e5927a04e9ff15f7df"
) "flags"
) "master"
) "link-pending-commands"
) ""
) "link-refcount"
) ""
) "last-ping-sent"
) ""
) "last-ok-ping-reply"
) ""
) "last-ping-reply"
) ""
) "down-after-milliseconds"
) ""
) "info-refresh"
) ""
) "role-reported"
) "master"
) "role-reported-time"
) ""
) "config-epoch"
) ""
) "num-slaves"
) ""
) "num-other-sentinels"
) ""
) "quorum"
) ""
) "failover-timeout"
) ""
) "parallel-syncs"
) ""

参考

https://redis.io/topics/sentinel

插播个广告

Redis主从加Sentinel模式部署
老丈人家的粉皮儿,农产品,没有乱七八糟的添加剂,欢迎惠顾