亲宝软件园·资讯

展开

Docker配置redis sentinel哨兵

卷积蛋挞 人气:0

本文演示一主二从。

先说一下遇到的问题。我看网上说想配置哨兵,必须让启动redis的docker网络模式为host,否则无法访问到从还是什么的。我指定--network host后无法外网访问,redis desktop manager连不上redis了,后来发现我这个新克隆的机器没关防火墙,关上就好了。

1.配置主从

docker pull一下redis

master

mkdir -p /mydata/redis/6379/conf
mkdir -p /mydata/redis/6379/data
touch /mydata/redis/6379/conf/redis.conf
echo "appendonly yes"  >> /mydata/redis/6379/conf/redis.conf
docker run --network host --name redis6379 -v /mydata/redis/6379/data:/data -v /mydata/redis/6379/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6379

slave

#slave1
mkdir -p /mydata/redis/6380/conf
mkdir -p /mydata/redis/6380/data
touch /mydata/redis/6380/conf/redis.conf
echo "appendonly yes"  >> /mydata/redis/6380/conf/redis.conf
docker run --network host --name redis6380 -v /mydata/redis/6380/data:/data -v /mydata/redis/6380/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6380 --slaveof 192.168.243.4 6379

#slave2
mkdir -p /mydata/redis/6381/conf
mkdir -p /mydata/redis/6381/data
touch /mydata/redis/6381/conf/redis.conf
echo "appendonly yes"  >> /mydata/redis/6381/conf/redis.conf
docker run --network host --name redis6381 -v /mydata/redis/6381/data:/data -v /mydata/redis/6381/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --port 6381 --slaveof 192.168.243.4 6379

查看配置:

#主
docker exec -it redis6379 redis-cli
>info replication
#从
docker exec -it redis6380 redis-cli -p 6380
>info replication

在这里插入图片描述

2. 配置哨兵

master

mkdir /mydata/redis/6379/sentinel
mkdir /mydata/redis/6379/sentinel/log
vi /mydata/redis/6379/sentinel/sentinel.conf
#输入
port 26379
dir "/var/log/sentinel"
logfile "/var/log/sentinel/26379.log"
sentinel monitor mymaster 192.168.243.4 6379 1 #这里暂时设置成1
                       ![在这里插入图片描述](https://img-blog.csdnimg.cn/9a303b239fec467385d2c8990cb3e629.png#pic_center)
                        #方便看一下主挂掉是什么效果
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
#end
docker run -d --name sentinel26379 -v /mydata/redis/6379/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6379/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf

先进行测试:

docker exec -it redis6379 redis-cli
> SHUTDOWN
> exit
cat /mydata/redis/6379/sentinel/log/26379.log

在这里插入图片描述

此时redis6381已成为新的master

在这里插入图片描述

再启动docker start redis6379 6379是slave了

测试成功,修改上面配置文件的sentinel monitor mymaster 192.168.243.4 6379 2 数字配置为2,代表至少有2个Sentinel节点认为主节点不可达,那么这个不可达的判定才是客观的。为了防止票数相同,sentinel启动奇数个。

slave

#slave1
mkdir /mydata/redis/6380/sentinel
mkdir /mydata/redis/6380/sentinel/log
vi /mydata/redis/6380/sentinel/sentinel.conf
#输入
port 26380
dir "/var/log/sentinel"
logfile "/var/log/sentinel/26380.log"
sentinel monitor mymaster 192.168.243.4 6380 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
#end
docker run -d --name sentinel26380 -v /mydata/redis/6380/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6380/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf

#slave2
mkdir /mydata/redis/6381/sentinel
mkdir /mydata/redis/6381/sentinel/log
vi /mydata/redis/6381/sentinel/sentinel.conf
#输入
port 26381
dir "/var/log/sentinel"
logfile "/var/log/sentinel/26381.log"
sentinel monitor mymaster 192.168.243.4 6381 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
#end
docker run -d --name sentinel26381 -v /mydata/redis/6381/sentinel/sentinel.conf:/conf/sentinel.conf -v /mydata/redis/6381/sentinel/log:/var/log/sentinel --network host redis redis-sentinel /conf/sentinel.conf

在这里插入图片描述

3.SpringBoot连接

application.yml

spring:
  redis:
    timeout: 5000
    sentinel:
      master: mymaster
      nodes: 192.168.243.4:26379,192.168.243.4:26380,192.168.243.4:26381

controller

    @Autowired
	private StringRedisTemplate redisTemplate;

	@RequestMapping("/redis")
    public String redis() {
        redisTemplate.opsForValue().set("test", "121323123");
        String test = redisTemplate.opsForValue().get("test");
        return "RESULT: " + test;
    }

访问localhost:8080/redis

在这里插入图片描述

 到此这篇关于使用Docker配置redis sentinel哨兵的方法步骤的文章就介绍到这了,更多相关Docker配置redis sentinel哨兵内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

加载全部内容

相关教程
猜你喜欢
用户评论