Redis哨兵模式

2019-11-15  本文已影响0人  快点给我想个名
主从节点问题
  1. 主节点出现故障,需手动将某从节点晋升为主节点,同时需要修改应用方的主节点地址,还需要命令其他从节点去复制新的主节点,整个过程都需要人工干预
  2. 主节点的写能力受到单机的限制
  3. 主节点的存储能力受到单机的限制
    由此,诞生出了哨兵模式
哨兵模式(Sentinel)

以一主两从为例:

  1. 主节点出现故障,此时两个从节点与主节点失去连接,主从复制失败
  2. Sentinel节点通过定期监控发现主节点出现了故障
  3. 多个Sentinel节点对主节点的故障达成一致,选举出某从节点作为领导者负责故障转移
哨兵模式搭建
  1. 搭建redis主从模式,我们这里搭建完毕后
port 6379 
daemonize yes 
logfile "/usr/local/redis-4.0.14/log/redis.log"
port 6379 
daemonize yes 
logfile "/usr/local/redis-4.0.14/log/redis.log"
slaveof 192.168.6.130 6379

其他配置信息都为默认值,然后启动redis服务。

  1. 搭建哨兵模式
port 26379
daemonize yes
logfile "/usr/local/redis-4.0.14/log/sentinel.log"
sentinel monitor mymaster 192.168.6.130 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
  1. 启动哨兵模式
src/redis-sentinel sentinel-26379.conf
src/redis-server sentinel-26379.conf --sentinel

这里注意根据自身实际情况指定对应的命令路径和脚本路径。同时Sentinel节点线上需要部署在其他不同的服务器。启动完成后,查看一下启动日志,以128服务器为例:

[root@simon log]# tail -f sentinel.log 
7145:X 15 Nov 11:34:49.997 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7145:X 15 Nov 11:34:49.997 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=7145, just started
7145:X 15 Nov 11:34:49.997 # Configuration loaded
7146:X 15 Nov 11:34:50.048 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7146:X 15 Nov 11:34:50.077 * Running mode=sentinel, port=26379.
7146:X 15 Nov 11:34:50.077 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7146:X 15 Nov 11:34:50.077 # Sentinel ID is 8e328e16d867ba5ef31e869b8d54c9c39b889777
7146:X 15 Nov 11:34:50.077 # +monitor master mymaster 192.168.6.130 6379 quorum 2
配置讲解
主节点故障模拟
  1. 关闭主节点模拟宕机
kill -9 7056
  1. 观察Sentinel日志
7245:X 15 Nov 11:49:00.926 # +sdown master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:00.989 # +odown master mymaster 192.168.6.130 6379 #quorum 2/2
7245:X 15 Nov 11:49:00.989 # +new-epoch 2
7245:X 15 Nov 11:49:00.989 # +try-failover master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:00.990 # +vote-for-leader ea797207e975696b1e03ca6c62dd1ac54b80e99a 2
7245:X 15 Nov 11:49:00.992 # 8e328e16d867ba5ef31e869b8d54c9c39b889777 voted for ea797207e975696b1e03ca6c62dd1ac54b80e99a 2
7245:X 15 Nov 11:49:01.063 # +elected-leader master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.063 # +failover-state-select-slave master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.163 # +selected-slave slave 192.168.6.128:6379 192.168.6.128 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.164 * +failover-state-send-slaveof-noone slave 192.168.6.128:6379 192.168.6.128 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.265 * +failover-state-wait-promotion slave 192.168.6.128:6379 192.168.6.128 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.340 # 48fcc2767a272993c7bfda8f4d5b02bf467b11c2 voted for ea797207e975696b1e03ca6c62dd1ac54b80e99a 2
7245:X 15 Nov 11:49:01.346 # +promoted-slave slave 192.168.6.128:6379 192.168.6.128 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.347 # +failover-state-reconf-slaves master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:01.390 * +slave-reconf-sent slave 192.168.6.129:6379 192.168.6.129 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:02.410 * +slave-reconf-inprog slave 192.168.6.129:6379 192.168.6.129 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:02.410 * +slave-reconf-done slave 192.168.6.129:6379 192.168.6.129 6379 @ mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:02.465 # -odown master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:02.466 # +failover-end master mymaster 192.168.6.130 6379
7245:X 15 Nov 11:49:02.466 # +switch-master mymaster 192.168.6.130 6379 192.168.6.128 6379
7245:X 15 Nov 11:49:02.466 * +slave slave 192.168.6.129:6379 192.168.6.129 6379 @ mymaster 192.168.6.128 6379
7245:X 15 Nov 11:49:02.466 * +slave slave 192.168.6.130:6379 192.168.6.130 6379 @ mymaster 192.168.6.128 6379
7245:X 15 Nov 11:49:32.481 # +sdown slave 192.168.6.130:6379 192.168.6.130 6379 @ mymaster 192.168.6.128 6379
7146:X 15 Nov 11:49:01.291 # +sdown master mymaster 192.168.6.130 6379
7146:X 15 Nov 11:49:01.396 # +new-epoch 2
7146:X 15 Nov 11:49:01.396 # +vote-for-leader ea797207e975696b1e03ca6c62dd1ac54b80e99a 2
7146:X 15 Nov 11:49:01.798 # +config-update-from sentinel ea797207e975696b1e03ca6c62dd1ac54b80e99a 192.168.6.130 26379 @ mymaster 192.168.6.130 6379
7146:X 15 Nov 11:49:01.798 # +switch-master mymaster 192.168.6.130 6379 192.168.6.128 6379
7146:X 15 Nov 11:49:01.799 * +slave slave 192.168.6.129:6379 192.168.6.129 6379 @ mymaster 192.168.6.128 6379
7146:X 15 Nov 11:49:01.799 * +slave slave 192.168.6.130:6379 192.168.6.130 6379 @ mymaster 192.168.6.128 6379
7146:X 15 Nov 11:49:31.837 # +sdown slave 192.168.6.130:6379 192.168.6.130 6379 @ mymaster 192.168.6.128 6379
7091:X 15 Nov 11:49:01.082 # +sdown master mymaster 192.168.6.130 6379
7091:X 15 Nov 11:49:01.452 # +new-epoch 2
7091:X 15 Nov 11:49:01.455 # +vote-for-leader ea797207e975696b1e03ca6c62dd1ac54b80e99a 2
7091:X 15 Nov 11:49:01.455 # +odown master mymaster 192.168.6.130 6379 #quorum 3/2
7091:X 15 Nov 11:49:01.455 # Next failover delay: I will not start a failover before Fri Nov 15 11:55:01 2019
7091:X 15 Nov 11:49:01.518 # +config-update-from sentinel ea797207e975696b1e03ca6c62dd1ac54b80e99a 192.168.6.130 26379 @ mymaster 192.168.6.130 6379
7091:X 15 Nov 11:49:01.518 # +switch-master mymaster 192.168.6.130 6379 192.168.6.128 6379
7091:X 15 Nov 11:49:01.519 * +slave slave 192.168.6.129:6379 192.168.6.129 6379 @ mymaster 192.168.6.128 6379
7091:X 15 Nov 11:49:01.519 * +slave slave 192.168.6.130:6379 192.168.6.130 6379 @ mymaster 192.168.6.128 6379
7091:X 15 Nov 11:49:31.523 # +sdown slave 192.168.6.130:6379 192.168.6.130 6379 @ mymaster 192.168.6.128 6379

最终成功将128选择主节点。

  1. 重启130(刚刚被宕掉的主节点)节点
    该节点会被当作slave节点,然后同步当前的master节点数据
java客户端

示例代码如下:

public class SentinelTest {
    
    public static void main(String[] args) {
        JedisPoolConfig config = new JedisPoolConfig();
        JedisSentinelPool jedisSentinelPool = null;
        Jedis jedis = null;
        try {
            Set<String> set = new HashSet<>();
            set.add("192.168.6.129:26379");
            set.add("192.168.6.130:26379");
            jedisSentinelPool = new JedisSentinelPool("mymaster",set,config);

            jedis = jedisSentinelPool.getResource();

            for(int i = 1;i < 6;i++){
                jedis.del("master:"+i);
            }

            jedis.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(jedis != null){
                jedis.close();
            }
            if(jedisSentinelPool != null){
                jedisSentinelPool.close();
            }
        }
    }
}
哨兵模式实现原理

内容来源:《Redis开发与运维》

上一篇下一篇

猜你喜欢

热点阅读