Redis集群系列之Redis Sentinel(哨兵模式)搭建


Redis Sentinel (哨兵模式) 搭建

Redis哨兵模式是基於Redis主從方案實現的

前提概要

  • Redis高可以用有三種常用配置方式:

    1. Redis自帶主從配置,可以直接實現,多機器為從,只讀,master可寫

      • 主節點Master可讀、可寫.

      • 從節點Slave只讀。(read-only)

      主從模型可以提高讀的能力,在一定程度上緩解了寫的能力。因為能寫仍然只有Master節點一個,
      可以將讀的操作全部移交到從節點上,變相提高了寫能力

    2. 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 已經搭建完成了。接下來驗證故障轉移。

故障轉移演示

laravel5.5以上配置使用哨兵模式


//配置文件`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";
    }


免責聲明!

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



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