Redis 哨兵模式原理

Redis 哨兵模式原理

redis 哨兵模式原理

哨兵模式是一種特殊的模式,首先Redis提供了哨兵的命令,哨兵是一個獨立的進程,作為進程,它會獨立運行、其原理是哨兵通過發送命令,等待Redis服務器響應,從而監控運行的多個Redis實例。

Linux – redis哨兵集群實例

命令整理

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

redis-cli?info?#查看redis數據庫信息 redis-cli?info?replication?#查看redis的復制授權信息 redis-cli?info?sentinel???#查看redis的哨兵信息

配置流程

思路:

redis主從

一主兩從的方案

1.環境準備,準備一主兩從的redis架構

redis-6379.conf

port?6379 daemonize?yes logfile?"6379.log" dbfilename?"dump-6379.rdb" dir?"/opt/redis/6379/" redis-6380.conf port?6380 daemonize?yes logfile?"6380.log" dbfilename?"dump-6380.rdb" dir?"/opt/redis/6380/" slaveof??127.0.0.1??6379 redis-6381.conf port?6381 daemonize?yes logfile?"6381.log" dbfilename?"dump-6381.rdb" dir?"/opt/redis/6381/" slaveof??127.0.0.1??6379

2.準備三個數據文件夾

mkdir?-p??/opt/redis/{6379,6380,6381}

3。分別啟動三個數據庫

[root@master?sbredis]#?redis-server?redis-6379.conf? [root@master?sbredis]#?redis-server?redis-6380.conf? [root@master?sbredis]#?redis-server?redis-6381.conf

4.檢測主從狀態

redis-cli?-p?6379???info?replication redis-cli?-p?6380???info?replication redis-cli?-p?6381???info?replication

5.準備三個redis哨兵,進行檢測主從狀態

準備三個哨兵的配置文件

redis-26379.conf

//?Sentinel節點的端口 port?26379?? dir?/var/redis/data/ logfile?"26379.log" //?當前Sentinel節點監控?192.168.119.10:6379?這個主節點 //?2代表判斷主節點失敗至少需要2個Sentinel節點節點同意 //?mymaster是主節點的別名 sentinel?monitor?mymaster?192.168.119.10?6379?2 //每個Sentinel節點都要定期PING命令來判斷Redis數據節點和其余Sentinel節點是否可達,如果超過30000毫秒30s且沒有回復,則判定不可達 sentinel?down-after-milliseconds?mymaster?30000 //當Sentinel節點集合對主節點故障判定達成一致時,Sentinel領導者節點會做故障轉移操作,選出新的主節點, 原來的從節點會向新的主節點發起復制操作,限制每次向新的主節點發起復制操作的從節點個數為1 sentinel?parallel-syncs?mymaster?1 //故障轉移超時時間為180000毫秒 sentinel?failover-timeout?mymaster?180000 redis-26380.conf port?7000 daemonize?yes dir?"/opt/data" logfile?"7000.log" dbfilename?"dump-7000.rdb" cluster-enabled?yes??? cluster-config-file?nodes-7000.conf cluster-require-full-coverage?no redis-26381.conf

三個配置文件,僅僅是端口的不同,通過命令快速生成配置文件

[root@master?sbredis]#?sed?"s/26379/26380/g"??redis-26379.conf???>??redis-26380.conf?? [root@master?sbredis]#?sed?"s/26379/26381/g"??redis-26379.conf???>??redis-26381.conf

6.分別啟動三個哨兵

[root@master?sbredis]#?redis-sentinel?redis-26379.conf? [root@master?sbredis]#?redis-sentinel?redis-26380.conf? [root@master?sbredis]#?redis-sentinel?redis-26381.conf

7.檢測哨兵,主從狀態

redis-cli?-p?26379??info?sentinel

看到如下信息,就和我一樣了

[root@master?sbredis]#?redis-cli?-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=s17ms,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3

8.測驗,干掉master redis,是否自動切換

ps?-ef|grep?redis ????kill?進程 ????..

9.再次啟動redis 6379 查看它是否加入 主從集群

redis-server?redis-6379.conf

推薦教程:《Redis教程


? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享