前期准备docker环境
dooker pull redis:5.0.12
dooker pull redis
一:redis 主从复制搭建:
1 .1 创建文件redis.conf:
master 节点:
redis.conf 文件:
appendonly yes
appendfilename "aa.aof"
bind 0.0.0.0
1 .2 执行docker
docker run --name redis -d -p 6379:6379 --restart=always -v /root/redisconf:/data redis:5.0.12 redis-server /data/redis.conf
2.1 创建文件redis.conf:
master 节点:
redis.conf 文件:
appendonly yes
appendfilename "aa.aof"
bind 0.0.0.0
port 6380
2 .2 执行docker
docker run --name redissalve1 -d -p 6380:6380 --restart=always -v /root/salve1/redisconf:/data redis:5.0.12 redis-server /data/redis.conf
3.1 创建文件redis.conf:
master 节点:
redis.conf 文件:
appendonly yes
appendfilename "aa.aof"
bind 0.0.0.0
port 6381
3 .2 执行docker
docker run --name redissalve2 -d -p 6381:6381 --restart=always -v /root/salve2/redisconf:/data redis:5.0.12 redis-server /data/redis.conf
注:以上为redis 主从复制 搭建完成
二 : docker 启动redis 哨兵:
前期准备sentinel.conf 文件
wget http://download.redis.io/redis-stable/sentinel.conf
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf
1 .1 创建文件sentinel1.conf:
sentinel1.conf 文件:
# 让sentinel服务后台运行(docker的话需要设置为no,非docker运行设置为yes, 因为docker有个-d属性就是让在后台运行的)
daemonize no
# 修改日志文件的路径
logfile "/var/tmp/sentinel.log"
# 修改监控的主redis服务器
# 最后一个2表示,两台机器判定主被动下线后,就进行failover(故障转移)
sentinel monitor mymaster 公网ip 6390 2
#超过5秒master还没有连接上,则认为master已经停止
sentinel down-after-milliseconds mymaster 5000
1 .2 执行docker
docker run --name sentinel1 -d -p 26379:26379 --restart=always -v /root/redisconf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf redis redis-sentinel /usr/local/etc/redis/sentinel.conf
2 .1 创建文件sentinel1.conf:
sentinel2.conf 文件:
# 让sentinel服务后台运行(docker的话需要设置为no,非docker运行设置为yes, 因为docker有个-d属性就是让在后台运行的)
daemonize no
# 修改日志文件的路径
logfile "/var/tmp/sentinel.log"
# 修改监控的主redis服务器
# 最后一个2表示,两台机器判定主被动下线后,就进行failover(故障转移)
sentinel monitor mymaster 公网ip 6390 2
#超过5秒master还没有连接上,则认为master已经停止
sentinel down-after-milliseconds mymaster 5000
2 .2 执行docker
docker run --name sentinel2 -d -p 26380:26380 --restart=always -v /root/redisconf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf redis redis-sentinel /usr/local/etc/redis/sentinel.conf
3 .1 创建文件sentinel1.conf:
sentinel2.conf 文件:
# 让sentinel服务后台运行(docker的话需要设置为no,非docker运行设置为yes, 因为docker有个-d属性就是让在后台运行的)
daemonize no
# 修改日志文件的路径
logfile "/var/tmp/sentinel.log"
# 修改监控的主redis服务器
# 最后一个2表示,两台机器判定主被动下线后,就进行failover(故障转移)
sentinel monitor mymaster 公网ip 6390 2
#超过5秒master还没有连接上,则认为master已经停止
sentinel down-after-milliseconds mymaster 5000
3.2 执行docker
docker run --name sentinel3 -d -p 26381:26381 --restart=always -v /root/redisconf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf redis redis-sentinel /usr/local/etc/redis/sentinel.conf
三:验证
启动后通过docker ps查看
通过redis-cli –p 26379(注意我是本机安装了redis的,如果本机没有安装,需要进入docker容器,docker exec –it sentinel1 bash)进入哨兵实例,查看哨兵监控状态
可以看到哨兵的状态是正确的,发现了1个主节点,2个从节点,并且哨兵一共有3个。
模拟主节点崩溃后,其中一个从节点变为主节点,并且另外一个从节点以新主节点做备份。(从节点通过客户端连接后,只能查看,不能写入数据)
docker stop redis
停止主节点后,需要等待5-10秒再查看redissalve1和redissalve2
redis-cli –p 6380
可以看到redissalve1变为了主节点
以上是redis 哨兵模式搭建,为redis 高可用解决方案。