docker-compose 搭建高可用Redis 主從、哨兵集羣

概述

Redis哨兵爲Redis提供了高可用性。實際上這意味着你可以使用哨兵模式創建一個可以不用人爲干預而應對各種故障的Redis部署。

哨兵模式還提供了其他的附加功能,如監控,通知,爲客戶端提供配置。

下面是在宏觀層面上哨兵模式的功能列表:

監控:哨兵不斷的檢查master和slave是否正常的運行。
通知:當監控的某臺Redis實例發生問題時,可以通過API通知系統管理員和其他的應用程序。
自動故障轉移:如果一個master不正常運行了,哨兵可以啓動一個故障轉移進程,將一個slave升級成爲master,其他的slave被重新配置使用新的master,並且應用程序使用Redis服務端通知的新地址。
配置提供者:哨兵作爲Redis客戶端發現的權威來源:客戶端連接到哨兵請求當前可靠的master的地址。如果發生故障,哨兵將報告新地址。

1. 創建Redis主從資源編排文件

創建redis集羣的docker-compose

設置一個主容器和兩個從容器,並在啓動時設置密碼。
由於是docker容器內環境,主從需要相互通信,所以給它們配置網絡,同時也是爲了下面哨兵能夠ping通集羣做準備。

version: '2'
services:
  master:
    image: redis       ## 鏡像
    container_name: redis-master
    command: redis-server --requirepass 123456
    ports:
    - "6379:6379"
    networks:
    - sentinel-master
  slave1:
    image: redis                ## 鏡像
    container_name: redis-slave-1
    ports:
    - "6380:6379"           ## 暴露端口
    command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 
    depends_on:
    - master
    networks:
    - sentinel-master
  slave2:
    image: redis                ## 鏡像
    container_name: redis-slave-2
    ports:
    - "6381:6379"           ## 暴露端口
    command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
    depends_on:
    - master
    networks:
    - sentinel-master
networks:
  sentinel-master:

2. 創建配置哨兵文件

由於使用docker環境,所以ip爲docker內的內部ip,可使用如下命令獲取master節點的ip地址。

docker inspect 容器id

可獲取到容器在同一網絡環境下的內網ip

哨兵配置文件sentinel.conf如下:

port 26379
dir /tmp
sentinel monitor mymaster 172.28.0.3 6379 2 
sentinel auth-pass mymaster 123456 
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000  
sentinel deny-scripts-reconfig yes
第三行表示Redis監控一個叫做mymaster的運行在172.28.0.3:6379的master,投票達到2則表示master以及掛掉了。  
第四行設置主節點的密碼  
第五行表示在一段時間範圍內sentinel向master發送的心跳PING沒有回覆則認爲master不可用了。 
第六行的parallel-syncs表示設置在故障轉移之後,同時可以重新配置使用新master的slave的數量。數字越低,更多的時間將會用故障轉移完成,但是如果slaves配置爲服務舊數據,你可能不希望所有的slave同時重新同步master。因爲主從複製對於slave是非阻塞的,當停止從master加載批量數據時有一個片刻延遲。通過設置選項爲1,確信每次只有一個slave是不可到達的。
第七行表示10秒內mymaster還沒活過來,則認爲master宕機了。

複製粘貼出相同的三份:

cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf

3. 創建哨兵集羣資源編排文件

配置哨兵的網絡環境和主從容器的環境相同。

version: '2'
services:
  sentinel1:
    image: redis       ## 鏡像
    container_name: redis-sentinel-1
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - "./sentinel.conf:/usr/local/etc/redis/sentinel.conf"
  sentinel2:
    image: redis                ## 鏡像
    container_name: redis-sentinel-2
    ports:
    - "26380:26379"           
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - "./sentinel2.conf:/usr/local/etc/redis/sentinel.conf"
  sentinel3:
    image: redis                ## 鏡像
    container_name: redis-sentinel-3
    ports:
    - "26381:26379"           
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
  default:
    external:
      name: redis_sentinel-master

4. 啓動Redis集羣

cd /home/redis-sentinel/redis
docker-compose up -d

5. 啓動哨兵集羣

cd /home/redis-sentinel/sentinel  
docker-compose up -d

6. 查看集羣健康情況

docker ps

可以看到整個集羣已經成功跑起來了。

7. 進入哨兵節點,查看是否有主節點

使用命令:

docker exec -it 哨兵容器id bash
redis-cli -p 26379
sentinel master mymaster

可以看到存在主節點mymaster和兩個從節點。

8. 測試故障遷移

查看哨兵當前的master節點ip:

關掉master主節點:

間隔10s(自己配置的failover-timeout),查看哨兵的是否會檢測到master節點宕機,並做主從切換:
再次查看哨兵信息,可發現master節點的ip已經更換位從節點了,也就是從節點升級成了主節點,而當宕掉的主節點從新恢復時,它便會成爲從節點了。

到此,我們的Redis哨兵集羣以成功工作了。

後面,還會出一篇博文使用SpringBoot連接Redis集羣。

學習本就逆流而上,不進即退。

參考鏈接:
https://www.cnblogs.com/PatrickLiu/p/8444546.html

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