1.从docker仓库拉取redis镜像
docker pull redis
2.在/home下分别创建redis-6379-data,redis-6380-data,redis-6381-data
3.拷贝/etc/redis/redis.conf 到 /home下
4.复制redis.conf为redis-6379.conf, redis-6380.conf,redis.6381.con并且分别修改其中的配置, logfile指定不同的文件
port 6380 logfile "redis-6380.log" dir /data appendonly yes appendfilename appendonly.aof slaveof 127.0.0.1 6379
5.docker启动3个redis
docker run -tdi -v /home/data:/data /home/redis-6379.conf:/usr/local/ect/redis/redis.conf -p 6379:6379 redis
docker run -tdi -v /home/data:/data /home/redis-6380.conf:/usr/local/ect/redis/redis.conf -p 6380:6380 redis
docker run -tdi -v /home/data:/data /home/redis-6381.conf:/usr/local/ect/redis/redis.conf -p 6381:6381 redis
6.通过redis-cli分别连入创建好的redis服务器,测试主从是否配好
7.配置redis-sentinelm,创建三个sentinel文件,内容如下
port 26379 dir "/data" logfile "sentinel-26379.log" sentinel monitor mymaster 127.0.0.1 6379 2 sentinel down-after-milliseconds mymaster 10000 sentinel failover-timeout mymaster 60000
只修改日志的文件名即可
8.通过docker启动三个sentinel
docker run -dit -v /home/sentinel-26379.conf:/usr/local/etc/redis/sentinel.conf -p 26379:26379 redis redis-sentinel /usr/local/etc/redis/sentinel.conf
docker run -dit -v /home/sentinel-26380.conf:/usr/local/etc/redis/sentinel.conf -p 26380:26380 redis redis-sentinel /usr/local/etc/redis/sentinel.conf
docker run -dit -v /home/sentinel-26381.conf:/usr/local/etc/redis/sentinel.conf -p 26381:26381 redis redis-sentinel /usr/local/etc/redis/sentinel.conf
9.停止redis的master节点
docker stop master节点的containerid
10.连入从节点,查看主从状态
redis-cli -p 6380 info replication
11.稍等几十秒,从节点就会自动变成主节点
哨兵模式的原理采用了心跳机制和投票裁决机制。