溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Redis之-哨兵模式原理

發(fā)布時(shí)間:2020-03-02 11:27:00 來(lái)源:網(wǎng)絡(luò) 閱讀:575 作者:yangjunfeng 欄目:關(guān)系型數(shù)據(jù)庫(kù)

master服務(wù)器異常down機(jī)后,兩個(gè)原有的slave1,slave2服務(wù)器接管服務(wù),如slave1變成新的master服務(wù)器,slave2變成slave1的從庫(kù)。

配置文件主要參數(shù)講解:

sentinel monitor mymaster 127.0.0.1 6379 1 幾個(gè)哨兵發(fā)現(xiàn)down才認(rèn)為真正的down

sentinel down-after-milliseconds mymaster 30000 多少毫秒后連接不到master認(rèn)為斷開(kāi)

sentinel parallel-syncs mymaster 1 同時(shí)把幾臺(tái)master指到新的master機(jī)器。

sentinel failover-timeout mymaster 180000 多長(zhǎng)時(shí)間失敗

啟動(dòng)哨兵
[root@ZFRC-YW-YJF-TEST-370123 redis]# ./bin/redis-server ./sentinel.conf --sentinel
17400:X 28 Jun 17:17:32.853 # Not listening to IPv6: unsupproted
.
_.-__ ''-._ <br/>_.- .. ''-. Redis 3.2.13 (00000000/0) 64 bit
.-.-```. ```\/ _.,_ ''-._ <br/>( ' , .-` | `, ) Running in sentinel mode<br/>|`-._`-...-` __...-.-.|'` .-'| Port: 26379
| -._. / .-' | PID: 17400
-._-. `-./ .-' .-'
|`-.
-._-..-' .-'.-'|
| -._-. .-'.-' | http://redis.io
`-.
-._-.
.-'.-' .-'
|-._-._ -.__.-' _.-'_.-'| <br/>|-.`-. .-'.-' |
-._-._-.__.-'_.-' _.-' <br/>-._ -.__.-' _.-' <br/>-. .-'
`-.__.-'

17400:X 28 Jun 17:17:32.854 # Sentinel ID is b81b851b02fec76bcfc7144b0a675fdedecf7188
17400:X 28 Jun 17:17:32.854 # +monitor master mymaster 127.0.0.1 6379 quorum 1
17400:X 28 Jun 17:17:32.854 +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:17:32.855
+slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379

測(cè)試將master down,查看哨兵是否有故障轉(zhuǎn)移
[root@ZFRC-YW-YJF-TEST-370123 ~]# cd /usr/local/redis/
[root@ZFRC-YW-YJF-TEST-370123 redis]# ./bin/redis-cli
127.0.0.1:6379> shutdown
not connected>

日志打印出了一些枚舉的過(guò)程,關(guān)鍵字switch為master機(jī)

17400:X 28 Jun 17:19:03.363 # +sdown master mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.363 # +odown master mymaster 127.0.0.1 6379 #quorum 1/1
17400:X 28 Jun 17:19:03.363 # +new-epoch 1
17400:X 28 Jun 17:19:03.363 # +try-failover master mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.364 # +vote-for-leader b81b851b02fec76bcfc7144b0a675fdedecf7188 1
17400:X 28 Jun 17:19:03.364 # +elected-leader master mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.364 # +failover-state-select-slave master mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.464 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.464 +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.564
+failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.917 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:03.917 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:04.006 +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:04.982
+slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:04.982 +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:05.064 # +failover-end master mymaster 127.0.0.1 6379
17400:X 28 Jun 17:19:05.064 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
17400:X 28 Jun 17:19:05.064
+slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
17400:X 28 Jun 17:19:05.064 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
17400:X 28 Jun 17:19:35.080 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380

同時(shí)登陸到6380從庫(kù),查看是否現(xiàn)在為master主節(jié)點(diǎn)
127.0.0.1:6380> info replication

role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=22858,lag=0
master_repl_offset:22858
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:22857
127.0.0.1:6380>

127.0.0.1:6381> info replication

role:slave
master_host:127.0.0.1
master_port:6380
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:35773
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

有時(shí)候主庫(kù)down了,從庫(kù)切換為master不是順序晉升,如master掛了后,6381為主庫(kù)了。其實(shí)是有個(gè)參數(shù)控制,在redis配置文件中,不在哨兵配置文件。
slave-priority 100 該數(shù)字越小。優(yōu)先級(jí)越高。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI