老规矩,废话不多说,直接上步骤加图示。
1.拉取redis镜像,直接最新版本
docker pull redis:latest
2.创建全部需要的文件夹和文件,我这里目录如图:
各目录作用介绍:
masterslave -存放redis主从docker-compose.yml文件
redisconf -存放redis哨兵主配置文件
sentinel -存放redis的docker-compose.yml文件和哨兵多个配置文件
3.reids主从docker-compose.yml
version: '3.7'
services:
master:
image: redis
container_name: redis-master
restart: always
command: redis-server --requirepass 123456 --appendonly yes
ports:
- 6379:6379
#数据卷目录挂载,都是当前目录,跟yml文件同级
volumes:
- ./data1:/data
slave1:
image: redis
container_name: redis-slave-1
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 --appendonly yes
ports:
- 6380:6379
volumes:
- ./data2:/data
slave2:
image: redis
container_name: redis-slave-2
restart: always
command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 --appendonly yes
ports:
- 6381:6379
volumes:
- ./data3:/data
4.进入redis主从目录,先启动redis主从
5.创建redis哨兵主配置文件和多个配置文件
主配置文件:
# 自定义集群名,192.168.001.220 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 #2),kingmaster是可以自定义集群的名字
port 26379
dir /tmp
sentinel monitor kingmaster 192.168.001.220 6379 2
sentinel down-after-milliseconds kingmaster 30000
sentinel parallel-syncs kingmaster 1
sentinel auth-pass kingmaster redispwd
sentinel failover-timeout kingmaster 180000
sentinel deny-scripts-reconfig yes
将此文件复制到指定位置3份并重命名
cp sentinel.conf /home/sentinelredis/sentinel/sentinel1.conf
cp sentinel.conf /home/sentinelredis/sentinel/sentinel2.conf
cp sentinel.conf /home/sentinelredis/sentinel/sentinel3.conf
6.创建哨兵docker-compose文件
version: '3.7'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
restart: always
ports:
- 26379:26379
command: redis-sentinel /home/sentinelredis/redisconf/sentinel.conf
volumes:
- ./sentinel1.conf:/home/sentinelredis/redisconf/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
restart: always
ports:
- 26380:26379
command: redis-sentinel /home/sentinelredis/redisconf/sentinel.conf
volumes:
- ./sentinel2.conf:/home/sentinelredis/redisconf/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /home/sentinelredis/redisconf/sentinel.conf
volumes:
- ./sentinel3.conf:/home/sentinelredis/redisconf/sentinel.conf
networks:
default:
external:
name: masterslave_default
这里要注意 networks:default:external:name: masterslave_default,这个是要查看自己redis的主节点信息,把Networks复制过来,命令:docker inspect d6a81ae3106e
7.进入对应目录,启动哨兵:
8.查看启动容器:
最终目录图示: