Docker部署系列:搭建Redis哨兵模式(一主两从)

ip

redis节点名称

sentinel节点名称

192.168.5.10

redis-master

redis-sentinel-1

192.168.5.11

redis-slave-1

redis-sentinel-2

192.168.5.12

redis-slave-2

redis-sentinel-3

# 先搭建一主两从
# 3台机器分别创建
mkdir /mydata/redis/data -p
mkdir /mydata/redis/conf -p

# 192.168.5.10机器上
vim /mydata/redis/conf/redis.conf

# 任何都可以连接redis
bind 0.0.0.0
# 配置master密码
requirepass 123456
# 宕机后成为从要连接master的密码
masterauth 123456
# 开启持久化
appendonly yes

# 启动主节点
docker run -p 6379:6379 --name redis-master \
-v /mydata/redis/data:/usr/local/etc/redis/data \
-v /mydata/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-d redis redis-server /usr/local/etc/redis/redis.conf

# 查看日志
docker logs -f redis-master

# 192.168.5.11 机器上
vim /mydata/redis/conf/redis.conf

# 配置master的ip和端口号
replicaof 192.168.5.10 6379
# 任何都可以连接redis
bind 0.0.0.0
# 成为master后的密码
requirepass 123456
# 连接master密码
masterauth 123456
# 开启持久化
appendonly yes

# 启动从节点1
docker run -p 6379:6379 --name redis-slave-1 \
-v /mydata/redis/data:/usr/local/etc/redis/data \
-v /mydata/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-d redis redis-server /usr/local/etc/redis/redis.conf

# 查看日志
docker logs -f redis-slave-1

# 192.168.5.12 机器上
vim /mydata/redis/conf/redis.conf

# 配置master的ip和端口号
replicaof 192.168.5.10 6379
# 任何都可以连接redis
bind 0.0.0.0
# 成为master后的密码
requirepass 123456
# 连接master密码
masterauth 123456
# 开启持久化
appendonly yes

# 启动从节点2
docker run -p 6379:6379 --name redis-slave-2 \
-v /mydata/redis/data:/usr/local/etc/redis/data \
-v /mydata/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-d redis redis-server /usr/local/etc/redis/redis.conf

# 查看日志
docker logs -f redis-slave-2

# 进入容器测试
docker exec -it redis-master /bin/bash
redis-cli
auth 123456
info

# 搭建3个哨兵
# 分别在3台机器上创建并编写配置文件
vim /mydata/redis/conf/sentinel.conf

port 26379
sentinel monitor redis-master 192.168.5.10 6379 2
sentinel auth-pass redis-master 123456
sentinel down-after-milliseconds redis-master 6000
sentinel parallel-syncs redis-master 1
sentinel failover-timeout redis-master 6000


docker run -p 26379:26379 --name redis-sentinel-1 \
-v /mydata/redis/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
-d redis redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run -p 26379:26379 --name redis-sentinel-2 \
-v /mydata/redis/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
-d redis redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run -p 26379:26379 --name redis-sentinel-3 \
-v /mydata/redis/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
-d redis redis-sentinel /usr/local/etc/redis/sentinel.conf

安装好之后,可以把redis-master停掉。在主节点上通过docker logs -f redis-sentinel-1 查看日志,会发现主节点转移。 

你可能感兴趣的:(Docker系列,redis,java,数据库)