Windows 環境如何設定Redis Master-Slave 與Sentinel

準備工作

  1. 下載Windows版Redis

    • .msi可以直接安裝爲Windows Service (但設定調整比較麻煩,建議使用.zip )

    • .zip

           建議不要放在OS 槽,會少掉許多權限設定問題

2.準備建立Master-Slave 與Sentinel(下列方式擇一即可)

  • 採取實體隔離
    • 將zip 解壓縮至資料夾後複製多份並標記用途(eg Redis-{Master|SlaveSentinel})後備用
  • 僅建立不同用途之config

3.以下demo 將以建立不同config 方式進行

Master-Slave 設定

  • 調整redis.windows.confconfig設定

    1. Master
      • 保留預設值即可
    2. Slave

      • 如果採用config 隔離,請記得修改檔案(eg redis.windows6380.conf)
      • 修改port (不可與master port 重複)
      • 加入slave 設定

        slaveof {masterip} {masterport>}

  • 啓動Master & Slave (下列方式擇一即可)

    1. 指令

      • 啓動master

        • 執行redis-server.exe 並指定使用master config

          redis-server redis.windows.conf

          1master

      • 啓動slave

        • 執行redis-server 並指定使用slave config (執行前確認config 是否已正確修改)

          redis-server redis.windows6380.conf

          2slave

        • 啓動後Master 也會出現sync 資料的訊息

          3slavesync

    2. Windows Service

           如果路徑包含空白請記得使用跳脫字元\搭配"將路徑包起來,下面Sentinel有使用範例

          設定master

sc.exe create  "Redis6379"  start = auto binPath= "c:\redis\redis-server.exe --service-run c:\redis\redis.windows-service.conf" DisplayName= "Redis6379"

          設定slave

sc.exe create  "Redis6380"  start = auto binPath= "c:\redis\redis-server.exe --service-run c:\redis\redis.windows-service6380.conf" DisplayName= "Redis6380"
  • 確認執行狀況

    1. Master

      • redis-cli -h 127.0.0.1 -p 6379 info replication

        4masterinfo

    2. Slave

      • redis-cli -h 127.0.0.1 -p 6380 info replication

        5slaveinfo

Sentinel config 設定

Winodws 7 使用Windows Redis 3.2.100 版本無法啓動sentinel,需使用3.0.504 版

  • 新增sentinel.conf 檔案
  • 逐一加入以下設定

    • 指定port

      port 26379

    • 指定監視名爲masterip爲127.0.0.1port爲6379的redis master instance,最後數字代表幾個sentinel同意才進行切換

      sentinel monitor master 127.0.0.1 6379 1

    • 指定3000毫秒沒有迴應就視名爲master的instance失效

      sentinel down-after-milliseconds master 3000

    • 指定18000毫秒未完成視爲failover失敗

      sentinel failover-timeout master 18000

    • 指定同時只有1個salve可以從新master同步資料回去

      sentinel parallel-syncs master 1

      • 數字愈小,failover 耗時就愈久(需等所有slave 都同步完)
      • slave 同步資料可能會造成slave 無法迴應,所以也不建議設太大
    • 指定連線密碼爲password(非必要)

      sentinel auth-pass password

  • 最終設定範例

port 26379
sentinel  monitor  master 127 .0 .0 .1 6379 1
sentinel  down-after-milliseconds  master 3000
sentinel  failover-timeout  master 18000
sentinel  parallel-syncs  master 1

啓動Sentinel

  1. 指令

    • 執行redis-server.exe並指定使用sentinel config及--sentinel參數(執行前請確認sentinel config已設定)

      • redis-server.exe sentinel.conf --sentinel

        6sentinel

  2. Windows Service

    • 如果路徑包含空白請記得使用跳脫字元\搭配"將路徑包起來

sc.exe create  "RedisSentinel"  start = auto binPath= "\"c:\Program Files\redis\redis-server.exe\" --service-run \"c:\Program Files\redis\sentinel.conf\" --sentinel" DisplayName= "RedisSentinel"

確認執行狀態

redis-cli -h 127 .0 .0 .1 -p 26379 info sentinel

7sentinelinfo

模擬master 失效

  1. 手動shutdown master

    • redis-cli -h 127.0.0.1 -p 6379 shutdown

      8mastershutdown

  2. 確認sentinel 資訊

    • consoel 出現failover 操作資訊

      9sentinelconsole

    • config 也自動修改了

      10sentinelconfig

  3. 確認slave 資訊

    • console 出現啓用Master mode

      11slave2master

    • config

      slaveof {masterip} {masterport>} 已被移除

    • 使用redis-cli -h 127.0.0.1 -p 6380 info replication確認

      12slave2master

模擬slave 接替成爲master 後,原master 回覆服務

  1. 手動shutdown master

    • redis-cli -h 127.0.0.1 -p 6379 shutdown

      8mastershutdown

  2. 確認sentinel 資訊

    • consoel 出現failover 操作資訊

      9sentinelconsole

    • config 也自動修改了

      10sentinelconfig

  3. 確認slave 資訊

    • console 出現啓用Master mode

      11slave2master

    • config

      slaveof {masterip} {masterport>} 已被移除

    • 使用redis-cli -h 127.0.0.1 -p 6380 info replication確認

      12slave2master

  4. 回覆已shutdown 的master

    • 再次啓動原master

      • 自動連線至新master (原slave) ,併成爲其slave 開始同步資料

        13master2slave

    • 新master (原slave) 同步資料至原master(新slave)

      14slavesynctomaster

    • Sentinel偵測到原 master加入並將其指定爲slave

      15sentinelslave

    • 原master(新slave) 的config 會被自動加上 slaveof {新 master} {新 masterport>}

    • 使用指令確認一下

      • 原master:6379 –> slave

        • redis-cli -h 127.0.0.1 -p 6379 info replication

          176379

      • 原slave :6380 –> master

        • redis-cli -h 127.0.0.1 -p 6380 info replication

          166380

使用ServiceStack.Redis鏈接訪問代碼

        public IRedisClient connectredis()
        {
            var sentinelHosts = new[] { "127.0.0.1:26379" };
            var sentinel = new RedisSentinel(sentinelHosts, masterName: "master");
            sentinel.SentinelWorkerConnectTimeoutMs = 500;
            IRedisClientsManager redisManager = sentinel.Start();
            return redisManager.GetClient();

        }

        [TestMethod]
        public void RedisSetValue()
        {
            for (int i = 0; i < 100; i++)
            {
                IRedisClient redisClient = connectredis();
                redisClient.Db = 0;
                //redisClient.Remove(i.ToString());
                redisClient.Set<string>(i.ToString(), i.ToString());
                Thread.Sleep(3000);
                System.Diagnostics.Debug.WriteLine(i.ToString());
            }
        }

因爲使用ServiceStack.redis結合密碼模式不知道如何配置,所以用了下面的案例?歡迎各位大神賜教!!

結合StackExchange.Redis使用密碼模式

在主從節點得配置文件中添加:

masterauth "foobared"
requirepass "foobared"

在哨兵的配置文件中添加

sentinel auth-pass master foobared

代碼案例:

        [TestMethod]
        public void RedisSetValue()
        {
            for (int i = 0; i < 100; i++)
            {
                ConfigurationOptions sentinelOptions = new ConfigurationOptions();
                sentinelOptions.EndPoints.Add("127.0.0.1", 26379);
                sentinelOptions.TieBreaker = "";
                sentinelOptions.CommandMap = CommandMap.Sentinel;
                sentinelOptions.AbortOnConnectFail = false;
                sentinelOptions.AllowAdmin = true;
                // Connect!
                ConnectionMultiplexer sentinelConnection = ConnectionMultiplexer.Connect(sentinelOptions);

                // Get a connection to the master
                ConfigurationOptions redisServiceOptions = new ConfigurationOptions();
                redisServiceOptions.ServiceName = "master";   //master名稱
                redisServiceOptions.Password = "foobared";     //master訪問密碼
                redisServiceOptions.AbortOnConnectFail = true;
                redisServiceOptions.AllowAdmin = true;
                ConnectionMultiplexer masterConnection = sentinelConnection.GetSentinelMasterConnection(redisServiceOptions);

                var db = masterConnection.GetDatabase(2);

                db.StringSet(i.ToString(), i.ToString(), TimeSpan.FromMinutes(10));

                Debug.WriteLine(i.ToString());
            }

        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章