首页 > 数据库 >Redis搭建Sentinel实验环境

Redis搭建Sentinel实验环境

时间:2022-10-22 00:33:12浏览次数:73  
标签:21 Redis 192.168 56.4 6379 master Sentinel sentinel 搭建

环境准备

在物理机上启动3台物理机,IP地址分别为:192.168.56.4192.168.56.5192.168.56.6
1.确保3台虚拟机的网络是相互联通的。
2.确保已经在3台虚拟机上安装了redis(本示例中使用redis-6.2.4),参考centos 6.8安装redis
3.sentinel部署架构设计如下:
Redis Sentinel集群架构

启动master

192.168.56.4上修改redis配置文件内容如下:

daemonize yes
protected-mode no
port 6379
bind 192.168.56.4
loglevel debug
logfile /var/log/redis-sentinel.log

启动redis服务。
查看redis服务状态:

$ redis-cli -h 192.168.56.4 -p 6379
192.168.56.4:6379> info replication
# Replication
role:master # master节点
connected_slaves:0
master_failover_state:no-failover
master_replid:f2816f8d2bae0165e69939f2c68c7893d404afd6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

启动slave

192.168.56.5上修改redis配置文件内容如下:

daemonize yes
protected-mode no
port 6379
bind 192.168.56.5
loglevel debug
logfile /var/log/redis-sentinel.log
replicaof 192.168.56.4 6379

启动redis服务。
查看redis服务状态:

$ redis-cli -h 192.168.56.5 -p 6379
192.168.56.5:6379> info replication
# Replication
role:slave # slave节点
master_host:192.168.56.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:154
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:021abd5cacf2b45ade959fdcb0e50012e108bf6a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154

现在再来看master节点在状态:

192.168.56.4:6379> info replication
# Replication
role:master
connected_slaves:1 # 已经有slave节点连接上了
slave0:ip=192.168.56.5,port=6379,state=online,offset=28,lag=1 # slave节点信息
master_failover_state:no-failover
master_replid:021abd5cacf2b45ade959fdcb0e50012e108bf6a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

启动哨兵

分别在三台主机上配置Redis Sentinel。
以修改master节点上的哨兵配置为例,配置内容如下:

port 26379
bind 192.168.56.4 # 注意修改IP地址
daemonize yes
logfile /var/log/redis-sentinel.log
sentinel monitor mymaster 192.168.56.4 6379 2 # sentinel监听的master节点都是相同的

完成哨兵配置修改后,依次启动各个主机上的哨兵。

$ src/redis-server sentinel.conf --sentinel

在本示例中先启动master节点的sentinel:

1648:X 21 Oct 2022 15:04:25.238 # Sentinel ID is 0e8299513272360c51c2b0a27fecd7c3e47b2ac9
1648:X 21 Oct 2022 15:04:25.238 # +monitor master mymaster 192.168.56.4 6379 quorum 2
1648:X 21 Oct 2022 15:04:25.239 * +slave slave 192.168.56.5:6379 192.168.56.5 6379 @ mymaster 192.168.56.4 6379

依次启动其他两台节点的sentinel,这时在master节点的sentinel日志中会看到如下信息:

# 有新的sentinel节点加入了
1648:X 21 Oct 2022 15:07:31.510 * +sentinel sentinel c31a742dd7b0485beff0463fcc0a15d17f31170b 192.168.56.5 26379 @ mymaster 192.168.56.4 6379
1648:X 21 Oct 2022 15:12:01.193 * +sentinel sentinel 1a0cf759c9bcde9d06e29d99064f26eb11454a09 192.168.56.6 26379 @ mymaster 192.168.56.4 6379

同时,在各个sentinel节点上都能看到自己的启动日志:

# 192.168.56.5节点的sentinel日志
1564:X 21 Oct 2022 15:07:29.478 # Sentinel ID is c31a742dd7b0485beff0463fcc0a15d17f31170b
1564:X 21 Oct 2022 15:07:29.478 # +monitor master mymaster 192.168.56.4 6379 quorum 2
1564:X 21 Oct 2022 15:07:29.480 * +slave slave 192.168.56.5:6379 192.168.56.5 6379 @ mymaster 192.168.56.4 6379
1564:X 21 Oct 2022 15:07:30.702 * +sentinel sentinel 0e8299513272360c51c2b0a27fecd7c3e47b2ac9 192.168.56.4 26379 @ mymaster 192.168.56.4 6379
1564:X 21 Oct 2022 15:12:01.086 * +sentinel sentinel 1a0cf759c9bcde9d06e29d99064f26eb11454a09 192.168.56.6 26379 @ mymaster 192.168.56.4 6379
# 192.168.56.6节点的sentinel日志
1618:X 21 Oct 2022 15:11:59.075 # Sentinel ID is 1a0cf759c9bcde9d06e29d99064f26eb11454a09
1618:X 21 Oct 2022 15:11:59.075 # +monitor master mymaster 192.168.56.4 6379 quorum 2
1618:X 21 Oct 2022 15:11:59.078 * +slave slave 192.168.56.5:6379 192.168.56.5 6379 @ mymaster 192.168.56.4 6379
1618:X 21 Oct 2022 15:11:59.742 * +sentinel sentinel 0e8299513272360c51c2b0a27fecd7c3e47b2ac9 192.168.56.4 26379 @ mymaster 192.168.56.4 6379
1618:X 21 Oct 2022 15:12:00.301 * +sentinel sentinel c31a742dd7b0485beff0463fcc0a15d17f31170b 192.168.56.5 26379 @ mymaster 192.168.56.4 6379

至此,一个拥有一个master节点,一个slave节点,3个sentinel节点的哨兵集群环境就组件完毕了。

分别登录到3个sentinel节点进行连接测试:

$ redis-cli -h 192.168.56.4 -p 26379
192.168.56.4:26379> ping
PONG

$ redis-cli -h 192.168.56.5 -p 26379
192.168.56.5:26379> ping
PONG

$ redis-cli -h 192.168.56.6 -p 26379
192.168.56.6:26379> ping
PONG

Sentinel故障转移测试

操作步骤:将master的Redis服务停掉,看看Redis Sentinel是如何操作的。
首先,先查看下目前master节点的信息:

$ redis-cli -h 192.168.56.4 -p 26379
192.168.56.4:26379> sentinel get-master-addr-by-name mymaster
1) "192.168.56.4"
2) "6379"

然后执行下面命令(强制Redis Server休眠120秒):

$ redis-cli -h 192.168.56.4 -p 6379 DEBUG sleep 120
OK

然后再查看下master节点的信息:

$ redis-cli -h 192.168.56.4 -p 26379
192.168.56.4:26379> sentinel get-master-addr-by-name mymaster
1) "192.168.56.5"
2) "6379"

发现IP已经变成了此前salve节点的,也就是说192.168.56.5变成了master,然后我们查看下当前master的信息:

$ redis-cli -h 192.168.56.5 -p 6379
192.168.56.5:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.56.4,port=6379,state=online,offset=198958,lag=1 # 此前的master节点现在变成了slave
master_failover_state:no-failover
master_replid:796b94f178162c912cadead8c84af2633812426a
master_replid2:021abd5cacf2b45ade959fdcb0e50012e108bf6a
master_repl_offset:199236
second_repl_offset:176659
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:199236

整个过程,可以查看Redis Sentinel的日志:

1648:X 21 Oct 2022 15:22:41.916 # +sdown master mymaster 192.168.56.4 6379
1648:X 21 Oct 2022 15:22:42.031 # +new-epoch 1
1648:X 21 Oct 2022 15:22:42.037 # +vote-for-leader c31a742dd7b0485beff0463fcc0a15d17f31170b 1
1648:X 21 Oct 2022 15:22:42.991 # +odown master mymaster 192.168.56.4 6379 #quorum 3/2
1648:X 21 Oct 2022 15:22:42.991 # Next failover delay: I will not start a failover before Fri Oct 21 15:28:42 2022
1648:X 21 Oct 2022 15:22:43.154 # +config-update-from sentinel c31a742dd7b0485beff0463fcc0a15d17f31170b 192.168.56.5 26379 @ mymaster 192.168.56.4 6379
1648:X 21 Oct 2022 15:22:43.155 # +switch-master mymaster 192.168.56.4 6379 192.168.56.5 6379
1648:X 21 Oct 2022 15:22:43.157 * +slave slave 192.168.56.4:6379 192.168.56.4 6379 @ mymaster 192.168.56.5 6379
1648:X 21 Oct 2022 15:23:13.218 # +sdown slave 192.168.56.4:6379 192.168.56.4 6379 @ mymaster 192.168.56.5 6379
1648:X 21 Oct 2022 15:24:11.612 # -sdown slave 192.168.56.4:6379 192.168.56.4 6379 @ mymaster 192.168.56.5 6379

【参考】
https://www.cnblogs.com/xishuai/p/redis-sentinel.html Redis Sentinel 高可用服务架构搭建

标签:21,Redis,192.168,56.4,6379,master,Sentinel,sentinel,搭建
From: https://www.cnblogs.com/nuccch/p/16815150.html

相关文章