ITPub博客

首页 > 数据库 > NoSQL > 使用 Sentinel failover执行切换Redis Master

使用 Sentinel failover执行切换Redis Master

原创 NoSQL 作者:bestpaydata 时间:2016-04-29 15:02:43 0 删除 编辑


先写结论:

    通过设置从库切换的优先级,我们可以通过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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-01-19

  • 博文量
    126
  • 访问量
    985844