1、创建映射需要的目录
mkdir -p /docker/redis/{sentinel,data}
2、权限设置
chmod -R 777 /docker/redis/
3、创建网络环境
docker network create --driver=bridge --subnet=172.21.0.0/16 redisnetwork
4、redis 的docker-compose.yml 一主两从
cat > /docker/redis/docker-compose.yml <<END
version: '3.3'
services:
master:
image: redis
container_name: redis-master
command: redis-server --requirepass admin@admin
ports:
- 6379:6379
restart: always
networks:
- redisnetwork
volumes:
- /docker/redis/data:/data
slave1:
image: redis
container_name: redis-slave-1
ports:
- 6380:6379
command: redis-server --slaveof redis-master 6379 --port 6380 --requirepass admin@admin --masterauth admin@admin
restart: always
networks:
- redisnetwork
volumes:
- /docker/redis/data:/data
#command: redis-server --slaveof redis-master 6379
slave2:
image: redis
container_name: redis-slave-2
ports:
- 6381:6379
command: redis-server --slaveof redis-master 6379 --port 6381 --requirepass admin@admin --masterauth admin@admin
restart: always
networks:
- redisnetwork
volumes:
- /docker/redis/data:/data
#command: redis-server --slaveof redis-master 6379
networks:
redisnetwork:
external: true
END
5、编写 redis-sentinel docker-compose.yml
cat > /docker/redis/sentinel/docker-compose.yml <<END
version: '3.3'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
networks:
- redisnetwork
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /docker/redis/sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
networks:
- redisnetwork
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /docker/redis/sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
networks:
- redisnetwork
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /docker/redis/sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
redisnetwork:
external: true
END
6、编写配置文件
cat > /docker/redis/sentinel/sentinel.conf <<END
port 26379
dir /tmp
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster admin@admin
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
END
7、复制配置文件为三份
cp /docker/redis/sentinel/sentinel.conf /docker/redis/sentinel/sentinel1.conf
cp /docker/redis/sentinel/sentinel.conf /docker/redis/sentinel/sentinel2.conf
cp /docker/redis/sentinel/sentinel.conf /docker/redis/sentinel/sentinel3.conf
7、启动redis
cd /docker/redis/
docker-compose up -d
8、启动sentinel
cd ./sentinel/
docker-compose up -d
9、查看主节点和从节点
docker exec -it redis-sentinel-3 bash 进入哨兵节点
redis-cli -p 26379 查看是否有主节点
sentinel master mymaster
10、测试主从
docker exec -it redis-master bash #进入redis 主节点
redis-cli #使用redis命令行
auth admin@admin #授权
set testkey testvalue #设置key value 分别为testkey,testvalue
get testkey #获取testkey 可以获取到testvalue
exit #退出命令行
exit #退出 容器
docker exec -it redis-slave-1 bash 进入从节点
redis-cli -p 6380 #进入从节点命令行,记得加端口
auth admin@admin #授权
get testkey #获取testkey 可以获取到testvalue 证明主从没问题
11、测试哨兵
docker exec -it redis-sentinel-1 bash #进入哨兵容器
redis-cli -p 26379 #使用命令行
sentinel master mymaster #查看主信息如下为正常
sentinel slaves mymaster #查看从信息如下为正常
11、测试主库挂了之后,哨兵是否正常选举
docker stop redis-master #停掉主库
docker ps -a #加上-a查看容器可以看到下面的挂掉了
docker logs redis-sentinel-1 查看哨兵日志,发现重新选择了主库
一主两从三哨兵配置完成