Redis-ha(sentinel)搭建

服務器描述:本次搭建是用來測試,所以是在一臺服務器上搭建三個redis服務(一主兩從)

服務角色

端口

Redis.conf名稱

sentinel配置文件名稱

sentinel端口

redis日志路徑

sentinel路勁

主(master)

6379

redis.conf

sentinel.conf

26379

/home/zhangxs/data/redislog/redis_server/master.log

/home/zhangxs/data/redislog/sentinel/sentinel6379.log

從(slave)

6380

redis_slave6380.conf

Sentinel6380.conf

26380

/home/zhangxs/data/redislog/redis_server/slave6380.log

/home/zhangxs/data/redislog/sentinel/sentinel6380.log

從(slave)

6381

redis_slave6381.conf

Sentinel6381.conf

26381

/home/zhangxs/data/redislog/redis_server/slave6381.log

/home/zhangxs/data/redislog/sentinel/sentinel6381.log

?

修改配置文件

  • 1: redis.conf

修改redis服務日志路徑:logfile "/home/zhangxs/data/redislog/redis_server/master.log"

其他沒有修改,使用的是默認配置

?

  • 2:redis_slave6380.conf 和redis_slave6381.conf? (copy redis_slave.conf)
  1. 設置他們指向的master服務的ip和端口:slaveof 127.0.0.1 6379(兩個文件都配置)
  2. 修改redis_slave6380.conf 和redis_slave6381.conf 端口:redis_slave6380.conf? 端口為26380 ; redis_slave6381.conf 端口為26381
  3. 設置slave服務日志路徑: logfile /home/zhangxs/data/redislog/redis_server/slave6380.log 和 logfile /home/zhangxs/data/redislog/redis_server/slave6381.log

?

  • 3:sentinel.conf
  1. 修改日志路徑:logfile "/home/zhangxs/data/redislog/sentinel/sentinel6379.log"

?

  • 4:Sentinel6380.conf 和 Sentinel6381.conf (copy sentinel.conf)
  1. 修改端口號:Sentinel6380.conf 改為 26380;?? Sentinel6381.conf 改為 26381;
  2. 修改日志路徑:logfile "/home/zhangxs/data/redislog/sentinel/sentinel6380.log" 和?logfile "/home/zhangxs/data/redislog/sentinel/sentinel6381.log"

?

?

上面配置好后,啟動redis服務

  • 1:啟動master
src/redis-server redis.conf&

?

  • 2:啟動從服務(slave6380)
src/redis-server redis_slave6380.conf &

?

查看主服務日志,會發現多出來一段

2755:M 29 Jul 00:13:25.469 * Starting BGSAVE for SYNC with target: disk
2755:M 29 Jul 00:13:25.502 * Background saving started by pid 2784
2784:C 29 Jul 00:13:25.608 * DB saved on disk
2784:C 29 Jul 00:13:25.608 * RDB: 6 MB of memory used by copy-on-write
2755:M 29 Jul 00:13:25.677 * Background saving terminated with success
2755:M 29 Jul 00:13:25.677 * Synchronization with slave 127.0.0.1:6380 succeeded   //同步到從服務127.0.0.1:6380成功

?

查看從服務日志slave6380.log

2780:S 29 Jul 00:13:25.436 * DB loaded from disk: 0.007 seconds
2780:S 29 Jul 00:13:25.436 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
2780:S 29 Jul 00:13:25.436 * Ready to accept connections
2780:S 29 Jul 00:13:25.436 * Connecting to MASTER 127.0.0.1:6379 //正在連接master服務
2780:S 29 Jul 00:13:25.436 * MASTER <-> SLAVE sync started //master到slave 啟動同步
2780:S 29 Jul 00:13:25.436 * Non blocking connect for SYNC fired the event.
2780:S 29 Jul 00:13:25.436 * Master replied to PING, replication can continue...
2780:S 29 Jul 00:13:25.436 * Trying a partial resynchronization (request a26696cff5d35f38896c4eb068f71adbb7cfc421:474104).
2780:S 29 Jul 00:13:25.577 * Full resync from master: 541cd938f43b4f144e647881af409fa1884ea5a4:0 //從master全量同步
2780:S 29 Jul 00:13:25.577 * Discarding previously cached master state.//丟棄之前緩存的master狀態
2780:S 29 Jul 00:13:25.677 * MASTER <-> SLAVE sync: receiving 250 bytes from master //slave從master 同步250個字節
2780:S 29 Jul 00:13:25.677 * MASTER <-> SLAVE sync: Flushing old data
2780:S 29 Jul 00:13:25.677 * MASTER <-> SLAVE sync: Loading DB in memory
2780:S 29 Jul 00:13:25.678 * MASTER <-> SLAVE sync: Finished with success

?

  • 3:啟動從服務(slave6381)
src/redis-server redis_slave6381.conf &

?

查看主服務日志,會發現多出來一段

//從服務127.0.0.1:6381發送同步請求
2755:M 29 Jul 00:26:46.531 * Slave 127.0.0.1:6381 asks for synchronization //接受127.0.1:6381的部分重同步請求。從偏移量1開始發送積壓的1106字節 2755:M 29 Jul 00:26:46.531 * Partial resynchronization request from 127.0.0.1:6381 accepted. Sending 1106 bytes of backlog starting from offset 1.

?

查看從服務日志slave6381.log

2809:S 29 Jul 00:26:46.531 * DB loaded from disk: 0.000 seconds
2809:S 29 Jul 00:26:46.531 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
2809:S 29 Jul 00:26:46.531 * Ready to accept connections
2809:S 29 Jul 00:26:46.531 * Connecting to MASTER 127.0.0.1:6379
2809:S 29 Jul 00:26:46.531 * MASTER <-> SLAVE sync started
2809:S 29 Jul 00:26:46.531 * Non blocking connect for SYNC fired the event.
2809:S 29 Jul 00:26:46.531 * Master replied to PING, replication can continue...
2809:S 29 Jul 00:26:46.531 * Trying a partial resynchronization (request 541cd938f43b4f144e647881af409fa1884ea5a4:1).
2809:S 29 Jul 00:26:46.531 * Successful partial resynchronization with master.
2809:S 29 Jul 00:26:46.532 * MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization

?

  • 4:測試數據同步功能(使用redis-cli 連接服務器)

1:連接master

[root@vm1 src]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set name fj

?

2:連接slave6380

[root@vm1 src]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> get name
"fj"
127.0.0.1:6380>

?

3:連接slave6381

[root@vm1 src]# redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> get name
"fj"
127.0.0.1:6381>

?

Ok 主從同步沒有問題。

默認情況下從服務是不允許set數據的,測試下

127.0.0.1:6380> set name hello
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380>
127.0.0.1:6381> set name hello
(error) READONLY You can't write against a read only slave.
127.0.0.1:6381>

?

啟動各個服務的sentinel

?

  • 啟動sentinel6379
src/redis-sentinel sentinel.conf &

?

  • 查看Sentinel6379.log
2908:X 29 Jul 01:01:32.838 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2908:X 29 Jul 01:01:32.839 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=2908, just started
2908:X 29 Jul 01:01:32.839 # Configuration loaded
2908:X 29 Jul 01:01:32.839 * Increased maximum number of open files to 10032 (it was originally set to 1024).
2908:X 29 Jul 01:01:32.840 * Running mode=sentinel, port=26379.
2908:X 29 Jul 01:01:32.840 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2908:X 29 Jul 01:01:32.855 # Sentinel ID is 1a77392638e41bb0ea0a865ffc93b8de6335227f
2908:X 29 Jul 01:01:32.855 # +monitor master mymaster 127.0.0.1 6379 quorum 2
//一個新的從服務器已經被sentinel識別并關聯
2908:X 29 Jul 01:01:32.856 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
2908:X 29 Jul 01:01:32.858 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379

?

  • 啟動sentinel6380
 src/redis-sentinel sentinel6380.conf &

?

  • 查看 Sentinel6380.log
2937:X 29 Jul 01:08:14.325 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=2937, just started
2937:X 29 Jul 01:08:14.325 # Configuration loaded
2937:X 29 Jul 01:08:14.327 * Increased maximum number of open files to 10032 (it was originally set to 1024).
2937:X 29 Jul 01:08:14.377 * Running mode=sentinel, port=26380.
2937:X 29 Jul 01:08:14.377 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2937:X 29 Jul 01:08:14.379 # Sentinel ID is 4a6aebffdd1301bf054e722c34e8a6611418ba8a
2937:X 29 Jul 01:08:14.379 # +monitor master mymaster 127.0.0.1 6379 quorum 2
2937:X 29 Jul 01:08:14.380 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
2937:X 29 Jul 01:08:14.381 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
//一個新的sentinel(監控)已經被識別并關聯
2937:X 29 Jul 01:08:14.919 * +sentinel sentinel 1a77392638e41bb0ea0a865ffc93b8de6335227f 127.0.0.1 26379 @ mymaster 127.0.0.1 6379

?

  • Sentinel6380啟動后會發現,Sentinel6379.log 加了一段日志
//一個新的sentinel(監控)已經被識別并關聯
2908:X 29 Jul 01:08:16.367 * +sentinel sentinel 4a6aebffdd1301bf054e722c34e8a6611418ba8a 127.0.0.1 26380 @ mymaster 127.0.0.1 6379

新啟動一個sentinel,會通過發布訂閱功能自動發現監控相同master下的其他sentinel。這一功能是通過向頻道 sentinel:hello 發送信息來實現的。

?

  • 啟動sentinel6381
[root@vm1 redis-4.0.10]# src/redis-sentinel sentinel6381.conf &

?

查看 Sentinel6381.log

2961:X 29 Jul 01:11:09.823 # Configuration loaded
2961:X 29 Jul 01:11:09.823 * Increased maximum number of open files to 10032 (it was originally set to 1024).
2961:X 29 Jul 01:11:09.852 * Running mode=sentinel, port=26381.
2961:X 29 Jul 01:11:09.852 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2961:X 29 Jul 01:11:09.853 # Sentinel ID is 1db1a4dcdf0ecca00b64d9362c2a2dd338da0030
2961:X 29 Jul 01:11:09.853 # +monitor master mymaster 127.0.0.1 6379 quorum 2
2961:X 29 Jul 01:11:09.853 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
2961:X 29 Jul 01:11:09.855 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
2961:X 29 Jul 01:11:10.334 * +sentinel sentinel 1a77392638e41bb0ea0a865ffc93b8de6335227f 127.0.0.1 26379 @ mymaster 127.0.0.1 6379
2961:X 29 Jul 01:11:11.446 * +sentinel sentinel 4a6aebffdd1301bf054e722c34e8a6611418ba8a 127.0.0.1 26380 @ mymaster 127.0.0.1 6379

?

?

  • 新加入的sentinel6381, sentinel6379和sentinel6380 都會收到通知(//一個新的sentinel(監控)已經被識別并關聯)

2908:X 29 Jul 01:11:11.880 * +sentinel sentinel 1db1a4dcdf0ecca00b64d9362c2a2dd338da0030 127.0.0.1 26381 @ mymaster 127.0.0.1 6379

2937:X 29 Jul 01:11:11.878 * +sentinel sentinel 1db1a4dcdf0ecca00b64d9362c2a2dd338da0030 127.0.0.1 26381 @ mymaster 127.0.0.1 6379

?

sentinel的狀態都會記錄到sentinel.conf文件中,用于啟動后恢復狀態,查看下各個sentinel.conf 文件 變動后的部分

  • sentinel.conf

啟動前:無???? 啟動后:sentinel myid 1a77392638e41bb0ea0a865ffc93b8de6335227f //自己的sentinel myid

啟動前:無???? 啟動后:

# Generated by CONFIG REWRITE

#master下得兩個從服務

sentinel known-slave mymaster 127.0.0.1 6380

sentinel known-slave mymaster 127.0.0.1 6381

#master下其他兩個sentinel

sentinel known-sentinel mymaster 127.0.0.1 26380 4a6aebffdd1301bf054e722c34e8a6611418ba8a

sentinel known-sentinel mymaster 127.0.0.1 26381 1db1a4dcdf0ecca00b64d9362c2a2dd338da0030

sentinel current-epoch 0

?

Sentinel6380.conf和Sentinel6381.conf改動和sentinel.conf 基本一樣。不一樣的就是 記錄自己sentinel myid和master下其他兩個sentinel不一樣,大同小異。

?

測試故障遷移

Sentinel 故障遷移我使用的是默認配置(不需要再配置,可以自定義修改)

//判斷master失效,至少有兩個sentinel同意才會執行故障遷移
sentinel monitor mymaster 127.0.0.1 6379 2 //如果在10秒內sentinel 都收到master的一次有效回復,就認為該master主觀下線 sentinel down-after-milliseconds mymaster 60000 sentinel failover-timeout mymaster 180000
//在執行故障轉移時,同時只有一個slave能對新的master進行數據同步
sentinel parallel-syncs mymaster 1
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

?

  • 1:查看redis的相關服務
root       2755   2551  0 00:11 pts/2    00:00:06 src/redis-server 127.0.0.1:6379
root       2780   2551  0 00:13 pts/2    00:00:06 src/redis-server 127.0.0.1:6380
root       2809   2551  0 00:26 pts/2    00:00:05 src/redis-server 127.0.0.1:6381
root       2816   2529  0 00:30 pts/1    00:00:00 redis-cli -h 127.0.0.1 -p 6379
root       2822   2530  0 00:33 pts/0    00:00:00 redis-cli -h 127.0.0.1 -p 6380
root       2841   2823  0 00:34 pts/6    00:00:00 redis-cli -h 127.0.0.1 -p 6381
root       2908   2551  0 01:01 pts/2    00:00:07 src/redis-sentinel *:26379 [sentinel]
root       2937   2551  0 01:08 pts/2    00:00:06 src/redis-sentinel *:26380 [sentinel]
root       2961   2551  0 01:11 pts/2    00:00:06 src/redis-sentinel *:26381 [sentinel]
root       3000   2551  0 01:50 pts/2    00:00:00 grep --color=auto redis

?

?

  • 2:查看整個備份狀態
127.0.0.1:6379> info# Server
# Clients
# Memory
# Persistence
# Stats
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=544591,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=544591,lag=0
master_replid:541cd938f43b4f144e647881af409fa1884ea5a4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:544857
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:544857
# CPU# Cluster# Keyspace

?

其他信息我都刪掉了,只留下【Replication】的信息,其他信息可以在redis-cli 命令行中使用【info】命令查看

可以看到,6379是master角色,master下有兩個從服務port=6380,port=6381

?

  • 3: Kill 掉 master,觀察日志
kill -9 2755

?

master被干掉了,所以master.log 沒有日志,看其他兩個從服務日志(截取部分)

redis_slave6380.log

///一分鐘內
2780:S 29 Jul 01:58:53.414 # Connection with master lost.
2780:S 29 Jul 01:58:53.414 * Caching the disconnected master state.
2780:S 29 Jul 01:58:54.163 * Connecting to MASTER 127.0.0.1:6379
2780:S 29 Jul 01:58:54.163 * MASTER <-> SLAVE sync started
2780:S 29 Jul 01:58:54.164 # Error condition on socket for SYNC: Connection refused
2780:S 29 Jul 01:58:55.168 * Connecting to MASTER 127.0.0.1:6379
2780:S 29 Jul 01:58:55.169 * MASTER <-> SLAVE sync started
....
...
...
2780:S 29 Jul 01:59:22.381 # Error condition on socket for SYNC: Connection refused
2780:S 29 Jul 01:59:23.389 * Connecting to MASTER 127.0.0.1:6379
2780:S 29 Jul 01:59:23.389 * MASTER <-> SLAVE sync started
2780:S 29 Jul 01:59:23.389 # Error condition on socket for SYNC: Connection refused
///一分鐘后
2780:S 29 Jul 01:59:24.321 * SLAVE OF 127.0.0.1:6381 enabled (user request from 'id=8 addr=127.0.0.1:52556 fd=11 name=sentinel-4a6aebff-cmd age=3070 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=133 qbuf-free=32635 obl=36 oll=0 omem=0 events=r cmd=exec')
2780:S 29 Jul 01:59:24.323 # CONFIG REWRITE executed with suc
2780:S 29 Jul 01:59:24.399 * Connecting to MASTER 127.0.0.1:6381
2780:S 29 Jul 01:59:24.399 * MASTER <-> SLAVE sync started
2780:S 29 Jul 01:59:24.399 * Non blocking connect for SYNC fired the event.
2780:S 29 Jul 01:59:24.399 * Master replied to PING, replication can continue...
2780:S 29 Jul 01:59:24.399 * Trying a partial resynchronization (request 541cd938f43b4f144e647881af409fa1884ea5a4:617714).
2780:S 29 Jul 01:59:24.400 * Successful partial resynchronization with master.
2780:S 29 Jul 01:59:24.400 # Master replication ID changed to 514edab0972b4b6e5388edc4f14fbdb4d223d39e
2780:S 29 Jul 01:59:24.400 * MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.

?

從01:58:53.414 到01:59:23.389 這一分鐘內一直在嘗試連接master,一分鐘內沒有連接成功后,sentinel 就會master判斷為主觀下線,看日志

Sentinel6379.log

//判定master 主觀下線
2908:X 29 Jul 01:59:23.492 # +sdown master mymaster 127.0.0.1 6379
//當前的紀元(epoch)已經被更新。
2908:X 29 Jul 01:59:23.546 # +new-epoch 1
 //開始給sentinel6380投票,誰來主導這次故障轉移
2908:X 29 Jul 01:59:23.549 # +vote-for-leader 4a6aebffdd1301bf054e722c34e8a6611418ba8a 1
//判定master 客觀觀下線,已經有2個sentinel同意
2908:X 29 Jul 01:59:23.569 # +odown master mymaster 127.0.0.1 6379 #quorum 3/2
2908:X 29 Jul 01:59:23.569 # Next failover delay: I will not start a failover before Sun Jul 29 02:05:23 2018
2908:X 29 Jul 01:59:24.328 # +config-update-from sentinel 4a6aebffdd1301bf054e722c34e8a6611418ba8a 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
2908:X 29 Jul 01:59:24.328 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381
2908:X 29 Jul 01:59:24.328 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
2908:X 29 Jul 01:59:24.328 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
2908:X 29 Jul 01:59:54.333 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

?

?

Sentinel6380.log

//判定master 主觀下線
2937:X 29 Jul 01:59:23.459 # +sdown master mymaster 127.0.0.1 6379
//判定master 客觀觀下線,已經有2個sentinel同意
2937:X 29 Jul 01:59:23.536 # +odown master mymaster 127.0.0.1 6379 #quorum 2/2
2937:X 29 Jul 01:59:23.537 # +new-epoch 1
//嘗試故障轉移master
2937:X 29 Jul 01:59:23.537 # +try-failover master mymaster 127.0.0.1 6379
 //開始給sentinel6380投票,誰來主導這次故障轉移
2937:X 29 Jul 01:59:23.540 # +vote-for-leader 4a6aebffdd1301bf054e722c34e8a6611418ba8a 1
//其他兩個sentinel 都投票給4a6aebffdd1301bf054e722c34e8a6611418ba8a 【6380sentinel】
2937:X 29 Jul 01:59:23.549 # 1db1a4dcdf0ecca00b64d9362c2a2dd338da0030 voted for 4a6aebffdd1301bf054e722c34e8a6611418ba8a 1
2937:X 29 Jul 01:59:23.549 # 1a77392638e41bb0ea0a865ffc93b8de6335227f voted for 4a6aebffdd1301bf054e722c34e8a6611418ba8a 1
//6379這個服務贏得選舉可以進行故障轉移
2937:X 29 Jul 01:59:23.619 # +elected-leader master mymaster 127.0.0.1 6379
//發現6379這個服務是故障轉移狀態,就開始選擇master下得從服務
2937:X 29 Jul 01:59:23.619 # +failover-state-select-slave master mymaster 127.0.0.1 6379
//故障轉移操作現在處于 select-slave 狀態 —— Sentinel 正在尋找可以升級為主服務器的從服務器。(選擇mymaster 127.0.0.1 6379 下 6381 的從服務)
2937:X 29 Jul 01:59:23.710 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
//Sentinel 正在將6379下的從服務器6381升級為主服務器,等待升級功能完成。
2937:X 29 Jul 01:59:23.710 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
//master下的從服務 6381  等待升級
2937:X 29 Jul 01:59:23.769 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
//升級master下從服務6381
2937:X 29 Jul 01:59:24.251 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
//故障轉移狀態切換到了 reconf-slaves 狀態。(再次確認從服務器轉為主服務器)
2937:X 29 Jul 01:59:24.251 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
//牽頭的sentinel 向6380從服務器發送slaveof 指令,將它設置為新的master
2937:X 29 Jul 01:59:24.321 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
//6379不再處于客觀下線狀態,客觀下線狀態只用于master服務,6379已經不是master了
2937:X 29 Jul 01:59:24.653 # -odown master mymaster 127.0.0.1 6379
//6380服務正在將自己設置為6381主服務的從服務器,還未完成
2937:X 29 Jul 01:59:25.270 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
//從服務器6380已經完成對新master服務的同步
2937:X 29 Jul 01:59:25.271 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
//master6379 故障轉移結束,所有的從服務器開始同步新的master
2937:X 29 Jul 01:59:25.347 # +failover-end master mymaster 127.0.0.1 6379
//配置變更主服務器的ip地址已經改變, 選擇master 為6381
2937:X 29 Jul 01:59:25.347 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381
//6381下的兩個從服務(新的從服務被識別并關聯)
2937:X 29 Jul 01:59:25.347 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
2937:X 29 Jul 01:59:25.347 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
//添加master下從服務6379 為客觀下線
2937:X 29 Jul 01:59:55.401 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

?

?

Sentinel6381.log

//判定master 主觀下線
2961:X 29 Jul 01:59:23.459 # +sdown master mymaster 127.0.0.1 6379 2961:X 29 Jul 01:59:23.545 # +new-epoch 1
//開始給6380投票 2961:X 29 Jul 01:59:23.548 # +vote-for-leader 4a6aebffdd1301bf054e722c34e8a6611418ba8a 1 2961:X 29 Jul 01:59:24.325 # +config-update-from sentinel 4a6aebffdd1301bf054e722c34e8a6611418ba8a 127.0.0.1 26380 @ mymaster 127.0.0.1 6379 2961:X 29 Jul 01:59:24.325 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381 2961:X 29 Jul 01:59:24.325 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381 2961:X 29 Jul 01:59:24.325 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381 2961:X 29 Jul 01:59:54.348 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

可以看到在 01:59:23秒 也就是一分鐘之后,三個監控master的sentinel 都判定了master為主觀下線(sdown),我們配置的至少有2個sentinel 同意master 主觀下線,master就會被切換到客觀下線(odown) 【+odown master mymaster 127.0.0.1 6379 #quorum 2/2】。當判斷master為客觀下線后,sentinel 就開始選舉出新的master,可以看到Sentinel6380.log 日志要比其他的sentinel.log多,因為整個選舉的過程是Sentinel6380 在牽頭執行。

?

4:在6381下查看整個服務的備份狀態

# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=1125775,lag=0
master_replid:514edab0972b4b6e5388edc4f14fbdb4d223d39e
master_replid2:541cd938f43b4f144e647881af409fa1884ea5a4
master_repl_offset:1125775
second_repl_offset:617714
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:77200
repl_backlog_histlen:1048576

可以看到6381的角色成為了master,只有一個slave,因為另一個掛了。

?

5:再次啟動6379服務

查看6379服務的日志

3037:S 29 Jul 02:46:47.753 # CONFIG REWRITE executed with success.
3037:S 29 Jul 02:46:48.359 * Connecting to MASTER 127.0.0.1:6381
3037:S 29 Jul 02:46:48.360 * MASTER <-> SLAVE sync started
3037:S 29 Jul 02:46:48.360 * Non blocking connect for SYNC fired the event.
3037:S 29 Jul 02:46:48.361 * Master replied to PING, replication can continue...
3037:S 29 Jul 02:46:48.362 * Trying a partial resynchronization (request 7b0dc6ac9c2188e3c92eb29eea200ea6c572619c:1).
3037:S 29 Jul 02:46:48.608 * Full resync from master: 514edab0972b4b6e5388edc4f14fbdb4d223d39e:1178142
3037:S 29 Jul 02:46:48.608 * Discarding previously cached master state.
3037:S 29 Jul 02:46:48.708 * MASTER <-> SLAVE sync: receiving 253 bytes from master
3037:S 29 Jul 02:46:48.708 * MASTER <-> SLAVE sync: Flushing old data
3037:S 29 Jul 02:46:48.708 * MASTER <-> SLAVE sync: Loading DB in memory
3037:S 29 Jul 02:46:48.708 * MASTER <-> SLAVE sync: Finished with success
3037:S 29 Jul 02:46:48.709 * Background append only file rewriting started by pid 3042
3037:S 29 Jul 02:46:48.750 * AOF rewrite child asks to stop sending diffs.
3042:C 29 Jul 02:46:48.750 * Parent agreed to stop sending diffs. Finalizing AOF...
3042:C 29 Jul 02:46:48.750 * Concatenating 0.00 MB of AOF diff received from parent.
3042:C 29 Jul 02:46:48.750 * SYNC append only file rewrite performed
3042:C 29 Jul 02:46:48.750 * AOF rewrite: 6 MB of memory used by copy-on-write
3037:S 29 Jul 02:46:48.781 * Background AOF rewrite terminated with success
3037:S 29 Jul 02:46:48.782 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
3037:S 29 Jul 02:46:48.782 * Background AOF rewrite finished successfully

?

看到啟動后重寫配置文件,然后自動連接6381這個新的master服務,開始從master 上全量同步數據

?

查看6381這個新master日志

?

//響應6379的同步請求
2809:M 29 Jul 02:46:48.362 * Slave 127.0.0.1:6379 asks for synchronization //不接受同步部分數據請求 2809:M 29 Jul 02:46:48.362 * Partial resynchronization not accepted: Replication ID mismatch (Slave asked for '7b0dc6ac9c2188e3c92eb29eea200ea6c572619c', my replication IDs are '514edab0972b4b6e5388edc4f14fbdb4d223d39e' and '541cd938f43b4f144e647881af409fa1884ea5a4') //開始同步 2809:M 29 Jul 02:46:48.362 * Starting BGSAVE for SYNC with target: disk 2809:M 29 Jul 02:46:48.607 * Background saving started by pid 3041 3041:C 29 Jul 02:46:48.607 * DB saved on disk #6m內存用于寫復制 3041:C 29 Jul 02:46:48.608 * RDB: 6 MB of memory used by copy-on-write //后臺保存成功 2809:M 29 Jul 02:46:48.708 * Background saving terminated with success 2809:M 29 Jul 02:46:48.708 * Synchronization with slave 127.0.0.1:6379 succeeded

?

查看sentinel日志

6379sentinel.log

//減去6379服務的主觀下線狀態
2908:X 29 Jul 02:46:37.610 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
//轉換為master6381 下的從服務
2908:X 29 Jul 02:46:47.628 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

?

6380sentinel.log

2937:X 29 Jul 02:46:37.767 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

?

6381sentinel.log

2961:X 29 Jul 02:46:38.023 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381

?

再看下6381這個新master的整個備份信息

# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=1348132,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=1348132,lag=0
master_replid:514edab0972b4b6e5388edc4f14fbdb4d223d39e
master_replid2:541cd938f43b4f144e647881af409fa1884ea5a4
master_repl_offset:1348132
second_repl_offset:617714
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:299557
repl_backlog_histlen:1048576

新的master 增加了一個slave6379 從服務

?

我們再搭建前做的redis配置,當故障轉移成功后,這些配置會被重寫,重寫的內容基本都在配置文件的最后

Redis.conf配置文件,多了

# Generated by CONFIG REWRITE
slaveof 127.0.0.1 6381

?

Redis6381.conf配置文件沒有變

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6381

?

新的master配置,也就是redis_slave6381.conf 已經沒有了slaveof 配置

# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# slaveof <masterip> <masterport>

Sentinel.conf 也會發生變化,可以自己去看看

?

6:再次測試故障轉移后的 同步功能

之前的master已經不再支持set

127.0.0.1:6379> set name zhangxs
(error) READONLY You can't write against a read only slave.

新master set成功

127.0.0.1:6381> set name zhangxs
OK
127.0.0.1:6379> get name
"zhangxs"
127.0.0.1:6380> get name
"zhangxs"

同步沒問題。

?

轉移后的服務器變成了

服務角色

端口

Redis.conf名稱

sentinel配置文件名稱

sentinel端口

redis日志路徑

sentinel路勁

從(master)

6379

redis.conf

sentinel.conf

26379

/home/zhangxs/data/redislog/redis_server/master.log

/home/zhangxs/data/redislog/sentinel/sentinel6379.log

從(slave)

6380

redis_slave6380.conf

Sentinel6380.conf

26380

/home/zhangxs/data/redislog/redis_server/slave6380.log

/home/zhangxs/data/redislog/sentinel/sentinel6380.log

主(slave)

6381

redis_slave6381.conf

Sentinel6381.conf

26381

/home/zhangxs/data/redislog/redis_server/slave6381.log

/home/zhangxs/data/redislog/sentinel/sentinel6381.log

?

?

?

參考文檔:http://www.redis.cn/topics

轉載于:https://www.cnblogs.com/zhangXingSheng/p/9385885.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/451856.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/451856.shtml
英文地址,請注明出處:http://en.pswp.cn/news/451856.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

學成在線--26.課程圖片管理(圖片刪除)

文章目錄一. 需求分析二. API三. 服務端開發1. Dao2. Service3. Controller四. 前端開發1. API方法2. 頁面1.before-remove鉤子方法2.handleRemove鉤子方法一. 需求分析 課程圖片上傳成功后&#xff0c;可以重新上傳&#xff0c;方法是先刪除現有圖片再上傳新圖片&#xff1b;…

警惕開源代碼庫中的安全隱患

最近的一項研究發現&#xff0c; 在調查的31個流行庫&#xff08;框架&#xff09;的1261個版本中&#xff0c;超過三分之一存在已知的安全漏洞&#xff0c;大約四分之一的下載文件已經被污染。 該項研究由Aspect Security和Sonatype發起。Aspect Security是一家評估軟件安全漏…

jsp注釋

jsp注釋 <%--注釋內容--%> html注釋 <!--注釋內容-->

線程間的協作(3)——管道輸入/輸出流

2019獨角獸企業重金招聘Python工程師標準>>> 1.管道輸入/輸出流類 分為兩類&#xff0c;字節流管道類&#xff08;PipedInputStream/PipedOutputStream&#xff09;和字符流管道類&#xff08;PipedReader/ PipedWriter&#xff09;。這兩個IO流實現了可以在不同的任…

windows簡易版本 Redis 使用 demo樣例(ssm框架下)

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 在網上下載 windows 版本 的Redis 。下載了直接解壓出來 &#xff1a; 2. 雙擊 redis-server.exe 啟動服務&#xff08;如下圖&#…

Redhat7.3安裝配置Telnet詳細教程

請參考&#xff1a;https://blog.csdn.net/weixin_39934520/article/details/84835949 謝謝樓主分享&#xff01;

程序員的半衰期只有15年?

曾在Google工作負責過技術工作的科技編輯 Matt Heusser總結了他在Google的生活經歷&#xff0c;得出結論&#xff1a; 作為程序員&#xff0c;你只有15年時間。Matt 寫道當我在Google工作時&#xff0c;發現Google大部分人都是20出頭的年輕人&#xff0c;他們經歷的很多事情都是…

EasyNVR、EasyDSS二次開發之:RTMP、HLS流在web頁面進行無插件播放示例Demo代碼

不管是基于EasyNVR還是EasyDSS&#xff0c;都是支持無插件直播&#xff0c;這也是未來視頻直播的一個趨勢。對于傳統的瀏覽器插件播放誰用誰知道&#xff1b; 以上是軟件自帶播放展示 背景需求 對于EasyNVR和EasyDSS的使用方式大概分為兩大類&#xff0c;一類是直接將軟件作為視…

jsp中%@ % 與% % 與%! %

<% %> 有個符號的&#xff0c;叫做指令用來提供整個JSP 網頁相關的信息&#xff0c;并且用來設定JSP網頁的相關屬性&#xff0c; 例如&#xff1a;網頁的編碼方式、語法、信息等。<% %>這個叫做小腳本&#xff0c;是寫java代碼的<%! %>這個是jsp中腳本聲明&a…

Hadoop的學習路線圖

目錄&#xff1a;.1.Hadoop家族產品2.Hadoop家族學習路線圖 Hadoop家族產品截止到2013年&#xff0c;根據cloudera的統計&#xff0c;Hadoop家族產品已經達到20個&#xff01;接下來&#xff0c;我把這20個產品&#xff0c;分成了2類。?第一類&#xff0c;是我已經掌握的?第二…

new TypeToken<List>>(){}.getType() 是什么意思

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 項目中代碼&#xff1a; List<AppVersion> redisList new Gson().fromJson(json, new TypeToken<List<AppVersion>…

11--移除重復節點

編寫代碼&#xff0c;移除未排序鏈表中的重復節點。保留最開始出現的節點。 示例1: 輸入&#xff1a;[1, 2, 3, 3, 2, 1] 輸出&#xff1a;[1, 2, 3] 示例2: 輸入&#xff1a;[1, 1, 1, 1, 2] 輸出&#xff1a;[1, 2]

信息圖:程序員/開發人員實際在用哪些工具

BestVendor.com的工作人員在全球范圍內采訪了500名重要開發人員&#xff0c;在調查詢問他們實際使用的工具后&#xff0c;制作了一張信息圖&#xff0c;如下。 這張信息圖覆蓋10個方面的工具&#xff1a;Bug 追蹤、數據庫、開發框架、集成開發環境&#xff08;IDE&#xff09;、…

class特性

每個HTML元素都可以附帶一個class特性。有時候&#xff0c;你希望有一種方法可以指定多個元素并將這些元素和頁面上的其他元素區分出來&#xff0c;而不是單獨指定文檔中的某個元素。 <!DOCTYPE html> <!-- To change this license header, choose License Headers in…

Xcode代碼提示聯想功能失效,按command鍵點不進去類庫,提示“?”

一大早電腦重啟了下&#xff0c;打開項目之后出現了一堆問號&#xff0c;懷疑是Xcode 抽風了&#xff0c;本著懷疑的態度&#xff0c;新建了項目&#xff0c;一波操作下來是正常的&#xff0c;代碼能聯想&#xff0c;command也好使。于是在網上找答案&#xff0c;終于在這里找到…

12-- 缺失的第一個正數

文章目錄1.問題描述2.解題代碼1.問題描述 給你一個未排序的整數數組&#xff0c;請你找出其中沒有出現的最小的正整數。 示例 1: 輸入: [1,2,0] 輸出: 3 示例 2: 輸入: [3,4,-1,1] 輸出: 2 示例 3: 輸入: [7,8,9,11,12] 輸出: 1 提示&#xff1a; 你的算法的時間復雜度應…

java中的private public protected

1、public&#xff1a;public表明該數據成員、成員函數是對所有用戶開放的&#xff0c;所有用戶都可以直接進行調用 2、private&#xff1a;private表示私有&#xff0c;私有的意思就是除了class自己之外&#xff0c;任何人都不可以直接使用&#xff0c;私有財產神圣不可侵…

7款最流行的在線項目管理工具推薦

本文介紹一些非常流行的在線項目管理工具&#xff0c;希望你喜歡。 1. Apptivo 如果你是一個創業的或自由開發人員&#xff0c;或者屬于一個小型專業團隊&#xff0c;Apptivo這個項目管理軟件很值得你你研究。實質上&#xff0c;它是一個基于Web的項目管理應用&#xff0c;讓你…

VR全景看年評!PConline年度評測盛典等你來體驗

【PConline 2016年度評測】又是一年春草綠&#xff0c;馬上還有十天扒拉月2016年就快過完了。每年到這個時候&#xff0c;我們不禁都會拷問自己&#xff1a;你存到錢了么&#xff1f;娶到媳婦了么&#xff1f;買到房了么&#xff1f;答案是都沒有&#xff01;哈哈&#xff0c;這…

swagger詳解

1快速環境搭建 pom.xml文件中添加如下內容(看清楚再復制&#xff0c;此處不是全部內容) 1 <properties>2 ...3 <swagger.version>2.2.2</swagger.version>4 ...5 </properties>6 7 <dependency>8 …