使用 Sentinel failover执行切换Redis Master

本文介绍了如何通过Sentinel在线切换Redis的Master到指定从库,详细步骤包括设置从库优先级、执行切换命令,并展示了Sentinel的日志,整个过程在2秒内完成,适合数据库迁移和维护。
部署运行你感兴趣的模型镜像


先写结论:

    通过设置从库切换的优先级,我们可以通过sentinel 来在线切换master到指定的实例,切换时间2s,并且重新建立主从关系,速度很快,从而满足我们迁移、维护硬件等需求。

 

环境:

Sentinel 192.168.164.231 26379

Master :    192.168.164.232 6379

Slave1:     192.168.164.231 6379

Slave2:     192.168.164.243 6379

 

 

使用主要命令:

config set slave-priority <parameter>

config get slave-priority <parameter>

Config rewrite

sentinel failover <parameter>

 

实验步奏:

现在我们需要把master切换到Slave2: 192.168.164.243 6379上:

1

首先在slave2 192.168.164.243 6379

上设置从库切换优先级:

 

127.0.0.1:6379> config set slave-priority 200

OK

127.0.0.1:6379> config get slave-priority

1) "slave-priority"

2) "200"

 

2

Sentinel 192.168.164.231 26379

上执行:192.168.164.231:26379> sentinel failover test

OK

 

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=test,status=ok,address=192.168.164.232:6379,slaves=2,sentinels=1

192.168.164.231:26379> sentinel failover test

OK

192.168.164.231:26379> info Sentinel

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=test,status=ok,address=192.168.164.231:6379,slaves=2,sentinels=1

 

这里说明 "slave-priority"越高,该节点的优先级越差

 

3

重新登录slave2 192.168.164.243 6379

 

127.0.0.1:6379> config set slave-priority 50

OK

127.0.0.1:6379> config get slave-priority

1) "slave-priority"

2) "50"

 

Sentinel 192.168.164.231 26379

上执行:192.168.164.231:26379> sentinel failover test

OK

完成了到192.168.164.243:6379的切换

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=test,status=ok,address=192.168.164.243:6379,slaves=2,sentinels=1

 

sentinellog来看:

81889:X 21 Apr 10:19:10.717 # Executing user requested FAILOVER of 'test'

81889:X 21 Apr 10:19:10.717 # +new-epoch 867

81889:X 21 Apr 10:19:10.717 # +try-failover master test 192.168.164.231 6379

81889:X 21 Apr 10:19:10.798 # +vote-for-leader 1262f201dc53f23fd6f0da707785d90a861e7fe0 867

81889:X 21 Apr 10:19:10.798 # +elected-leader master test 192.168.164.231 6379

81889:X 21 Apr 10:19:10.798 # +failover-state-select-slave master test 192.168.164.231 6379

81889:X 21 Apr 10:19:10.853 # +selected-slave slave 192.168.164.243:6379 192.168.164.243 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:10.853 * +failover-state-send-slaveof-noone slave 192.168.164.243:6379 192.168.164.243 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:10.925 * +failover-state-wait-promotion slave 192.168.164.243:6379 192.168.164.243 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:11.878 # +promoted-slave slave 192.168.164.243:6379 192.168.164.243 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:11.878 # +failover-state-reconf-slaves master test 192.168.164.231 6379

81889:X 21 Apr 10:19:11.945 * +slave-reconf-sent slave 192.168.164.232:6379 192.168.164.232 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:12.931 * +slave-reconf-inprog slave 192.168.164.232:6379 192.168.164.232 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:12.931 * +slave-reconf-done slave 192.168.164.232:6379 192.168.164.232 6379 @ test 192.168.164.231 6379

81889:X 21 Apr 10:19:12.980 # +failover-end master test 192.168.164.231 6379

81889:X 21 Apr 10:19:12.981 # +switch-master test 192.168.164.231 6379 192.168.164.243 6379

81889:X 21 Apr 10:19:12.985 * +slave slave 192.168.164.232:6379 192.168.164.232 6379 @ test 192.168.164.243 6379

81889:X 21 Apr 10:19:12.985 * +slave slave 192.168.164.231:6379 192.168.164.231 6379 @ test 192.168.164.243 6379

81889:X 21 Apr 10:19:23.063 * +convert-to-slave slave 192.168.164.231:6379 192.168.164.231 6379 @ test 192.168.164.243 6379

 

 

切换主库需要时间2s,完成slave的重建10s(和数据量有关)

完全按照我们设置的优先级优先级来进行了切换。


                              ---- writen by liuyu

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30109892/viewspace-2090652/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30109892/viewspace-2090652/

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值