Redis 高可用之哨兵模式


 

 

 

參考   :  

  https://mp.weixin.qq.com/s/Z-PyNgiqYrm0ZYg0r6MVeQ

 這篇文章有兩個問題

  1.雖然運行了3個sentinel容器,實際上只有一個sentinel運行 具體可以留意輸出的最后一行

        

  2.外部調試連接redis 獲取到的monster地址是docker 的虛擬ip(172.17.0.2:6379)地址,外部不能訪問,但是如果你把新建的springboot 項目和這些容器放到同一個主機的話是沒有問題的

 詳細解決方案 查看  

Redis 高可用之哨兵模式(二)

   

一、redis高可用解決方案

  1. redis主從 

    優點:1、高可靠性,主從實時備份,有效解決單節點數據丟失問題。

       2、可做讀寫分離,從庫分擔讀操作,緩解主庫壓力

    缺點:主庫異常,需要手動主從切換

     2.redis哨兵模式

    優點:1、有效解決主從模式主庫異常手動主從切換的問題

    缺點:1、運維復雜,哨兵選舉期間,不能對外提供服務

   其他解決方案優缺點,可以查看 高可用 ,本篇主要介紹哨兵解決方案

二、具體實現

  windows上安裝redis新版本好像已經不支持了,本文采用的是docker快速實現redis高可用實例的創建

  具體環境參數:

    操作系統:centos7

    docker:18.09.3

 

1、拉取鏡像,采用docker官方鏡像

docker pull docker.io/redis

2、創建redis配置文件

mkdir  /data/redis/conf
cd /data/redis/conf
touch redis.conf
vi redis.conf

其中配置內容如下

logfile "redis.log"
port 6379
dir /data
appendonly yes
appendfilename appendonly.aof
requirepass 123456

3、創建從庫redis配置文件 文件名稱命名為 redis_01.conf  

其中內容如下:其中slaveof 的ip 是宿主的docker0網卡的ip 具體ip需要在主庫容器運行起來后,通過 docker inspect redis-6379 命令來查看ip地址

 

logfile "redis.log"
port 6379
dir /data
appendonly yes
appendfilename appendonly.aof
slaveof 172.21.186.236 6379
masterauth 123456
requirepass 123456

 4、運行容器

  

docker run --name redis-6379 -v /data/conf/redis.conf:/data/redis.conf -p 6379:6379  -d docker.io/redis redis-server /data/redis.conf

docker run --name redis-6380 -v /data/conf/redis_01.conf:/data/redis.conf -p 6380:6379  -d docker.io/redis redis-server /data/redis.conf

docker run --name redis-6381 -v /data/conf/redis_02.conf:/data/redis.conf -p 6381:6379  -d docker.io/redis redis-server /data/redis.conf

5、查看主庫運行情況

[root@localhost conf]# docker exec -ti redis-6379 /bin/bash
root@9865d9442e67:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.3,port=6379,state=online,offset=714,lag=1
slave1:ip=172.17.0.4,port=6379,state=online,offset=714,lag=1
master_replid:6baa315b071196f7530bf2c9fdfb132785deeadb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:714
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:714
127.0.0.1:6379>

 可以看到已經有兩個從庫

6、創建sentinel.conf

touch sentinel.conf
vi sentinel.conf

其中sentinel.conf內容如下

logfile "sentinel.log"
sentinel monitor mymaster 172.17.0.2 6379 1

7、創建sentinel容器

docker run --name redis-s-6379 -v /data/conf/sentinel.conf:/data/sentinel.conf -p 26379:26379 -d docker.io/redis redis-sentinel sentinel.conf
docker run --name redis-s-6380 -v /data/conf/sentinel.conf:/data/sentinel.conf -p 26380:26379 -d docker.io/redis redis-sentinel sentinel.conf
docker run --name redis-s-6381 -v /data/conf/sentinel.conf:/data/sentinel.conf -p 26381:26379 -d docker.io/redis redis-sentinel sentinel.conf

 8、查看sentinel運行狀態

[root@localhost conf]# docker exec -ti redis-s-6380 /bin/bash
root@430b54968068:/data# redis-cli -h 127.0.0.1 -p 26379
127.0.0.1:26379> info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.17.0.2:6379,slaves=2,sentinels=1
127.0.0.1:26379>

9、停止主庫 

docker stop redis-6379

10、進入sentinel容器,查看日志

docker exec -ti redis-s-6379 /bin/bash
cat sentinel.log

紅色划線部分可以看到成功切換主從

11、重新啟動redis-6379 容器

docker start redis-6379

12、進入容器內部查看redis運行情況

docker exec -ti redis-6379 /bin/bash

可以看到已經成為從庫

 

  


免責聲明!

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



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