基於 docker 搭建 redis-sentinel 集羣

一、概述

Redis 集羣能夠在一組 redis 節點之間實現高可用性和 sharding。在集羣中會有 1 個 master 和多個 slave 節點。當 master 節點失效時,應選舉出一個 slave 節點做爲新的 master。然而 Redis 自己(包括它的不少客戶端)沒有實現自動故障發現並進行主備切換的能力,須要外部的監控方案來實現自動故障恢復。redis

Redis Sentinel 是官方推薦的高可用性解決方案。它是 Redis 集羣的監控管理工具,能夠提供節點監控、通知、自動故障恢復和客戶端配置發現服務。docker

二、遇到的問題

一、docker host網絡

docker使用host網絡時對於windows 、mac不生效(沒找到解決方案),最後放棄了windows 使用centos部署集羣。windows

二、不使用host網絡的狀況下sentinel 鏈接問題

不使用host網絡的狀況下鏈接sentinel集羣時能夠指定主節點端口故能夠正常聯通, 但在主節點故障時 sentinel 從主節點獲取到的 IP 是容器內的虛擬 IP 致使集羣沒法正常鏈接。centos

三、搭建過程

一、目錄結構

二、sentinel 配置文件

一、sentinel1.conf

#端口號
port 26379
dir /tmp
# mymaster:自定義集羣名,2:投票數量必須2個sentinel才能判斷主節點是否失敗
sentinel monitor mymaster <ip> <port> 2
# 指的是超過5000秒,且沒有回覆,則斷定主節點不可達
sentinel down-after-milliseconds mymaster 5000
# 表示在故障轉移的時候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障轉移超時時間
sentinel failover-timeout mymaster 5000

複製代碼

二、sentinel2.conf

#端口號
port 26380
dir /tmp
# mymaster:自定義集羣名,2:投票數量必須2個sentinel才能判斷主節點是否失敗
sentinel monitor mymaster <ip> <port> 2
# 指的是超過5000秒,且沒有回覆,則斷定主節點不可達
sentinel down-after-milliseconds mymaster 5000
# 表示在故障轉移的時候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障轉移超時時間
sentinel failover-timeout mymaster 5000

複製代碼

三、sentinel3.conf

#端口號
port 26381
dir /tmp
# mymaster:自定義集羣名,2:投票數量必須2個sentinel才能判斷主節點是否失敗
sentinel monitor mymaster <ip> <port> 2
# 指的是超過5000秒,且沒有回覆,則斷定主節點不可達
sentinel down-after-milliseconds mymaster 5000
# 表示在故障轉移的時候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障轉移超時時間
sentinel failover-timeout mymaster 5000

複製代碼

三、docker-compose.yml

version: '2'
services:
  master:
    image: redis:4.0
    restart: always
    container_name: redis-master
    #使用主機網絡
    network_mode: "host"
    command: redis-server --port 16379  
   
  slave1:
    image: redis:4.0
    restart: always
    container_name: redis-slave-1
    network_mode: "host"
    # 指定端口並指定master ip 端口
    command: redis-server --port 16380 --slaveof <master ip> 16379
   
  slave2:
    image: redis:4.0
    restart: always
    container_name: redis-slave-2
    network_mode: "host"    
    command: redis-server --port 16381 --slaveof <master ip> 16379
    
  sentinel1:
    image: redis:4.0
    restart: always
    container_name: redis-sentinel-1
    network_mode: "host"
    # 指定sentinel文件位置
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    # 使用數據卷映射文件到指定sentinel位置
    volumes:
      - ./sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
   
  sentinel2:
    image: redis:4.0
    restart: always
    container_name: redis-sentinel-2
    network_mode: "host"    
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
   
  sentinel3:
    image: redis:4.0
    restart: always
    container_name: redis-sentinel-3
    network_mode: "host"    
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf  
    
複製代碼

四、使用centos 部署集羣測試效果

一、測試經過sentinel1鏈接集羣

二、測試主節點子節點數據同步

三、關閉master查看主備切換

sentinel 正常聯通
主節點從16379 切換 至16381

結尾

端午以後偷了一週的懶,以前就搭建了一次sentinel 集羣因爲docker 網絡模型問題致使主備節點切換後集羣鏈接不上,昨天看到host不能在window上實現就放到centos上測試了一番完美搞定。bash

相關文章
相關標籤/搜索