基于docker搭建redis-sentinel集群的方法示例
1、概述
Redis 集群可以在一組 redis 節(jié)點(diǎn)之間實(shí)現(xiàn)高可用性和 sharding。在集群中會(huì)有 1 個(gè) master 和多個(gè) slave 節(jié)點(diǎn)。當(dāng) master 節(jié)點(diǎn)失效時(shí),應(yīng)選舉出一個(gè) slave 節(jié)點(diǎn)作為新的 master。然而 Redis 本身(包括它的很多客戶端)沒(méi)有實(shí)現(xiàn)自動(dòng)故障發(fā)現(xiàn)并進(jìn)行主備切換的能力,需要外部的監(jiān)控方案來(lái)實(shí)現(xiàn)自動(dòng)故障恢復(fù)。
Redis Sentinel 是官方推薦的高可用性解決方案。它是 Redis 集群的監(jiān)控管理工具,可以提供節(jié)點(diǎn)監(jiān)控、通知、自動(dòng)故障恢復(fù)和客戶端配置發(fā)現(xiàn)服務(wù)。
2、遇到的問(wèn)題
1、docker host網(wǎng)絡(luò)
docker使用host網(wǎng)絡(luò)時(shí)對(duì)于windows 、mac不生效(沒(méi)找到解決方案),最后放棄了windows 使用centos部署集群。
2、不使用host網(wǎng)絡(luò)的情況下sentinel 連接問(wèn)題
不使用host網(wǎng)絡(luò)的情況下連接sentinel集群時(shí)可以指定主節(jié)點(diǎn)端口故可以正常聯(lián)通, 但在主節(jié)點(diǎn)故障時(shí) sentinel 從主節(jié)點(diǎn)獲取到的 IP 是容器內(nèi)的虛擬 IP 導(dǎo)致集群無(wú)法正常連接。
3、搭建過(guò)程
1、目錄結(jié)構(gòu)
2、sentinel 配置文件
1、sentinel1.conf
#端口號(hào) port 26379 dir /tmp # mymaster:自定義集群名,2:投票數(shù)量必須2個(gè)sentinel才能判斷主節(jié)點(diǎn)是否失敗 sentinel monitor mymaster <ip> <port> 2 # 指的是超過(guò)5000秒,且沒(méi)有回復(fù),則判定主節(jié)點(diǎn)不可達(dá) sentinel down-after-milliseconds mymaster 5000 # 表示在故障轉(zhuǎn)移的時(shí)候最多有numslaves在同步更新新的master sentinel parallel-syncs mymaster 1 # 故障轉(zhuǎn)移超時(shí)時(shí)間 sentinel failover-timeout mymaster 5000
2、sentinel2.conf
#端口號(hào) port 26380 dir /tmp # mymaster:自定義集群名,2:投票數(shù)量必須2個(gè)sentinel才能判斷主節(jié)點(diǎn)是否失敗 sentinel monitor mymaster <ip> <port> 2 # 指的是超過(guò)5000秒,且沒(méi)有回復(fù),則判定主節(jié)點(diǎn)不可達(dá) sentinel down-after-milliseconds mymaster 5000 # 表示在故障轉(zhuǎn)移的時(shí)候最多有numslaves在同步更新新的master sentinel parallel-syncs mymaster 1 # 故障轉(zhuǎn)移超時(shí)時(shí)間 sentinel failover-timeout mymaster 5000
3、sentinel3.conf
#端口號(hào) port 26381 dir /tmp # mymaster:自定義集群名,2:投票數(shù)量必須2個(gè)sentinel才能判斷主節(jié)點(diǎn)是否失敗 sentinel monitor mymaster <ip> <port> 2 # 指的是超過(guò)5000秒,且沒(méi)有回復(fù),則判定主節(jié)點(diǎn)不可達(dá) sentinel down-after-milliseconds mymaster 5000 # 表示在故障轉(zhuǎn)移的時(shí)候最多有numslaves在同步更新新的master sentinel parallel-syncs mymaster 1 # 故障轉(zhuǎn)移超時(shí)時(shí)間 sentinel failover-timeout mymaster 5000
3、docker-compose.yml
version: '2' services: master: image: redis:4.0 restart: always container_name: redis-master #使用主機(jī)網(wǎng)絡(luò) 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 # 使用數(shù)據(jù)卷映射文件到指定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
4、使用centos 部署集群測(cè)試效果
1、測(cè)試通過(guò)sentinel1連接集群

2、測(cè)試主節(jié)點(diǎn)子節(jié)點(diǎn)數(shù)據(jù)同步


3、關(guān)閉master查看主備切換

sentinel 正常聯(lián)通

主節(jié)點(diǎn)從16379 切換 至16381

結(jié)尾
端午之后偷了一周的懶,之前就搭建了一次sentinel 集群由于docker 網(wǎng)絡(luò)模型問(wèn)題導(dǎo)致主備節(jié)點(diǎn)切換后集群連接不上,昨天看到host不能在window上實(shí)現(xiàn)就放到centos上測(cè)試了一番完美搞定。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vscode利用ssh配置docker容器并開(kāi)啟遠(yuǎn)程編程模式的詳細(xì)步驟
這篇文章主要介紹了vscode利用ssh配置docker容器內(nèi)開(kāi)啟遠(yuǎn)程編程模式,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
docker實(shí)現(xiàn)導(dǎo)出、導(dǎo)入和數(shù)據(jù)搬遷
本篇文章主要介紹了docker實(shí)現(xiàn)導(dǎo)出、導(dǎo)入和數(shù)據(jù)搬遷,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Docker搭建私有倉(cāng)庫(kù)的實(shí)現(xiàn)步驟
因?yàn)閐ockerHub公共倉(cāng)庫(kù)是外網(wǎng)的,所以訪問(wèn)就特別慢,所以一般公司都會(huì)搭建私人的鏡像倉(cāng)庫(kù)來(lái)保存鏡像,本文主要介紹了Docker搭建私有倉(cāng)庫(kù)的實(shí)現(xiàn)步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Dockerfile 部署java web的環(huán)境詳解
這篇文章主要介紹了Dockerfile 構(gòu)建java web 環(huán)境詳解的相關(guān)資料,這里對(duì)Dockerfile 進(jìn)行基本介紹,并說(shuō)明如何構(gòu)建java web環(huán)境進(jìn)行詳解,需要的朋友可以參考下2016-12-12
docker環(huán)境中websocket?通過(guò)nginx代理不通解決方案
這篇文章主要介紹了docker環(huán)境中websocket?通過(guò)nginx代理不通解決方案,下面是一些可能會(huì)導(dǎo)致?WebSocket?代理失敗的問(wèn)題以及相應(yīng)的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

