redis之哨兵集群


一、主從復制背景問題

Redis主從復制可將主節點數據同步給從節點,從節點此時有兩個作用:

  • 一旦主節點宕機,從節點作為主節點的備份可以隨時頂上來。
  • 擴展主節點的讀能力,分擔主節點讀壓力。

但是問題是:

  • 一旦主節點宕機,從節點上位,那么需要人為修改所有應用方的主節點地址(改為新的master地址),還需要命令所有從節點復制新的主節點

那么這個問題,redis-sentinel就可以解決了

二、Redis-Sentinel

Redis-Sentinel是redis官方推薦的高可用性解決方案,
當用redis作master-slave的高可用時,如果master本身宕機,redis本身或者客戶端都沒有實現主從切換的功能。

而redis-sentinel就是一個獨立運行的進程,用於監控多個master-slave集群,
自動發現master宕機,進行自動切換slave > master。

三、Sentinel工作方式

每個Sentinel以每秒鍾一次的頻率向它所知的Master,Slave以及其他 Sentinel 實例發送一個 PING 命令
 

如果一個實例(instance)距離最后一次有效回復 PING 命令的時間超過 down-after-milliseconds 選項所指定的值, 則這個實例會被 Sentinel 標記為主觀下線。

如果一個Master被標記為主觀下線,則正在監視這個Master的所有 Sentinel 要以每秒一次的頻率確認Master的確進入了主觀下線狀態。

當有足夠數量的 Sentinel(大於等於配置文件指定的值)在指定的時間范圍內確認Master的確進入了主觀下線狀態, 則Master會被標記為客觀下線

在一般情況下, 每個 Sentinel 會以每 10 秒一次的頻率向它已知的所有Master,Slave發送 INFO 命令

當Master被 Sentinel 標記為客觀下線時,Sentinel 向下線的 Master 的所有 Slave 發送 INFO 命令的頻率會從 10 秒一次改為每秒一次

若沒有足夠數量的 Sentinel 同意 Master 已經下線, Master 的客觀下線狀態就會被移除。

若 Master 重新向 Sentinel 的 PING 命令返回有效回復, Master 的主觀下線狀態就會被移除。

主觀下線和客觀下線

主觀下線:Subjectively Down,簡稱 SDOWN,指的是當前 Sentinel 實例對某個redis服務器做出的下線判斷。
客觀下線:Objectively Down, 簡稱 ODOWN,指的是多個 Sentinel 實例在對Master Server做出 SDOWN 判斷,並且通過 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下線判斷,然后開啟failover.

SDOWN適合於Master和Slave,只要一個 Sentinel 發現Master進入了ODOWN, 這個 Sentinel 就可能會被其他 Sentinel 推選出, 並對下線的主服務器執行自動故障遷移操作。

ODOWN只適用於Master,對於Slave的 Redis 實例,Sentinel 在將它們判斷為下線前不需要進行協商, 所以Slave的 Sentinel 永遠不會達到ODOWN。
View Code

四、主從復制架構

五、Redis Sentinel架構

Sentinel是redis的一個進程,但是不存儲數據,只是監控redis

六、redis命令

官網地址:http://redisdoc.com/

redis-cli info #查看redis數據庫信息

redis-cli info replication #查看redis的復制授權信息

redis-cli info sentinel   #查看redis的哨兵信息

七、環境配置

redis的哨兵,自動的主從故障切換

# 准備3個redis數據庫實例
主庫:端口6379
從庫:端口6380
從庫:端口6381

# 准備3個redis-sentinel哨兵
redis-server redis-6379.conf 
redis-server redis-6380.conf 
redis-server redis-6381.conf 

# 三個哨兵同時監測主庫6379的運行狀況,宕機后三個哨兵根據算法選擇從庫中的一個切換成主庫

redis數據庫實例

生成數據文件夾

mkdir -p /var/redis/data/{6379,6380,6381}

主庫6379配置文件redis-6379.conf 

port 6379
daemonize yes
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/var/redis/data/6379"

從庫6380配置文件redis-6380.conf 

port 6380
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/var/redis/data/6380" 
slaveof 127.0.0.1 6379   

從庫6381配置文件redis-6381.conf 

port 6381
daemonize yes
logfile "6380.log"
dbfilename "dump-6380.rdb"
dir "/var/redis/data/6381" 
slaveof 127.0.0.1 6379   

分別啟動三個redis數據庫實例 

redis-server redis-6379.conf 
redis-server redis-6380.conf 
redis-server redis-6381.conf 

准備三個redis-sentinel哨兵的配置文件

創建配置文件

touch redis-sentinel-26379.conf
touch redis-sentinel-26380.conf
touch redis-sentinel-26381.conf

參數詳解

port 26379  
dir /var/redis/data/26379
logfile "26379.log"

// 當前Sentinel節點監控 127.0.0.1:6379 這個主節點
// 2代表判斷主節點失敗至少需要2個Sentinel節點節點同意
// mymaster是主節點的別名
sentinel monitor s20master 127.0.0.1   6379  2

//每個Sentinel節點都要定期PING命令來判斷Redis數據節點和其余Sentinel節點是否可達,如果超過30000毫秒30s且沒有回復,則判定不可達
sentinel down-after-milliseconds s20master 30000

//當Sentinel節點集合對主節點故障判定達成一致時,Sentinel領導者節點會做故障轉移操作,選出新的主節點,
原來的從節點會向新的主節點發起復制操作,限制每次向新的主節點發起復制操作的從節點個數為1
sentinel parallel-syncs s20master 1

//故障轉移超時時間為180000毫秒
sentinel failover-timeout s20master 180000
//讓哨兵在后台運行
daemonize yes
View Code

注意

如果主庫中設置了密碼,我們需要在哨兵配置文件中加上下面的參數:

protected-mode no

sentinel auth-pass

redis-sentinel-26379.conf

port 26379  
dir /var/redis/data/26379
logfile "26379.log"
sentinel monitor s20master 127.0.0.1   6379  2
sentinel down-after-milliseconds s20master 30000
sentinel parallel-syncs s20master 1
sentinel failover-timeout s20master 180000
daemonize yes

redis-sentinel-26380.conf

port 26380  
dir /var/redis/data/26380
logfile "26380.log"
sentinel monitor s20master 127.0.0.1   6379  2
sentinel down-after-milliseconds s20master 30000
sentinel parallel-syncs s20master 1
sentinel failover-timeout s20master 180000
daemonize yes

redis-sentinel-26380.conf

port 26381
dir /var/redis/data/26381
logfile "26381.log"
sentinel monitor s20master 127.0.0.1   6379  2
sentinel down-after-milliseconds s20master 30000
sentinel parallel-syncs s20master 1
sentinel failover-timeout s20master 180000
daemonize yes

分別運行三個哨兵進程

redis-sentinel redis-26379.conf 
redis-sentinel redis-26380.conf 
redis-sentinel redis-26381.conf 

# 保證sentinel的配置正確,否則,你在啟動報錯后,配置文件的內容會發生變化,這是個坑!!!!

檢查redis的哨兵狀態

redis-cli -p 26379 info sentinel
redis-cli -p 26380 info sentinel
redis-cli -p 26381 info sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
# 看到最后一條信息正確即成功了哨兵,哨兵主節點名字叫做s20master,狀態ok,監控地址是127.0.0.0:6379,有兩個從節點,3個哨兵
master0:name=s20master,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3

八、redis高可用故障實驗

大致思路

  • 殺掉主節點的redis進程6379端口,觀察從節點是否會進行新的master選舉,進行切換
  • 重新恢復舊的“master”節點,查看此時的redis身份

首先查看三個redis的進程狀態

檢查三個節點的復制身份狀態

redis-cli -p 端口 info replication

【6379】

[root@szx / 17:18:24]#redis-cli -p 6379 info replication
# Replication
role:master
connected_slaves:2  # 兩個從庫
slave0:ip=127.0.0.1,port=6380,state=online,offset=837877,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=838011,lag=0
master_replid:a4ecb61110814dc5b117db545c0c96c904990fc4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:838011
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:838011

【6380】

[root@szx / 17:19:14]#redis-cli -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1   # 主庫ip
master_port:6379     # 主庫端口
master_link_status:up  # 狀態正常
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:852447
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a4ecb61110814dc5b117db545c0c96c904990fc4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:852447
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:852447

【6381】

[root@szx / 17:20:27]#redis-cli -p 6381 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:874725
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a4ecb61110814dc5b117db545c0c96c904990fc4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:874725
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:874711

此時,干掉master!!!然后等待其他兩個節點是否能自動被哨兵sentienl,切換為master節點

查看剩余的6380和6381的節點身份

注意:重新啟動6379redis服務

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM