文件存放如下图:
docker-compose.yml文件内容如下:
version: '3.3'
services:
master:
image: redis:3.2.12
restart: always
container_name: redis-master
command: redis-server /usr/local/redis/conf/redis.conf
ports:
- 6380:6380
volumes:
- /root/redis/master/data:/data
- /root/redis/master/conf:/usr/local/redis/conf
slave1:
image: redis:3.2.12
restart: always
container_name: redis-slave-1
command: redis-server /usr/local/redis/conf/redis.conf
ports:
- 6381:6381
volumes:
- /root/redis/slave1/data:/data
- /root/redis/slave1/conf:/usr/local/redis/conf
slave2:
image: redis:3.2.12
restart: always
container_name: redis-slave-2
command: redis-server /usr/local/redis/conf/redis.conf
ports:
- 6382:6382
volumes:
- /root/redis/slave2/data:/data
- /root/redis/slave2/conf:/usr/local/redis/conf
sentinel1:
image: redis:3.2.12
container_name: redis-sentinel-1
ports:
- 26380:26380
volumes:
- /root/redis/sentinel1/conf:/usr/local/redis/sentinel1/conf
command: redis-sentinel /usr/local/redis/sentinel1/conf/sentinel.conf
sentinel2:
image: redis:3.2.12
container_name: redis-sentinel-2
ports:
- 26381:26381
volumes:
- /root/redis/sentinel2/conf:/usr/local/redis/sentinel2/conf
command: redis-sentinel /usr/local/redis/sentinel2/conf/sentinel.conf
sentinel3:
image: redis:3.2.12
container_name: redis-sentinel-3
ports:
- 26382:26382
volumes:
- /root/redis/sentinel3/conf:/usr/local/redis/sentinel3/conf
command: redis-sentinel /usr/local/redis/sentinel3/conf/sentinel.conf
redis.conf配置文件修改内容:
# 配置端口根据实际情况更改
1.配置端口:
port 6379
2.修改绑定ip为服务器内网ip地址,做绑定,三台各自填写各自的ip地址
bind 0.0.0.0
2.保护模式修改为否,允许远程连接
protected-mode no
3.设定密码
requirepass "XXXX"
4.设定主库密码与当前库密码同步,保证从库能够提升为主库
masterauth "XXXX"
5.打开AOF持久化支持
appendonly yes
6.守护进程
daemonize no
sentinel.conf做如下修改:
bind 0.0.0.0
# 端口根据实际情况更改
port 26379
# 根据实际修改
dir "/tmp"
# 根据实际情况写主redis服务
sentinel monitor mymaster 192.168.123.182 6380 2
# 3秒连不上,就换主redis服务,生产环境时间可以久点
sentinel down-after-milliseconds mymaster 3000
# redis设置了密码,这个得加上
sentinel auth-pass mymaster 123456p
当停止redis-master容器,主redis服务会转移,重启redis-master后,redis-master变成从redis服务。