redis-sentinel主从切换

上篇我们搭建了redis主从,那么如果redis的master宕掉之后,sentinel可以切换一台slave为master

上篇我们搭建redis主从是一主二从在一台机器上,所以现在需要3个sentinel服务来监控三个redis节点

主:127.0.0.1:6379

从:127.0.0.1:6380,127.0.0.1:6381

sentinel:127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
开始配置:

修改sentinel的配置文件(sentinel的配置文件在redis的包中,是sentinel.conf):只修改以下几项

redis_6379的sentinel.conf:

    port 26379
    daemonize yes
    dir "/export/work/redis_6379/data"
    logfile "/export/work/redis_6379/log/26379.log"

    sentinel monitor mymaster 127.0.0.1 6379 2

redis_6380的sentinel.conf:

     port 26380
    daemonize yes
    dir "/export/work/redis_6380/data"
    logfile "/export/work/redis_6380/log/26380.log"

    sentinel monitor mymaster 127.0.0.1 6379 2

redis_6381的sentinel.conf:

     port 26381
    daemonize yes
    dir "/export/work/redis_6381/data"
    logfile "/export/work/redis_6380/log/26381.log"

    sentinel monitor mymaster 127.0.0.1 6379 2

修改好配置文件就可以启动sentinel服务了:

[root@localhost redis_6379]# bin/redis-sentinel conf/sentinel.conf

[root@localhost redis_6380]# bin/redis-sentinel conf/sentinel.conf

[root@localhost redis_6381]# bin/redis-sentinel conf/sentinel.conf

启动之后,sentinel的配置文件sentinel.conf会多下面这些内容:

redis_6379的sentinel.conf:

    sentinel known-slave mymaster 127.0.0.1 6381

    sentinel known-slave mymaster 127.0.0.1 6380
    sentinel known-sentinel mymaster 127.0.0.1 26381 e35d72a566891cbbf419f3189ab5f17a9176b661

    sentinel known-sentinel mymaster 127.0.0.1 26380 425a2cdaa7c402a25a3a0845a2009db7d8875704

redis_6380的sentinel.conf:

    sentinel known-slave mymaster 127.0.0.1 6381
    sentinel known-slave mymaster 127.0.0.1 6380
    sentinel known-sentinel mymaster 127.0.0.1 26379 1917ab844524ccb067b97776b6e13f4a5115bb0d
    sentinel known-sentinel mymaster 127.0.0.1 26381 e35d72a566891cbbf419f3189ab5f17a9176b661

redis_6381的sentinel.conf:

    sentinel known-slave mymaster 127.0.0.1 6380
    sentinel known-slave mymaster 127.0.0.1 6381
    sentinel known-sentinel mymaster 127.0.0.1 26379 1917ab844524ccb067b97776b6e13f4a5115bb0d

    sentinel known-sentinel mymaster 127.0.0.1 26380 425a2cdaa7c402a25a3a0845a2009db7d8875704

接下来我们测试主从切换:

先看下redis的进程:

redis-sentinel主从切换_第1张图片

杀掉现在的redis的master6379:

    [root@localhost redis_6379]# kill -9 838

检查:

redis-sentinel主从切换_第2张图片

master此时就切换到了6380,同学们还可以去看配置文件,也有变化

这样就算6379重新启动了,他也是slave

你可能感兴趣的:(redis)