文章目录
- 一、前言
- 二、docker-compose部署redis一主二从三哨兵模式
- docker-compose-redis-master-slave-sentinel.yml
- redis.conf
- redis-sentinel.conf
- 三、测试
- 四、其它
- 解决情况1:端口映射问题
- 解决情况2:redis由于安全问题禁用命令CONFIG - 防止客户端修改Redis配置
- 解决情况3:redis主节点下线后再重启变成从节点时,需配置认证主节点
一、前言
运维(23) docker-compose部署redis主从复制模式
本文将基于以下环境部署redis一主二从三哨兵模式
- CentOS Linux release 7.6.1810 (Core)
- Docker version 20.10.5, build 55c4c88
- docker-compose version 1.28.6, build 5db8d86f
tips: 本文仅基于单台服务器环境部署
二、docker-compose部署redis一主二从三哨兵模式
可参考 https://gitee.com/zhengqingya/docker-compose
git clone https://gitee.com/zhengqingya/docker-compose.git
cd docker-compose/Liunx
chmod -R 777 ./redis-master-slave-sentinel
docker-compose -f docker-compose-redis-master-slave-sentinel.yml -p redis up -d
docker exec -it redis-sentinel-1 redis-cli -p 26379 -a 123456
sentinel master mymaster
sentinel slaves mymaster
下面贴出相关配置文件
docker-compose-redis-master-slave-sentinel.yml
version: '3'
networks:redis:services:redis-master:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 container_name: redis-master restart: unless-stopped command: redis-server /etc/redis/redis.conf --port 6380 --requirepass 123456 --masterauth 123456 --appendonly no environment: TZ: Asia/ShanghaiLANG: en_US.UTF-8volumes:
- "./redis-master-slave-sentinel/redis/master/config/redis.conf:/etc/redis/redis.conf" ports: - "6380:6380"networks:- redisredis-slave-1:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 container_name: redis-slave-1 restart: unless-stopped command: redis-server /etc/redis/redis.conf --port 6381 --requirepass 123456 --appendonly no --slaveof redis-master 6380 --masterauth 123456 environment: TZ: Asia/ShanghaiLANG: en_US.UTF-8volumes:
- "./redis-master-slave-sentinel/redis/slave-1/config/redis.conf:/etc/redis/redis.conf" ports: - "6381:6381"networks:- redisdepends_on:- redis-masterlinks:- redis-masterredis-slave-2:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 container_name: redis-slave-2 restart: unless-stopped command: redis-server /etc/redis/redis.conf --port 6382 --requirepass 123456 --appendonly no --slaveof redis-master 6380 --masterauth 123456 environment: TZ: Asia/ShanghaiLANG: en_US.UTF-8volumes:
- "./redis-master-slave-sentinel/redis/slave-2/config/redis.conf:/etc/redis/redis.conf" ports: - "6382:6382"networks:- redisdepends_on:- redis-masterlinks:- redis-masterredis-sentinel-1:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 container_name: redis-sentinel-1 restart: unless-stopped command: redis-sentinel /etc/redis/sentinel.confenvironment: TZ: Asia/ShanghaiLANG: en_US.UTF-8ports:- "26379:26379"volumes:- "./redis-master-slave-sentinel/sentinel/redis-sentinel-1.conf:/etc/redis/sentinel.conf" networks:- redisdepends_on:- redis-master- redis-slave-1- redis-slave-2links:- redis-master- redis-slave-1- redis-slave-2redis-sentinel-2:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 container_name: redis-sentinel-2 restart: unless-stopped command: redis-sentinel /etc/redis/sentinel.confenvironment: TZ: Asia/ShanghaiLANG: en_US.UTF-8ports:- "26380:26380"volumes:- "./redis-master-slave-sentinel/sentinel/redis-sentinel-2.conf:/etc/redis/sentinel.conf" networks:- redisdepends_on:- redis-master- redis-slave-1
- redis-sentinel-1links:- redis-master- redis-slave-1- redis-slave-2redis-sentinel-3:image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8 container_name: redis-sentinel-3 restart: unless-stopped command: redis-sentinel /etc/redis/sentinel.confenvironment: TZ: Asia/ShanghaiLANG: en_US.UTF-8ports:- "26381:26381"volumes:- "./redis-master-slave-sentinel/sentinel/redis-sentinel-3.conf:/etc/redis/sentinel.conf" networks:- redisdepends_on:- redis-master- redis-slave-1- redis-slave-2- redis-sentinel-1- redis-sentinel-2links:- redis-master- redis-slave-1- redis-slave-2
redis.conf
参考 http://download.redis.io/redis-stable/redis.conf
redis-sentinel.conf
参考 http://download.redis.io/redis-stable/sentinel.conf
port 26379
dir /tmp
sentinel monitor mymaster redis-master 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster 123456
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
运行后如下
redis-sentinel.conf配置文件会自动加载到redis从节点信息
三、测试
- 主写从读
- 主节点下线后,sentinel自动将其它从节点升级为主节点且具备写能力
- 将之前下线的主节点重启上线时变成从节点
这个就自己测试着玩吧^_^
sentinel切换redis主从节点日志如下
四、其它
小编在部署的时候遇到sentinel无法自动切换主从的问题
解决情况1:端口映射问题
在同一机器上部署时端口最好一一对应
解决情况2:redis由于安全问题禁用命令CONFIG - 防止客户端修改Redis配置
在redis.conf
中关闭其配置
解决情况3:redis主节点下线后再重启变成从节点时,需配置认证主节点
今日分享语句:
不要轻易用过去来衡量生活的幸与不幸!每个人的生命都是可以绽放美丽~ 只要你懂得珍惜。