基於docker-compose實現redis高可用集群(哨兵模式)
yls
2019-9-20
簡介
- 基於docker和docker-compose
- 使用redis集群和sentinel集群,達到redis高可用,為緩存做鋪墊
- 每一個redis要匹配一個sentinel;多個sentinel之間要互相關聯
搭建redis集群
-
新建redis文件夾,在該文件夾下創建docker-compose.yml文件,輸入如下內容
version: '3.7' services: master: image: redis container_name: redis-master restart: always command: redis-server /etc/redis/redis.conf --bind 0.0.0.0 --port 6382 --requirepass test@dbuser2018 --appendonly yes ports: - 6382:6382 network_mode: "host" volumes: - ./data:/data - ./redis.conf:/etc/redis/redis.conf slave1: image: redis container_name: redis-slave-1 restart: always command: redis-server /etc/redis/redis.conf --bind 0.0.0.0 --slaveof 39.97.234.52 6382 --port 6380 --requirepass test@dbuser2018 --masterauth test@dbuser2018 --appendonly yes ports: - 6380:6380 network_mode: "host" volumes: - ./data:/data - .redis.conf:/etc/redis/redis.conf slave2: image: redis container_name: redis-slave-2 restart: always #39.97.234.52設置成公網,當前容器ip就是公網ip #--bind 0.0.0.0表示任何ip都可以訪問當前容器 command: redis-server /etc/redis/redis.conf --bind 0.0.0.0 --slaveof 39.97.234.52 6382 --port 6381 --requirepass test@dbuser2018 --masterauth test@dbuser2018 --appendonly yes ports: - 6381:6381 #與主機使用同一個ip network_mode: "host" volumes: - ./data:/data - ./redis.conf:/etc/redis/redis.conf
-
運行redis容器:在docker-compose.yml當前所在文件夾下,運行命令
docker-compose up -d
- 可以使用如下命令查看日志
docker logs -f 容器ID
搭建sentinel集群
-
新建sentinel文件夾,在該文件夾下創建docker-compose.yml文件,輸入如下內容
version: '3.1' services: sentinel1: image: redis container_name: redis-sentinel-1 ports: - 26379:26379 command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - ./sentinel1.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
-
創建三個數據卷配置文件(sentinel1.conf,sentinel2.conf,sentinel3.conf),分別為sentinel容器提供配置,我這里將三個配置文件內容設置成一樣
port 26379
dir /tmp
# 自定義集群名,其中 39.97.234.52 為 redis-master 的 ip,6382 為 redis-master 的端口,2 為最小投票數(因為有 3 台 Sentinel 所以可以設置成 2)
sentinel monitor mymaster 39.97.234.52 6382 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster test@dbuser2018
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
-
運行sentinel集群:與docker-compose同級目錄下,運行如下命令
docker-compose up -d
驗證是否成功部署
-
以交互的方式進入任意一個sentinel容器
docker exec -it redis-sentinel-1 bash
-
在進入的sentinel容器中,通過命令的方式進入redis客戶端
redis-cli -p 26379
-
在客戶端中查看此sentinel對應的redis主節點信息
sentinel master mymaster
-
能看到如下結果,說明正常
1) "name" 2) "mymaster" 3) "ip" 4) "192.168.145.128" 5) "port" 6) "6379" 7) "runid" 8) "324f148cf69ef6d2a86cfa4bdfc4f6937974b7b8" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "561" 19) "last-ping-reply" 20) "561" 21) "down-after-milliseconds" 22) "300000" 23) "info-refresh" 24) "7109" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "4454165" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1"
-
如下命令查看redis從節點是否正常
sentinel slaves mymaster
- 顯示如下說明正常
1) "name"
2) "39.97.234.52:6381"
3) "ip"
4) "39.97.234.52"
5) "port"
6) "6381"
7) "runid"
8) "4223d038a4899d77d17e15c8f7af446649c6ddad"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "111"
19) "last-ping-reply"
20) "111"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "2984"
25) "role-reported"
26) "slave"
27) "role-reported-time"
28) "1145357"
29) "master-link-down-time"
30) "0"
31) "master-link-status"
32) "ok"
33) "master-host"
34) "39.97.234.52"
35) "master-port"
36) "6382"
37) "slave-priority"
38) "100"
39) "slave-repl-offset"
40) "164572"