Redis單點時,當一臺機器掛機了,redis的服務徹底中止,這時就會影響其餘服務的正常運行。下面利用redis sentinel作一個主從切換的集羣管理。git
下面兩段官方的說辭:github
Redis Sentinel provides high availability for Redis. In practical terms this means that using Sentinel you can create a Redis deployment that resists without human intervention to certain kind of failures.
Redis Sentinel also provides other collateral tasks such as monitoring, notifications and acts as a configuration provider for clients.redis
環境配置:
因爲我此次配置沒有太多的機器,參考前面的主從搭建,測試環境就兩臺Linux機器,因此用了一臺Windows機器作了一個slave。
Windows只支持64位,因此要找64位的機器,參考此博客http://blog.csdn.net/renfufei/article/details/38474435,安裝包今後連接上下載https://github.com/MSOpenTech/redis/releases,有zip免安裝的,也有msi的,根據本身須要下載,我下載的zip,解壓默認配置便可。windows
windows這臺是slave,啓動命令:ide
將當前機器加入主從命令: ./redis-cli -p 6379 slaveof 192.168.0.149 6379測試
集羣配置最少須要三臺機器,那麼我就兩臺Linux機器,一臺Windows機器分別安裝一樣的redis的環境
IP分別:
192.168.0.148 (redis sentinel 集羣監控)
192.168.0.149 (redis 主)
192.168.9.68 (windows redis 從)this
啓動主和從,而後在主查看Replication信息.net
[root@JfbIpadServer02 /usr/local/redis/bin]#./redis-cli -h 192.168.0.149 info Replication
# Replication
role:master #表明當前主機爲master
connected_slaves:1 #有一臺slave
slave0:ip=192.168.0.68,port=6379,state=online,offset=50959,lag=1 #slave的IP和端口
master_repl_offset:50959
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:50958日誌
相看從機器的Replication信息code
D:\Programs\Redis-x64-3.0.501>redis-cli -h 192.168.0.68 info Replication
# Replication
role:slave
master_host:192.168.0.149
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:61166
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:18167
配置redis sentinel集羣監控服務
1.添加一份redis sentinel 配置文件,在192.168.0.148上,配置此文件。
jfb-test:/usr/local/src/redis-3.0.7 # cp sentinel.conf /usr/local/redis/bin/
jfb-test:/usr/local/src/redis-3.0.7 # cd /usr/local/redis/bin
jfb-test:/usr/local/redis/bin # vi sentinel.conf
The Redis source distribution contains a file called sentinel.conf
that is a self-documented example configuration file you can use to configure Sentinel, however a typical minimal configuration file looks like the following:
sentinel monitor mymaster 192.168.0.149 6379 2 sentinel down-after-milliseconds mymaster 60000 sentinel failover-timeout mymaster 180000 sentinel parallel-syncs mymaster 1 sentinel monitor resque 192.168.1.3 6380 4 sentinel down-after-milliseconds resque 10000 sentinel failover-timeout resque 180000 sentinel parallel-syncs resque 5
2,啓動redis sentinel
有配置文件了,那麼啓動redis sentinel作redis集羣監聽。
把redis sentinel 集羣監聽啓動,觀察redis sentinel 日誌信息
jfb-test:/usr/local/redis/bin # ./redis-sentinel sentinel.conf --sentinel
這裏很清楚地看到,從的redis加入了集羣,上面加了紅框的那行。
執行如下命令,查看redis主從信息。
jfb-test:/usr/local/redis/bin # ./redis-cli -h 192.168.0.148 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.0.149:6379,slaves=1,sentinels=1
那麼表示一切都正常了。你的redis sentinel集羣已經配置成功!
3,故障演示
執行如下命令使用主的redis(149)服務中止 stopRedis。查看sentinel機器的日誌,發現master發生了轉移。
這張圖片很清晰地反應到,redis sentinel 監控到主的redis(149)服務中止,而後自動把從的redis(68)切換到主。
再執行如下命令,查看redis主從信息。
jfb-test:/usr/local/redis/bin # ./redis-cli -h 192.168.0.148 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.0.68:6379,slaves=1,sentinels=1
4,恢復啓動原主Redis
當咱們已經發現,一臺redis發生故障了,可能會收到一些故障信息,那麼再把服務已關閉的redis恢復服務狀態,會發生怎麼樣的狀況呢?
+slave slave 192.168.0.149:6379 192.168.0.149 6379 @ mymaster 192.168.0.68 6379
redis sentinel 集羣服務,會把上次主redis從新加入服務中,可是他再以不是主的redis了,變成從的reids。
5,恢復原主,這是手動操做,現實中的自動切換不用操做這步,在如今的主Redis 68上的的執行下面的命令,將149變成主。
D:\Programs\Redis-x64-3.0.501>redis-cli -p 6379 slaveof 192.168.0.149 6379
參考資料:
http://redis.io/topics/sentinel