一、下载redis镜像
二、redis主库配置 redis.conf
绑定的IP地址和端口
bind 0.0.0.0
必须使用6379,因为容器内默认是6379端口
port 6379
设置密码
requirepass 123456
启用持久化
appendonly yes
三、主库sentinel配置sentinel.conf
protected-mode no
配置端口号,各个节点不能相同
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
(sentinel的暴露端口)
sentinel announce-ip 主机IP
(sentinel的暴露端口)
sentinel announce-port 6380
设置Redis主节点信息
xxx.IP redis主库IP 主库端口(6370)
sentinel monitor mymaster xxx.IP 6370 2
(主库密码)
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
四、docker-compose.yml配置
services:
redis-master:
image: redis:7.2
container_name: redis-master
restart: always
networks:
- redis-net
ports:
- 6370:6379
environment:
TZ: "Asia/Shanghai"
REDIS_PASSWORD: 123456
volumes:
- /container/mnt/redis/master/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /container/mnt/redis/master/data:/data
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
logging:
driver: "json-file"
options:
max-size: "1024m"
max-file: "5"
redis-sentinel:
image: redis:7.2
container_name: redis-sentinel
command: redis-sentinel /etc/sentinel.conf
volumes:
- /container/mnt/redis/sentinel/conf/sentinel.conf:/etc/sentinel.conf
ports:
- 6380:26379
networks:
- redis-net
logging:
driver: "json-file"
options:
max-size: "1024m"
max-file: "5"
=========== 从库可以废弃
redis-slave:
image: redis:7.2
container_name: redis-slave
restart: always
networks:
- redis-net
ports:
- 6371:6379
environment:
TZ: "Asia/Shanghai"
REDIS_PASSWORD: 123456
volumes:
- /container/mnt/redis/slave/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /container/mnt/redis/slave/data:/data
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
logging:
driver: "json-file"
options:
max-size: "1024m"
max-file: "5"
networks:
redis-net:
driver: bridge
五、从节点slave1的redis.conf配置
绑定的IP地址和端口
bind 0.0.0.0
必须使用6379,因为容器内默认是6379端口
port 6379
设置密码
requirepass 123456
masterauth 123456
启用持久化
appendonly yes
xxx.IP是redis主库的的IP地址 ,6370是主库的端口
slaveof xxx.IP 6370
六、从库的sentinel配置sentinel.conf
protected-mode no
配置端口号,各个节点不能相同
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
从库 sentinel对外暴露的IP
sentinel announce-ip xxx.IP
从库 sentinel对外暴露的端口
sentinel announce-port 24080
设置Redis主节点信息, xxx.IP是redis主库的IP地址
sentinel monitor mymaster xxx.IP 6370 2
123456是主库的登录密码
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
七、从库的docker-compose.yml配置
services:
redis-slave-2:
image: redis:7.2
container_name: redis-slave-2
restart: always
networks:
- redis-net
ports:
- 24050:6379
environment:
TZ: "Asia/Shanghai"
REDIS_PASSWORD: 123455
volumes:
- /container/mnt/redis/slave/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /container/mnt/redis/slave/data:/data
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
logging:
driver: "json-file"
options:
max-size: "1024m"
max-file: "5"
redis-sentinel:
image: redis:7.2
container_name: redis-sentinel
command: redis-sentinel /etc/sentinel.conf
volumes:
/container/mnt/redis/sentinel/conf/sentinel.conf
- /container/mnt/redis/sentinel/conf/sentinel.conf:/etc/sentinel.conf
ports:
- 24080:26379
networks:
- redis-net
logging:
driver: "json-file"
options:
max-size: "1024m"
max-file: "5"
networks:
redis-net:
driver: bridge
八、docker compose启动所有redis容器
docker compose up -d