[Redis]-加入Sentinel進行auto failover

Redis建置學習心得

建立Sentinel, 比較適合的是獨立一個Redis instance來執行, 其一是減少個別instanceresource占用 -- single thread, 二來也避免單點失敗的問題 -- 一次影響二個元件. 所以就先依照 "建立多個Redis Instance" 的內容, 再拉一個Instance並指定為6390 port. Phase 1來看, 放到192.168.127.103.

 

完成獨立的Redis Instance, 在該instance上建立Sentinel.

1.      先將 <REDIS PATH>/sentinel.conf複製一份到/etc/redis/sentinel.conf (變動檔名是為了方便管理)

# cp sentinel.conf /etc/redis/sentinel_26379.conf

 

2.      修改/etc/redis/sentinel_26379.conf檔案內容

 

port預設26379, 如果要調整sentinelport就改這行

port 26379

 

接下來四行則依照需要進行修改

sentinel monitor mymaster 127.0.0.1 6379 2

sentinel down-after-milliseconds mymaster 30000

sentinel failover-timeout mymaster 180000

sentinel parallel-syncs mymaster 1

 

第一行是要monitorIP, PORT, 和有多少個sentinel決定權

第二行是發現master失敗多久後進行failover

第三行是failover過程中多久未完成則算failover失敗

第四行是有多少slave可以向新的master請求

 

因此在本次的測試環境中, 做了下列的修改 (使用實體IP, 並且只有5秒失敗就failover)

 

sentinel monitor apgp1 191.168.127.101 6379 1

sentinel down-after-milliseconds apgp1 5000

sentinel failover-timeout apgp1 180000

sentinel parallel-syncs apgp1 1

 

3.      sentinel監控的機器群, 要將firewall開啟, 開啟相關port的語法如前述.

# firewall-cmd --permanent --add-port=xxxxx/tcp

# firewall-cmd --reload

 

4.      啟動sentinel的語法, 可以透過redis-sentinel或是redis-server, 然後指向上面進行修改的sentinel_xxxx.conf

# redis-sentinel /etc/redis/sentinel_26379.conf

 

完成後先檢查一下目前sentinel監控的狀況

 

192.168.127.103:26379> sentinel masters

1)  1) "name"

    2) "apgp1"

    3) "ip"

    4) "192.168.127.101"

    5) "port"

    6) "6379"

    7) "runid"

……

 

192.168.127.103:26379> sentinel master apgp1

 1) "name"

 2) "apgp1"

 3) "ip"

 4) "192.168.127.101"

 5) "port"

 6) "6379"

 7) "runid"

……

 

192.168.127.103:26379> sentinel slaves apgp1

1)  1) "name"

    2) "192.168.127.102:6379"

    3) "ip"

    4) "192.168.127.102"

    5) "port"

    6) "6379"

    7) "runid"

……

 

 

手動測試failover的狀況

 

192.168.127.103:26379> sentinel failover apgp1

OK

 

15011:X 23 Jun 10:56:54.351 # +switch-master apgp1 192.168.127.101 6379 192.168.127.103 6379

15011:X 23 Jun 10:56:54.352 * +slave slave 192.168.127.102:6379 192.168.127.102 6379 @ apgp1 192.168.127.103 6379

15011:X 23 Jun 10:56:54.352 * +slave slave 192.168.127.101:6379 192.168.127.101 6379 @ apgp1 192.168.127.103 6379

15011:X 23 Jun 10:57:04.479 * +convert-to-slave slave 192.168.127.101:6379 192.168.127.101 6379 @ apgp1 192.168.127.103 6379

 

# redis-cli -h 192.168.127.103 -p 6379 info replication

# Replication

role:master

connected_slaves:2

slave0:ip=192.168.127.102,port=6379,state=online,offset=13947,lag=0

slave1:ip=192.168.127.101,port=6379,state=online,offset=13947,lag=0

master_repl_offset:13947

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:10126

repl_backlog_histlen:3822

 

 

實際下線Redis進行Failover

# redis-cli -h 192.168.127.101 -p 6379 shutdown

 

15011:X 23 Jun 10:59:57.266 # +failover-end master apgp1 192.168.127.101 6379

15011:X 23 Jun 10:59:57.266 # +switch-master apgp1 192.168.127.101 6379 192.168.127.103 6379

15011:X 23 Jun 10:59:57.266 * +slave slave 192.168.127.102:6379 192.168.127.102 6379 @ apgp1 192.168.127.103 6379

15011:X 23 Jun 10:59:57.266 * +slave slave 192.168.127.101:6379 192.168.127.101 6379 @ apgp1 192.168.127.103 6379

15011:X 23 Jun 11:00:02.267 # +sdown slave 192.168.127.101:6379 192.168.127.101 6379 @ apgp1 192.168.127.103 6379