redis 学习笔记 Redis哨兵(sentinel)

先配置主从复制环境(配置6379、6380、6381)

redis6379.conf 配置    port 6379  requirepass 12345678  masterauth 12345678

redis6380.conf 配置    port 6380       slaveof 127.0.0.1 6379     requirepass 12345678  masterauth 12345678

redis6381.conf 配置    port 6381       slaveof 127.0.0.1 6379     requirepass 12345678  masterauth 12345678


redis sentinel哨兵机制配置(也是3个节点):
   /usr/local/bin/conf/sentinel_26379.conf  
   /usr/local/bin/conf/sentinel_26380.conf
   /usr/local/bin/conf/sentinel_26381.conf
将三个文件的端口改成: 26379   26380   26381
sentinel monitor mymaster 192.168.1.111 6379 2  //监听主节点6379
sentinel auth-pass mymaster 12345678     //连接主节点时的密码
三个配置除端口外,其它一样
配完此脚本,哨兵机制可正常启动运行。

sentinel monitor mymaster 192.168.1.111 6379 2  //监控主节点的IP地址端口
sentinel auth-pass mymaster 12345678  //sentinel连主节点的密码
sentinel config-epoch mymaster 2      //执行故障转移时, 最多可以有多少个从节点同时对新的主节点进行数据同步
sentinel leader-epoch mymaster 2
sentinel failover-timeout mymaster 180000 //故障转移超时时间180s,                            
    a,如果转移超时失败,下次转移时时间为之前的2倍;
    b,从节点变主节点时,从节点执行slaveof no one命令一直失败的话,当时间超过180S时,则故障转移失败
    c,从节点复制新主节点时间超过180S转移失败
sentinel down-after-milliseconds mymaster 300000//sentinel节点定期向主节点ping命令

  启动sentinel服务:
            ./redis-sentinel conf/sentinel_26379.conf &
            ./redis-sentinel conf/sentinel_26380.conf &
            ./redis-sentinel conf/sentinel_26381.conf &

测试:kill -9 6379  杀掉6379的redis服务
看日志是分配6380 还是6381做为主节点,当6379服务再启动时,已变成从节点


假设6380升级为主节点:
进入6380>info replication     
         role:master
打开sentinel_26379.conf等三个配置,
     sentinel monitor mymaster 192.168.1.111 6380 2 //有2个sentinel认为master下线
打开redis6379.conf等三个配置, slaveof 127.0.0.1 6380,也变成了6380
注意:生产环境建议让redis Sentinel部署到不同的物理机上。


坑点:sentinel monitor mymaster 192.168.1.111 6379 2 
     //切记将IP不要写成127.0.0.1
不然使用JedisSentinelPool取jedis连接的时候会变成取127.0.0.1 6379的错误地址






猜你喜欢

转载自blog.csdn.net/wenaibing/article/details/79229253