主從複製#
Redis 5.0 之後提供了REPLICAOF替代了之前的SLAVEOF命令來實現Master-Slave之間的數據交換 接下來我們設置主從複製模型:
- Master節點
首先修改redis-master.conf配置文件來配置端口和安全認證
<code>bind 127.0.0.1
port 6379 #端口
requirepass redis #配置密碼之後需要先認證,然後才能繼續進行後續操作/<code>
- Slave節點
首先修改redis-slave.conf配置文件來配置端口和安全認證
<code>bind 127.0.0.1
port 6379 #端口
masterauth redis #配置密碼之後需要先認證,然後才能繼續進行後續操作
replicaof 127.0.0.1 6379 #設置其為127.0.0.1:6379的備機 /<code>
啟動主節點
<code>redis-server redis-master.conf
29035:C 18 Mar 2019 23:21:32.272 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29035:C 18 Mar 2019 23:21:32.273 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=29035, just started
29035:C 18 Mar 2019 23:21:32.273 # Configuration loaded
29035:M 18 Mar 2019 23:21:32.275 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 29035
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
29035:M 18 Mar 2019 23:21:32.287 # Server initialized
29035:M 18 Mar 2019 23:21:32.289 * DB loaded from disk: 0.003 seconds
29035:M 18 Mar 2019 23:21:32.289 * Ready to accept connections
29035:M 18 Mar 2019 23:26:58.112 * Replica 127.0.0.1:6380 asks for synchronization
29035:M 18 Mar 2019 23:26:58.112 * Full resync requested by replica 127.0.0.1:6380
29035:M 18 Mar 2019 23:26:58.112 * Starting BGSAVE for SYNC with target: disk
29035:M 18 Mar 2019 23:26:58.112 * Background saving started by pid 29298
29298:C 18 Mar 2019 23:26:58.114 * DB saved on disk
29035:M 18 Mar 2019 23:26:58.118 * Background saving terminated with success
29035:M 18 Mar 2019 23:26:58.119 * Synchronization with replica 127.0.0.1:6380 succeeded/<code>
啟動從節點
<code>redis-server redis-slave.conf
29297:C 18 Mar 2019 23:26:58.107 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29297:C 18 Mar 2019 23:26:58.107 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=29297, just started
29297:C 18 Mar 2019 23:26:58.107 # Configuration loaded
29297:S 18 Mar 2019 23:26:58.108 * Increased maximum number of open files to 10032 (it was originally set to 4864).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.3 (00000000/0) 64 bit
.-`` .-```. ```\\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6380
| `-._ `._ / _.-' | PID: 29297
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
29297:S 18 Mar 2019 23:26:58.110 # Server initialized
29297:S 18 Mar 2019 23:26:58.110 * DB loaded from disk: 0.000 seconds
29297:S 18 Mar 2019 23:26:58.110 * Ready to accept connections
29297:S 18 Mar 2019 23:26:58.110 * Connecting to MASTER 127.0.0.1:6379
29297:S 18 Mar 2019 23:26:58.110 * MASTER REPLICA sync started
29297:S 18 Mar 2019 23:26:58.111 * Non blocking connect for SYNC fired the event.
29297:S 18 Mar 2019 23:26:58.111 * Master replied to PING, replication can continue...
29297:S 18 Mar 2019 23:26:58.111 * Partial resynchronization not possible (no cached master)
29297:S 18 Mar 2019 23:26:58.113 * Full resync from master: e98b4a9238559875100344dea137f9a803c5f805:0
29297:S 18 Mar 2019 23:26:58.119 * MASTER REPLICA sync: receiving 365 bytes from master
29297:S 18 Mar 2019 23:26:58.119 * MASTER REPLICA sync: Flushing old data
29297:S 18 Mar 2019 23:26:58.119 * MASTER REPLICA sync: Loading DB in memory
29297:S 18 Mar 2019 23:26:58.119 * MASTER REPLICA sync: Finished with success/<code>
從節點啟動之後會自動從主節點同步數據,接下來我們測試從主節點寫入數據,從節點讀數據
主節點寫入mastermsg
<code>redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> auth redis
OK
127.0.0.1:6379> set mastermsg "master msgs"
OK/<code>
從節點讀mastermsg
<code>redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> get msg
"hello world"
127.0.0.1:6380> get mastermsg #第一次獲取時沒有獲取到,因為還未同步
(nil)
127.0.0.1:6380> get mastermsg
"master msgs"
127.0.0.1:6380>/<code>
哨兵(Sentinel)#
哨兵(Sentinel)是Redis的高可用性解決方案,由一個或多個Sentinel實例組成的Sentinel系統,可以監視任意多個主服務器以及主服務器下的從服務器。並在主服務器下線狀態是,自動將從服務器選舉為新的主服務器,然後由新的主服務器代替已下線的主服務器繼續處理命令請求。
假設由四個服務器server1 為主服務器,server2,server3,server4 為從服務器 主從切換按照如下步驟進行:
- server1 主服務器下線
- server2,server3,server4 從服務器終端與主服務器的複製操作
- Sentinel 系統察覺到 server1下線
- Sentinel會從從服務器列表中選擇一個服務器作為新的主服務器,假設為server2
- Sentinel系統向除server2以外的其他從服務器發送新的複製指令讓他們從server2複製數據
- Sentinel監視到server1重新上線,會將其重新加入到server2的從服務器列表
Sentinel 配置文件:
配置哨兵時,最好禁用redis.conf中的requirepass,否則哨兵啟動時會認為主機下線,或者需要在哨兵配置文件中增加sentinel auth-pass master-name password
master 配置文件
<code># The port that this sentinel instance will run on
port 26379
# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
daemonize no
# When running daemonized, Redis Sentinel writes a pid file in
# /var/run/redis-sentinel.pid by default.
pidfile /var/run/redis-sentinel.pid
# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output.
logfile ""
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2 #配置監視的Redis 主機,判斷一個主機下線需要兩個哨兵達成一致
sentinel down-after-milliseconds mymaster 5000 #Redis主機斷線5秒認為其下線
sentinel parallel-syncs mymaster 1 #選項指定了在執行故障轉移時, 最多可以有多少個從服務器同時對新的主服務器進行同步
sentinel failover-timeout mymaster 10000 #故障轉移超時時間
sentinel deny-scripts-reconfig yes/<code>
slave1 配置文件
<code>port 26380
# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized.
daemonize no
pidfile "/var/run/redis-sentinel-slave1.pid"
sentinel myid 524b588bb23f15e6a2f749698ff7359c0ae6a2f1
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 1
# Generated by CONFIG REWRITE
dir "/Users/jet"
protected-mode no
sentinel leader-epoch mymaster 1
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6379
sentinel known-sentinel mymaster 127.0.0.1 26381 8a1794781323d413d9753c497a637c7cb9ca1462
sentinel known-sentinel mymaster 127.0.0.1 26379 524b588bb23f15e6a2f749698ff7359c0ae6a2f0
sentinel current-epoch 1/<code>
slave 2 配置文件
<code>port 26381
# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized.
daemonize no
pidfile "/var/run/redis-sentinel-slave2.pid"
sentinel myid 8a1794781323d413d9753c497a637c7cb9ca1462
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel config-epoch mymaster 1
# Generated by CONFIG REWRITE
dir "/Users/jet"
protected-mode no
sentinel leader-epoch mymaster 1
sentinel known-replica mymaster 127.0.0.1 6381
sentinel known-replica mymaster 127.0.0.1 6379
sentinel known-sentinel mymaster 127.0.0.1 26380 524b588bb23f15e6a2f749698ff7359c0ae6a2f1
sentinel known-sentinel mymaster 127.0.0.1 26379 524b588bb23f15e6a2f749698ff7359c0ae6a2f0
sentinel current-epoch 1/<code>
操作結果如圖:
如果設置了密碼,執行完故障轉移之後哨兵會修改配置文件,哨兵會重寫master,slave, 之前如果配置了密碼此時很可能會產生AUTH失敗的問題
閱讀更多 JetQin90 的文章