在workspace中创建文件和目录
- 在workspace中创建
redis-sentinel 文件夹 - 在
redis-sentinel 中创建sentinel 文件夹 - 在sentinel创建
sentinel.conf`` 、 Dockerfile、 sentinel-entrypoint.sh```三个文件 - 回到redis-sentinel目录创建
docker-compose.yml 文件
整个文件结构如图:
sentinel.conf
# sentinel monitor {masterName} {masterIp} {masterPort} {quorum} sentinel monitor masterName为哨兵masterId 指定了主节点IP和端口
sentinel monitor mymaster redis-master 6379 2
# 当哨兵发出ping命令 超过5秒则判断为主观下线
sentinel down-after-milliseconds mymaster 5000
# 每次故障转以后,从节点向新的主节点发起复制操作的节点个数,可以减轻Master的压力
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000
Dockerfile
# 建立Dockerfile指定基础镜像,同时拷贝配置文件到镜像内部
FROM redis
EXPOSE 26379
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
COPY sentinel-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
ENTRYPOINT ["sentinel-entrypoint.sh"]
sentinel-entrypoint.sh
#!/bin/sh
sed -i "s/$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf
sed -i "s/$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf
sed -i "s/$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf
exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel
docker-compose.yml
version: '3.5'
networks:
app-tier:
driver: bridge
services:
redis:
image: 'bitnami/redis:latest'
restart: always
environment:
- REDIS_REPLICATION_MODE=master
- REDIS_PASSWORD=""
networks:
- app-tier
ports:
- '6379:6379'
redis-slave:
image: 'bitnami/redis:latest'
restart: always
environment:
- REDIS_REPLICATION_MODE=slave
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PASSWORD=""
- REDIS_PASSWORD=""
ports:
- '6380:6379'
depends_on:
- redis
networks:
- app-tier
redis-slave2:
image: 'bitnami/redis:latest'
restart: always
environment:
- REDIS_REPLICATION_MODE=slave
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PASSWORD=""
- REDIS_PASSWORD=""
ports:
- '6381:6379'
depends_on:
- redis
networks:
- app-tier
redis-sentinel:
image: 'bitnami/redis-sentinel:latest'
restart: always
environment:
- REDIS_MASTER_HOST=127.0.0.1
- REDIS_MASTER_PASSWORD=""
depends_on:
- redis
- redis-slave
- redis-slave2
ports:
- '26379-26381:26379'
networks:
- app-tier
创建容器
创建好文件结构后回到redis-sentinel 目录,执行一下语句即可:
docker-compose up --scale redis-sentinel=1
他会搭建起一个一主2从的环境:
|