Redis筆記-Sentinel哨兵模式


Redis以主從的模式搭建集群后,如果主節點Master掛掉,雖然可以實現將備用節點Slave切換成主節點,但是Redis本身並沒有自動監控機制,需要借助Sentinel哨兵模式,實現監控並實現自動切換。為了實現Sentinel的高可用,需要sentinel也以集群模式來搭建,這里通過一台機器的不同端口來模擬。相關環境信息如下:

1、Redis集群信息:

角色 IP地址 監聽端口
Master 127.0.0.1 6379
Slave 127.0.0.1 6380
Slave 127.0.0.1 6381

 

 

 

 

 

 

Sentinel集群信息:

哨兵角色 IP地址 監聽端口
Node-1 127.0.0.1 26379
Node-2 127.0.0.1 26380
Node-3 127.0.0.1 26381

 

 

 

 

 

 

2、Redis集群搭建過程參考上篇,這里不再描述。首先啟動Redis的主節點和兩個Slave節點。

進入Master節點,查看信息:

 1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 6379 -a  funnyboy
 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 3 127.0.0.1:6379> info replication
 4 # Replication
 5 role:master
 6 connected_slaves:2
 7 slave0:ip=127.0.0.1,port=6380,state=online,offset=4256,lag=0
 8 slave1:ip=127.0.0.1,port=6381,state=online,offset=4256,lag=1
 9 master_replid:d5802af0905736ae28201050ce4871ee2921c16c
10 master_replid2:0000000000000000000000000000000000000000
11 master_repl_offset:4256
12 second_repl_offset:-1
13 repl_backlog_active:1
14 repl_backlog_size:1048576
15 repl_backlog_first_byte_offset:1
16 repl_backlog_histlen:4256
17 127.0.0.1:6379> 

顯示當前節點role為master,並且連接的slave個數為2,OK。

分別進入兩個slave節點查看信息:

 1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 6380 -a  funnyboy
 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 3 127.0.0.1:6380> info replication
 4 # Replication
 5 role:slave
 6 master_host:127.0.0.1
 7 master_port:6379
 8 master_link_status:up
 9 master_last_io_seconds_ago:8
10 master_sync_in_progress:0
11 slave_repl_offset:4424
12 slave_priority:100
13 slave_read_only:1
14 connected_slaves:0
15 master_replid:d5802af0905736ae28201050ce4871ee2921c16c
16 master_replid2:0000000000000000000000000000000000000000
17 master_repl_offset:4424
18 second_repl_offset:-1
19 repl_backlog_active:1
20 repl_backlog_size:1048576
21 repl_backlog_first_byte_offset:1
22 repl_backlog_histlen:4424
23 127.0.0.1:6380> 
 1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 6381 -a  funnyboy
 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 3 127.0.0.1:6381> info replication
 4 # Replication
 5 role:slave
 6 master_host:127.0.0.1
 7 master_port:6379
 8 master_link_status:up
 9 master_last_io_seconds_ago:5
10 master_sync_in_progress:0
11 slave_repl_offset:4550
12 slave_priority:100
13 slave_read_only:1
14 connected_slaves:0
15 master_replid:d5802af0905736ae28201050ce4871ee2921c16c
16 master_replid2:0000000000000000000000000000000000000000
17 master_repl_offset:4550
18 second_repl_offset:-1
19 repl_backlog_active:1
20 repl_backlog_size:1048576
21 repl_backlog_first_byte_offset:1
22 repl_backlog_histlen:4550
23 127.0.0.1:6381> 

角色都為slave,並且master信息正常。確保Redis集群OK后,開始准備搭建sentinel集群。

3、Sentinel集群搭建

step1、將redis-sentinel拷貝到redis對應的執行目錄bin(該命令是redis-server的一個連接,用redis-server也OK,后面講到,啟動sentinel會有兩種方式),然后拷貝sentinel.config到redis配置文件目錄config。

 

 step2、分別編輯sentinel的配置文件(沒特別強調的保持默認即可)

sentinel-26379.conf配置如下:

1 daemonize yes                                            #開啟后台守護進程
2 port 26379                                               #端口配置
3 pidfile "/usr/local/redis/pid/redis-sentinel-26379.pid"  #PID文件
4 logfile "/usr/local/redis/logs/sentinel-26379.log"       #日志文件
5 sentinel monitor mymaster 127.0.0.1 6379 2               #哨兵監控配置。注意,如果配置了認證,改配置必須在auth-pass配置之前,否則啟動報找不到master的錯誤
6 sentinel auth-pass mymaster funnyboy                     #認證配置
7 sentinel down-after-milliseconds mymaster 5000          #master或者slave多少時間(默認30秒)不能使用標記為down狀態。
8 sentinel failover-timeout mymaster 9000                 #若哨兵在配置值內未能完成故障轉移操作,則任務本次故障轉移失敗。

sentinel-26380.conf配置如下:

1 daemonize yes                                            #開啟后台守護進程
2 port 26380                                               #端口配置
3 pidfile "/usr/local/redis/pid/redis-sentinel-26380.pid"  #PID文件
4 logfile "/usr/local/redis/logs/sentinel-26380.log"       #日志文件
5 sentinel monitor mymaster 127.0.0.1 6379 2               #哨兵監控配置。注意,如果配置了認證,改配置必須在auth-pass配置之前,否則啟動報找不到master的錯誤
6 sentinel auth-pass mymaster funnyboy                     #認證配置
7 sentinel down-after-milliseconds mymaster 5000         #master或者slave多少時間(默認30秒)不能使用標記為down狀態。
8 sentinel failover-timeout mymaster 9000                #若哨兵在配置值內未能完成故障轉移操作,則任務本次故障轉移失敗。

sentinel-26381.conf配置如下:

1 daemonize yes                                            #開啟后台守護進程
2 port 26381                                               #端口配置
3 pidfile "/usr/local/redis/pid/redis-sentinel-26381.pid"  #PID文件
4 logfile "/usr/local/redis/logs/sentinel-26381.log"       #日志文件
5 sentinel monitor mymaster 127.0.0.1 6379 2               #哨兵監控配置。注意,如果配置了認證,改配置必須在auth-pass配置之前,否則啟動報找不到master的錯誤
6 sentinel auth-pass mymaster funnyboy                     #認證配置
7 sentinel down-after-milliseconds mymaster 5000         #master或者slave多少時間(默認30秒)不能使用標記為down狀態。
8 sentinel failover-timeout mymaster 9000                #若哨兵在配置值內未能完成故障轉移操作,則任務本次故障轉移失敗。

step3、啟動哨兵監控程序:

1 2 [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-26379.conf 
3 [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-26380.conf 
4 [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-26381.conf 

啟動有兩種方式:

一是執行:redis-sentinel  sentinel.conf 

二是執行:redis-server sentinel --sentinel

step4、通過哨兵連接,並檢查信息:

 1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 26379 -a funnyboy
 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 3 127.0.0.1:26379> info sentinel
 4 # Sentinel
 5 sentinel_masters:1
 6 sentinel_tilt:0
 7 sentinel_running_scripts:0
 8 sentinel_scripts_queue_length:0
 9 sentinel_simulate_failure_flags:0
10 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
11 127.0.0.1:26379> 

可以看到監控的redis服務,一個Master、兩個Slave、sentinels = 3 說明配置OK。

4、模擬場景:Redis Master節點掛掉,查看Redis集群狀態。

step1、關掉Master節點:

1 [root@VM_0_14_centos bin]# ./redis-cli -p 6379 -a funnyboy
2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
3 127.0.0.1:6379> shutdown
4 not connected> 
5 [root@VM_0_14_centos bin]# 

step2、通過哨兵查看集群狀態:

1 127.0.0.1:26379> info sentinel
2 # Sentinel
3 sentinel_masters:1
4 sentinel_tilt:0
5 sentinel_running_scripts:0
6 sentinel_scripts_queue_length:0
7 sentinel_simulate_failure_flags:0
8 master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3
9 127.0.0.1:26379> 

通過sentinel信息可以看到,Master節點已經自動切換到6380端口了,說明主節點掛掉后,6380 Slave節點自動升級成為了Master節點。

 step3、啟動6379 redis服務,然后查看節點角色,此時6379變成了Slave,6380為Master節點,OK。

 1 [root@VM_0_14_centos redis]# ./bin/redis-server ./config/redis-6379.conf
 2 [root@VM_0_14_centos redis]# ./bin/redis-cli -p 6379 -a funnyboy
 3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
 4 127.0.0.1:6379> 
 5 127.0.0.1:6379> info replication
 6 # Replication
 7 role:slave
 8 master_host:127.0.0.1
 9 master_port:6380
10 master_link_status:up
11 master_last_io_seconds_ago:1
12 master_sync_in_progress:0
13 slave_repl_offset:392671
14 slave_priority:100
15 slave_read_only:1
16 connected_slaves:0
17 master_replid:c77763408266bcebf233bdc9e59e3bcf14dc7a08
18 master_replid2:0000000000000000000000000000000000000000
19 master_repl_offset:392671
20 second_repl_offset:-1
21 repl_backlog_active:1
22 repl_backlog_size:1048576
23 repl_backlog_first_byte_offset:378586
24 repl_backlog_histlen:14086

 


免責聲明!

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



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