Redis以主從的模式搭建集羣后,若是主節點Master掛掉,雖然能夠實現將備用節點Slave切換成主節點,可是Redis自己並無自動監控機制,須要藉助Sentinel哨兵模式,實現監控並實現自動切換。爲了實現Sentinel的高可用,須要sentinel也以集羣模式來搭建,這裏經過一臺機器的不一樣端口來模擬。相關環境信息以下:redis
一、Redis集羣信息:centos
角色 | IP地址 | 監聽端口 |
Master | 127.0.0.1 | 6379 |
Slave | 127.0.0.1 | 6380 |
Slave | 127.0.0.1 | 6381 |
Sentinel集羣信息:spa
哨兵角色 | IP地址 | 監聽端口 |
Node-1 | 127.0.0.1 | 26379 |
Node-2 | 127.0.0.1 | 26380 |
Node-3 | 127.0.0.1 | 26381 |
二、Redis集羣搭建過程參考上篇,這裏再也不描述。首先啓動Redis的主節點和兩個Slave節點。日誌
進入Master節點,查看信息:code
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。server
分別進入兩個slave節點查看信息:blog
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集羣。進程
三、Sentinel集羣搭建ip
step一、將redis-sentinel拷貝到redis對應的執行目錄bin(該命令是redis-server的一個鏈接,用redis-server也OK,後面講到,啓動sentinel會有兩種方式),而後拷貝sentinel.config到redis配置文件目錄config。it
step二、分別編輯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 #若哨兵在配置值內未能完成故障轉移操做,則任務本次故障轉移失敗。
step三、啓動哨兵監控程序:
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
step四、經過哨兵鏈接,並檢查信息:
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。
四、模擬場景:Redis Master節點掛掉,查看Redis集羣狀態。
step一、關掉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]#
step二、經過哨兵查看集羣狀態:
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節點。
step三、啓動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