51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Redis集群-哨兵模式搭建(1主2从3哨兵节点)

主机规划

| 类型 | IP地址 | 端口号 | |-----|----------------|-------| | 主 | 192.168.77.145 | 6379 | | 从1 | 192.168.77.146 | 6379 | | 从2 | 192.168.77.147 | 6379 | | 哨兵1 | 192.168.77.145 | 26379 | | 哨兵2 | 192.168.77.146 | 26379 | | 哨兵3 | 192.168.77.147 | 26379 |

1主2从搭建

见主从复制搭建,Redis集群-主从复制搭建

哨兵配置

哨兵配置每个节点执行。

从安装包中,复制哨兵配置文件到redis配置中,作为哨兵节点的配置文件

 cp /opt/redis-7.0.4/sentinel.conf /opt/redis/conf/

编辑哨兵配置文件修改内容如下:

port 26379
 #设置为后台启动
 daemonize yes
 pidfile "/opt/redis/tmp/redis-sentinel.pid"
 logfile "/opt/redis/logs/redis-sentinel.log"
 dir "/opt/redis"
 #哨兵sentinel监控的redis主节点的 ip port 
 sentinel monitor mymaster 192.168.77.145 6379 2
 #当在Redis实例中开启了requirepass,所有连接Redis实例的客户端都要提供密码。需要注意的是,master实例也需要配置masterauth,否则master实例下线重新上线后,会无法加入到集群中
 sentinel auth-pass mymaster 123456

启动哨兵服务

 /opt/redis/bin/redis-sentinel /opt/redis/conf/sentinel.conf

哨兵配置查看

[root@dba Wed Apr 19 09:38 ~]# redis-cli -p 26379 info Sentinel
 # Sentinel
 sentinel_masters:1
 sentinel_tilt:0
 sentinel_tilt_since_seconds:-1
 sentinel_running_scripts:0
 sentinel_scripts_queue_length:0
 sentinel_simulate_failure_flags:0
 master0:name=mymaster,status=ok,address=192.168.77.145:6379,slaves=2,sentinels=3

模拟故 障转移

关闭主节点192.168.77.145:6379,间隔一会后再次查看可以看到master节点变为了192.168.77.147:6379

[root@dba Wed Apr 19 09:40 ~]# redis-cli -a 123456 shutdown
 [root@dba Wed Apr 19 09:42 ~]# redis-cli -p 26379 info Sentinel
 # Sentinel
 sentinel_masters:1
 sentinel_tilt:0
 sentinel_tilt_since_seconds:-1
 sentinel_running_scripts:0
 sentinel_scripts_queue_length:0
 sentinel_simulate_failure_flags:0
 master0:name=mymaster,status=ok,address=192.168.77.147:6379,slaves=2,sentinels=3

在选举后的master节点192.168.77.147:6379上查看当前仅有192.168.77.146:6379节点在线,192.168.77.145:6379因为已经shutdown了

[root@dbc ~]# redis-cli -a 123456 info replication
 # Replication
 role:master
 connected_slaves:1
 slave0:ip=192.168.77.146,port=6379,state=online,offset=1114835,lag=1
 master_failover_state:no-failover
 master_replid:c556a04bd19dfc7627743569c9f2613dc8a7232b
 master_replid2:ce16d352e923b9c77de6dd925f289dcd27e590ca
 master_repl_offset:1115121
 second_repl_offset:1090349
 repl_backlog_active:1
 repl_backlog_size:1048576
 repl_backlog_first_byte_offset:1051856
 repl_backlog_histlen:63266

启动恢复192.168.77.145:6379节点服务,并再次在检查发现之前shutdown掉的主节点192.168.77.145:6379变为了从节点了加入了当前集群。

 [root@dbc ~]# redis-cli -a 123456 info replication
 # Replication
 role:master
 connected_slaves:2
 slave0:ip=192.168.77.146,port=6379,state=online,offset=1147649,lag=0
 slave1:ip=192.168.77.145,port=6379,state=online,offset=1147792,lag=0
 master_failover_state:no-failover
 master_replid:c556a04bd19dfc7627743569c9f2613dc8a7232b
 master_replid2:ce16d352e923b9c77de6dd925f289dcd27e590ca
 master_repl_offset:1147958
 second_repl_offset:1090349
 repl_backlog_active:1
 repl_backlog_size:1048576
 repl_backlog_first_byte_offset:1051856
 repl_backlog_histlen:96103
赞(0)
未经允许不得转载:工具盒子 » Redis集群-哨兵模式搭建(1主2从3哨兵节点)