Redis Sentinel (哨兵模式) 搭建
在Redis5最新版本安裝完成後就帶有哨兵模式管理工具,是Redis獨立組件工具,位於安裝目錄下(與redis-cli同級),執行文件爲:redis-sentinel。
Redis目前可以輕鬆實現哨兵模式的管理,實現的底層原理這裏不作探討。Redis哨兵顧名思義是監控檢查的作用,不提供Redis緩存存取功能,所以我們需要啓動幾個不同的Redis服務,然後通過哨兵去監聽,這裏提示下,程序使用端要自行開發使用哨兵的代碼來獲取Redis鏈接,因爲哨兵監聽Redis,會返回可用的Redis鏈接,給客戶端來創建Redis緩存服務鏈接,是的,哨兵的作用相當於管理Redis服務鏈接的工具,而redis實際鏈接需要客戶端接收到哨兵的redis服務鏈接,再進行redis創建,然後進行緩存存取。
- 搭建哨兵模式
- laravel 配置哨兵模式
前提概要
-
Redis高可以用有三種常用配置方式:
-
Redis自帶主從配置,可以直接實現,多機器爲從,只讀,master可寫
-
主節點Master可讀、可寫.
-
從節點Slave只讀。(read-only)
主從模型可以提高讀的能力,在一定程度上緩解了寫的能力。因爲能寫仍然只有Master節點一個,
可以將讀的操作全部移交到從節點上,變相提高了寫能力 -
-
Redis Sentinel
主從配置是存在缺陷的,因爲從機器或主機器是會出現宕機的,此時某個節點將無法訪問,這時候業務某一部分將會引發無法訪問甚至影響業務訪問,損失巨大。
而哨兵模式正是爲了解決這一問題而生的。
-
-
說明
Redis Sentinel 屬於主從備份的範疇,主從數據同步使用的是redis自帶的主從配置,不同的是sentinel是在此基礎上加入的故障檢測和轉移,防止Redis單點故障。
友情提示: 哨兵模式是redis一個架構模式,但業務使用該架構是需要業務代碼端實現的,不能直接連接redis,而是鏈接redis的哨兵端口
哨兵檢查redis節點並返回正確的哨兵連接的Redis節點鏈接配置,有業務端根據鏈接創建Redis進程池或TCP握手進程。
Redis主從複製是直接通過tcp進行數據交流的,不是像mysql的文件複製。
最終的目標
- 架構圖
Redis Sentinel 最終搭建目標架構圖
實現功能點
- Setinel 集羣
- Redis 主從複製
- Sentinel 集羣實現 Redis 高可用(故障自動轉移)
redis sentinel 配置
-
redis sentinel 節點1:redis-sentinel-26379.conf
- 新建 redis-sentinel-26379.conf
可從 /urs/redis/redis-sentinel.conf 複製到 /urs/redis/config/redis-sentinel.conf
在config 目錄下執行cp ../redis-sentinel.conf ./redis-sentinel-26379.conf
-
修改配置 redis-sentinel-26379.conf:
port 26379 daemonize yes pidfile /var/run/redis-sentinel-26379.pid logfile "redis-sentinel-26379.log" # 監控節點,且超過2個sentinel 任務故障,方可執行故障轉移 sentinel monitor mymaster 127.0.0.1 7000 2 # 如果節點在 30000毫秒內未迴應,就認爲故障 sentinel down-after-milliseconds mymaster 30000 # 如果故障轉移後,同時進行主從複製數爲 1 sentinel parallel-syncs mymaster 1 # 故障轉移的超時時間 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
-
redis sentinel 節點2:redis-sentinel-26380.conf
-
創建
touch redis-sentinel-26380.conf
-
修改配置 redis-sentinel-26380.conf:
port 26380 daemonize yes pidfile /var/run/redis-sentinel-26380.pid logfile "redis-sentinel-26380.log" # 監控節點,且超過2個sentinel 任務故障,方可執行故障轉移 sentinel monitor mymaster 127.0.0.1 7000 2 # 如果節點在 30000毫秒內未迴應,就認爲故障 sentinel down-after-milliseconds mymaster 30000 # 如果故障轉移後,同時進行主從複製數爲 1 sentinel parallel-syncs mymaster 1 # 故障轉移的超時時間 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
-
-
redis sentinel 節點3:redis-sentinel-26381.conf
-
創建
touch redis-sentinel-26381.conf
port 26381 daemonize yes pidfile /var/run/redis-sentinel-26381.pid logfile "redis-sentinel-26381.log" # 監控節點,且超過2個sentinel 任務故障,方可執行故障轉移 sentinel monitor mymaster 127.0.0.1 7000 2 # 如果節點在 30000毫秒內未迴應,就認爲故障 sentinel down-after-milliseconds mymaster 30000 # 如果故障轉移後,同時進行主從複製數爲 1 sentinel parallel-syncs mymaster 1 # 故障轉移的超時時間 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
-
啓動 redis sentinel
-
啓動命令:
./src/redis-sentinel ./config/redis-sentinel-26379.conf ./src/redis-sentinel ./config/redis-sentinel-26380.conf ./src/redis-sentinel ./config/redis-sentinel-26381.conf redis-sentinel redis-sentinel-26379.conf redis-sentinel redis-sentinel-26380.conf redis-sentinel redis-sentinel-26381.conf
-
驗證:
ps -ef | grep redis-sentinel
-
./src/redis-cli -p 26379 info sentinel
至此 Redis 主從和 Redis Sentinel 已經搭建完成了。接下來驗證故障轉移。
故障轉移演示
1. laravel5.5以上配置使用哨兵模式
laravel底層自帶哨兵模式配置,所以只需要配置好哨兵連接即可,Redis在使用時無需做任何更改。
//配置文件`config/database.php`
'redis' => [
'client' => 'predis', //指示redis客戶端使用的是predis組件
'default' => [
'tcp://127.0.0.1:26379',
'tcp://127.0.0.1:26381',
'tcp://127.0.0.1:26382', //這3個都是sentinel節點的地址
'options' => [
'replication' => 'sentinel',
'service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'), //sentinel
'parameters' => [
'password' => env('REDIS_PASSWORD', null), //redis的密碼,沒有時寫null
'database' => 0,
],
],
],
],
編寫命令並使用
php artisan test
$redis = new \Illuminate\Support\Facades\Redis();
while (true) {
sleep(1);
try {
$redis::set($key='test_sentinel:' . time(), 1);
echo $redis::get($key);
} catch (Exception $e) {
echo $e->getMessage();
}
echo "\n";
}