Redis Sentinel集群部署
目錄
?
實驗環境
機器規劃
原理說明
主從復制存在的問題
Redis Sentinel方案
安裝部署
1.配置并啟動Master節點
2.配置并啟動兩個Slave節點
3.驗證主從關系
部署Sentinel節點
1.配置Sentinel1節點
2.啟動Sentinel1節點
3.確認Sentinel1節點Sentinel狀態
配置并啟動Sentinel2和Sentinel3節點
驗證Sentinel狀態
Sentinel 配置說明
port,daemonize,logfile和dir
sentinel monitor mymaster
sentinel down-after-milliseconds
sentinel parallel-syncs
sentinel failover-timeout
sentinel auth-pass
sentinel notification-script
sentinel client-reconfig-script
Sentinel的特點
實驗環境
操作系統: Ubuntu 18.04.1 LTS
redis版本: redis 5.0.2
機器規劃
| Master | 127.0.0.1 | 6379 |
| Slave1 | 127.0.0.1 | 6380 |
| Slave2 | 127.0.0.1 | 6381 |
| Sentinel1 | 127.0.0.1 | 26379 |
| Sentinel2 | 127.0.0.1 | 26380 |
| Sentinel3 | 127.0.0.1 | 26381 |
注意:
生產環境中建議Redis Sentinel的所有節點應該分布在不同的物理機上。
原理說明
主從復制存在的問題
Redis的主從復制模式可以將主節點的數據改變同步給從節點,這樣從節點就可以起到兩個作用:
- 第一,作為主節點的一個備份,一旦主節點出了故障不可達的情況,從節點可以作為后備“頂”上來,并且保證數據盡量不丟失(主從復制是最終一致性)。
- 第二,從節點可以擴展主節點的讀能力,一旦主節點不能支撐住大并發量的讀操作,從節點可以在一定程度上幫助主節點分擔讀壓力。
但是主從復制也帶來了以下問題:·一旦主節點出現故障,需要手動將一個從節點晉升為主節點,同時需要修改應用方的主節點地址,還需要命令其他從節點去復制新的主節點,整個過程都需要人工干預。
Redis Sentinel方案
Redis Sentinel是一個分布式架構,其中包含若干個Sentinel節點和Redis數據節點,每個Sentinel節點會對數據節點和其余Sentinel節點進行監控,當它發現節點不可達時,會對節點做下線標識。
如果被標識的是主節點,它還會和其他Sentinel節點進行“協商”,當大多數Sentinel節點都認為主節點不可達時,它們會選舉出一個Sentinel節點來完成自動故障轉移的工作,同時會將這個變化實時通知給Redis應用方。整個過程完全是自動的,不需要人工來介入,所以這套方案很有效地解決了Redis的高可用問題。
安裝部署
1.配置并啟動Master節點
創建data目錄:
root@sbc-VirtualBox:~# mkdir -p /opt/soft/redis/data創建配置文件:
root@sbc-VirtualBox:~# vim /etc/redis/redis-6379.confport 6379 daemonize yes logfile "6379.log" dbfilename "dump-6379.rdb" dir "/opt/soft/redis/data/"啟動Master節點:
root@sbc-VirtualBox:~# redis-server /etc/redis/redis-6379.conf驗證Master節點狀態:
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6379 ping PONG2.配置并啟動兩個Slave節點
創建配置文件:
slave1:
root@sbc-VirtualBox:~# vim /etc/redis/redis-6380.confport 6380 daemonize yes logfile "6380.log" dbfilename "dump-6380.rdb" dir "/opt/soft/redis/data/" slaveof 127.0.0.1 6379slave標志:slaveof 127.0.0.1 6379,說明該slave是127.0.0.1 6379這個master的slave
slave2:
root@sbc-VirtualBox:~# vim /etc/redis/redis-6381.confport 6381 daemonize yes logfile "6381.log" dbfilename "dump-6381.rdb" dir "/opt/soft/redis/data/" slaveof 127.0.0.1 6379啟動2個Slave節點:
root@sbc-VirtualBox:~# redis-server /etc/redis/redis-6380.conf root@sbc-VirtualBox:~# redis-server /etc/redis/redis-6381.conf驗證:
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6380 ping PONG root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6381 ping PONG3.驗證主從關系
查看Master節點replication信息:
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6379 info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=126,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=126,lag=1 master_replid:1ca0bf14ca2cd79941df7285bf413aa6b8ae921d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:126 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:126可觀察到connected_slaves有2個,分別為:
slave0:ip=127.0.0.1,port=6380,state=online,offset=126,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=126,lag=1同樣在Slave節點也可查看到replication信息:
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 6380 info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:10 master_sync_in_progress:0 slave_repl_offset:252 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:1ca0bf14ca2cd79941df7285bf413aa6b8ae921d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:252 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:252可觀察到Master信息,連接狀態正常:
master_host:127.0.0.1 master_port:6379 master_link_status:up部署Sentinel節點
1.配置Sentinel1節點
創建conf文件:
root@sbc-VirtualBox:~# vim /etc/redis/redis-sentinel-26379.confport 26379 daemonize yes logfile "26379.log" dir /opt/soft/redis/data sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000- Sentinel節點本身就是獨立的Redis節點,只不過它們有一些特殊,它們不存儲數據,只支持部分命令。
- sentinel monitor mymaster 127.0.0.1 6379 2:此行配置代表sentinel-1節點需要監控127.0.0.1:6379這個主節點,2代表判斷主節點失敗至少需要2個Sentinel節點同意,mymaster是主節點的別名
2.啟動Sentinel1節點
root@sbc-VirtualBox:~# redis-sentinel /etc/redis/redis-sentinel-26379.conf3.確認Sentinel1節點Sentinel狀態
在Sentinel1節點查看Sentinel狀態:
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 26379 info Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=1配置并啟動Sentinel2和Sentinel3節點
生成配置文件:
Sentinel2:
Sentinel3:
root@sbc-VirtualBox:~# vim /etc/redis/redis-sentinel-26381.confport 26381 daemonize yes logfile "26381.log" dir /opt/soft/redis/data sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000啟動Sentinel2和Sentinel3:
root@sbc-VirtualBox:~# redis-sentinel /etc/redis/redis-sentinel-26380.conf root@sbc-VirtualBox:~# redis-sentinel /etc/redis/redis-sentinel-26381.conf驗證Sentinel狀態
連接任意Sentinel節點:
root@sbc-VirtualBox:~# redis-cli -h 127.0.0.1 -p 26379 info Sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3可觀察到master地址,端口正確。slave節點,sentinel節點數量正確。
Sentinel 配置說明
當所有節點啟動后,配置文件中的內容發生了變化,體現在三個方面:
- Sentinel節點自動發現了從節點、其余Sentinel節點。
- 去掉了默認配置,例如parallel-syncs、failover-timeout參數。
- 添加了配置紀元相關參數。
下面以Sentinel1節點為例,Sentinel1節點的conf文件變為了:
port 26379 daemonize yes logfile "26379.log" dir "/opt/soft/redis/data" sentinel myid b6983fbf6b4527811b724ce5ef0ea08388056e34 sentinel deny-scripts-reconfig yes sentinel monitor mymaster 127.0.0.1 6379 2 sentinel config-epoch mymaster 0 # Generated by CONFIG REWRITE protected-mode no sentinel leader-epoch mymaster 0 sentinel known-replica mymaster 127.0.0.1 6381 sentinel known-replica mymaster 127.0.0.1 6380 sentinel known-sentinel mymaster 127.0.0.1 26381 4d77ba4fbfd1696db44e6302340988f771bbb62a sentinel known-sentinel mymaster 127.0.0.1 26380 75cd9743a892825573793c4417e7e8465d65b616 sentinel current-epoch 0port,daemonize,logfile和dir
port:表示Sentinel節點的端口;
daemonize:表示守護進程,開啟則Sentinel將在后臺運行;
logfile:表示Sentinel節點的日志路徑;
dir:表示Sentinel節點的工作目錄;
sentinel monitor mymaster
sentinel monitor <master-name> <ip> <port> <quorum>Sentinel節點會定期監控主節點,所以從配置上必然也會有所體現,本配置說明Sentinel節點要監控的是一個名字叫做,地址ip和端口為port的主節點,quorum代表要判定主節點最終不可達所需要的票數。
一般建議將quorum設置為Sentinel節點的一半加1。同時還與Sentinel節點的領導者選舉有關,至少要有max(quorum,num(sentinels)/2+1)個Sentinel節點參與選舉,才能選出領導者Sentinel,從而完成故障轉移。例如有5個Sentinel節點,quorum=4,那么至少要有max(quorum,num(sentinels)/2+1)=4個在線Sentinel節點才可以進行領導者選舉
如:
sentinel down-after-milliseconds
sentinel down-after-milliseconds <master-name> <times>每個Sentinel節點都要通過定期發送ping命令來判斷Redis數據節點和其余Sentinel節點是否可達,如果超過了down-after-milliseconds配置的時間且沒有有效的回復,則判定節點不可達,(單位為毫秒)就是超時時間。這個配置是對節點失敗判定的重要依據。
sentinel parallel-syncs
sentinel parallel-syncs <master-name> <nums>當Sentinel節點集合對主節點故障判定達成一致時,Sentinel領導者節點會做故障轉移操作,選出新的主節點,原來的從節點會向新的主節點發起復制操作,parallel-syncs就是用來限制在一次故障轉移之后,每次向新的主節點發起復制操作的從節點個數。如果這個參數配置的比較大,那么多個從節點會向新的主節點同時發起復制操作,盡管復制操作通常不會阻塞主節點,但是同時向主節點發起復制,必然會對主節點所在的機器造成一定的網絡和磁盤IO開銷。
sentinel failover-timeout
sentinel failover-timeout <master-name> <times>failover-timeout通常被解釋成故障轉移超時時間,但實際上它作用于故障轉移的各個階段:
a)選出合適從節點。
b)晉升選出的從節點為主節點。
c)命令其余從節點復制新的主節點。
d)等待原主節點恢復后命令它去復制新的主節點。
sentinel auth-pass
sentinel auth-pass <master-name> <password>如果Sentinel監控的主節點配置了密碼,sentinel auth-pass配置通過添加主節點的密碼,防止Sentinel節點對主節點無法監控。
sentinel notification-script
sentinel notification-script <master-name> <script-path>sentinel notification-script的作用是在故障轉移期間,當一些警告級別的Sentinel事件發生(指重要事件,例如-sdown:客觀下線、-odown:主觀下線)時,會觸發對應路徑的腳本,并向腳本發送相應的事件參數。
sentinel client-reconfig-script
sentinel client-reconfig-script <master-name> <script-path>sentinel client-reconfig-script的作用是在故障轉移結束后,會觸發對應路徑的腳本,并向腳本發送故障轉移結果的相關參數。
Sentinel的特點
Redis Sentinel具有以下幾個功能:
·監控:Sentinel節點會定期檢測Redis數據節點、其余Sentinel節點是否可達。
·通知:Sentinel節點會將故障轉移的結果通知給應用方。
·主節點故障轉移:實現從節點晉升為主節點并維護后續正確的主從關系。
·配置提供者:在Redis Sentinel結構中,客戶端在初始化的時候連接的是Sentinel節點集合,從中獲取主節點信息
Redis Sentinel的優勢:
·對于節點的故障判斷是由多個Sentinel節點共同完成,這樣可以有效地防止誤判。
·Sentinel節點集合是由若干個Sentinel節點組成的,這樣即使個別Sentinel節點不可用,整個Sentinel節點集合依然是健壯的。
總結
以上是生活随笔為你收集整理的Redis Sentinel集群部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis的配置文件介绍
- 下一篇: (二)Docker中以redis.con