基于docker-compose实现redis高可用集群(哨兵模式)
简介
- 基于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"