产品分类

新闻资讯

联系我们

IT技术问答

当前位置:主页 > IT技术问答 >

redis master-slave 切换演练

2018-10-12 18:43

redis master-slave 切换演练
redis master-slave 切换演练

环境

IP地址端口属性
192.168.31.2086379master
192.168.31.208
6378
slave
192.168.31.209
6379
master
192.168.31.209
6378
slave
192.168.31.210
6379
master
192.168.31.210
6378
slave

环境变量
PATH=$PATH:$HOME/cpprelease/redis-3.0.2/src/:$HOME/bin
BASE_PATH=/home/beidou_soa/cpprelease/
export PATH
alias redisstart1='cd ~/redis/ && redis-server $BASE_PATH/cfg/redis/redis1.conf && cd -'
alias redisstart2='cd ~/redis/ && redis-server $BASE_PATH/cfg/redis/redis2.conf && cd -'
alias redisstop1='cd ~/redis/ && redis-cli -p 6379 shutdown && cd -'
alias redisstop2='cd ~/redis/ && redis-cli -p 6378 shutdown && cd -'


1.检查环境[[email protected]~]$redis-trib.rbcheck192.168.31.208:6379Connectingtonode192.168.31.208:6379:OKConnectingtonode192.168.31.210:6378:OKConnectingtonode192.168.31.209:6378:OKConnectingtonode192.168.31.210:6379:OKConnectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.208:6378:OK>>>PerformingClusterCheck(usingnode192.168.31.208:6379)M:273aa3c0416e7d1795ce678d56bd2db148613f7e192.168.31.208:6379slots:10923-16383(5461slots)master1additionalreplica(s)S:08f61dcd66389dae5c39e375d4f52e1defa77ec1192.168.31.210:6378slots:(0slots)slavereplicates40cecda23f32cb3b8ff60752c00514f2d7d9c3d0S:d8e8369bfdbf4f9e0bebd04911181785e3ee1129192.168.31.209:6378slots:(0slots)slavereplicates4f11d4265178d72e0ccf7edf0ddabf835e9c56dfM:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master1additionalreplica(s)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master1additionalreplica(s)S:4b6e2b13b1be1a081db2153dc4beaf430b489605192.168.31.208:6378slots:(0slots)slavereplicates273aa3c0416e7d1795ce678d56bd2db148613f7e[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
[[email protected]~]$redis-cli-c-p6379127.0.0.1:6379>127.0.0.1:6379>127.0.0.1:6379>gettestkey001(nil)127.0.0.1:6379>127.0.0.1:6379>127.0.0.1:6379>settestkey002testvalue002->Redirectedtoslot[401]locatedat192.168.31.210:6379OK192.168.31.210:6379>192.168.31.210:6379>gettestkey002"testvalue002"192.168.31.210:6379>settestkey003testvalue003OK
2.准备关闭208的master[[email protected]~]$redisstop1
3.检查集群状态查看slave的日志[[email protected]]$vimredis-6378.log6874:S30Dec15:28:04.755#ErrorconditiononsocketforSYNC:Connectionrefused6874:S30Dec15:28:05.758*ConnectingtoMASTER192.168.31.208:63796874:S30Dec15:28:05.758*MASTER<->SLAVEsyncstarted6874:S30Dec15:28:05.759#ErrorconditiononsocketforSYNC:Connectionrefused6874:S30Dec15:28:06.647*FAILmessagereceivedfrom40cecda23f32cb3b8ff60752c00514f2d7d9c3d0about273aa3c0416e7d1795ce678d56bd2db148613f7e6874:S30Dec15:28:06.647#Clusterstatechanged:fail6874:S30Dec15:28:06.662#Startofelectiondelayedfor842milliseconds(rank#0,offset105547).6874:S30Dec15:28:06.762*ConnectingtoMASTER192.168.31.208:63796874:S30Dec15:28:06.762*MASTER<->SLAVEsyncstarted6874:S30Dec15:28:06.763#ErrorconditiononsocketforSYNC:Connectionrefused6874:S30Dec15:28:07.565#Startingafailoverelectionforepoch4.6874:S30Dec15:28:07.567#Failoverelectionwon:I'mthenewmaster.6874:S30Dec15:28:07.567#configEpochsetto4aftersuccessfulfailover6874:M30Dec15:28:07.567*Discardingpreviouslycachedmasterstate.6874:M30Dec15:28:07.567#Clusterstatechanged:ok
查看集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.208:6378Connectingtonode192.168.31.208:6378:OKConnectingtonode192.168.31.209:6378:OKConnectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6378:OKConnectingtonode192.168.31.210:6379:OK>>>PerformingClusterCheck(usingnode192.168.31.208:6378)M:4b6e2b13b1be1a081db2153dc4beaf430b489605192.168.31.208:6378slots:10923-16383(5461slots)master0additionalreplica(s)S:d8e8369bfdbf4f9e0bebd04911181785e3ee1129192.168.31.209:6378slots:(0slots)slavereplicates4f11d4265178d72e0ccf7edf0ddabf835e9c56dfM:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master1additionalreplica(s)S:08f61dcd66389dae5c39e375d4f52e1defa77ec1192.168.31.210:6378slots:(0slots)slavereplicates40cecda23f32cb3b8ff60752c00514f2d7d9c3d0M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master1additionalreplica(s)[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
4.关闭slave[[email protected]~]$redisstop1

5.检查集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:OKConnectingtonode192.168.31.210:6378:OKConnectingtonode192.168.31.209:6378:OKConnectingtonode192.168.31.208:6378:OK>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master1additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master1additionalreplica(s)S:08f61dcd66389dae5c39e375d4f52e1defa77ec1192.168.31.210:6378slots:(0slots)slavereplicates40cecda23f32cb3b8ff60752c00514f2d7d9c3d0S:d8e8369bfdbf4f9e0bebd04911181785e3ee1129192.168.31.209:6378slots:(0slots)slavereplicates4f11d4265178d72e0ccf7edf0ddabf835e9c56dfM:4b6e2b13b1be1a081db2153dc4beaf430b489605192.168.31.208:6378slots:10923-16383(5461slots)master0additionalreplica(s)[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
6.关闭3台slave[[email protected]~]$redisstop2
[[email protected]~]$redisstop2
[[email protected]~]$redisstop2

7.检查集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:OKConnectingtonode192.168.31.208:6378:OK>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master0additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master0additionalreplica(s)M:4b6e2b13b1be1a081db2153dc4beaf430b489605192.168.31.208:6378slots:10923-16383(5461slots)master0additionalreplica(s)[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
8.在关闭slave的情况下关闭一台master[[email protected]~]$redisstop1

9.检查集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:OK>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master0additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master0additionalreplica(s)[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[ERR]Notall16384slotsarecoveredbynodes.
集群进入fail状态,不可用
10.在集群down的状态开启一台slave[[email protected]~]$redisstar2

11.检查集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:OKConnectingtonode192.168.31.208:6379:OK***WARNING:192.168.31.208:6379claimstobeslaveofunknownnodeID4b6e2b13b1be1a081db2153dc4beaf430b489605.>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master0additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master0additionalreplica(s)S:273aa3c0416e7d1795ce678d56bd2db148613f7e192.168.31.208:6379slots:(0slots)slavereplicates4b6e2b13b1be1a081db2153dc4beaf430b489605[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[ERR]Notall16384slotsarecoveredbynodes
集群在fail状态,无法自动选举master,集群不可用
12开启master,检查集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:OKConnectingtonode192.168.31.208:6379:OKConnectingtonode192.168.31.208:6378:OK>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master0additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master0additionalreplica(s)S:273aa3c0416e7d1795ce678d56bd2db148613f7e192.168.31.208:6379slots:(0slots)slavereplicates4b6e2b13b1be1a081db2153dc4beaf430b489605M:4b6e2b13b1be1a081db2153dc4beaf430b489605192.168.31.208:6378slots:10923-16383(5461slots)master1additionalreplica(s)[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
13.关闭一台master[[email protected]~]$redisstop1

14.检查集群状态[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:^[[AOKConnectingtonode192.168.31.208:6379:OK***WARNING:192.168.31.208:6379claimstobeslaveofunknownnodeID4b6e2b13b1be1a081db2153dc4beaf430b489605.>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master0additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master0additionalreplica(s)S:273aa3c0416e7d1795ce678d56bd2db148613f7e192.168.31.208:6379slots:(0slots)slavereplicates4b6e2b13b1be1a081db2153dc4beaf430b489605[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[ERR]Notall16384slotsarecoveredbynodes.
预计1分钟之后
[[email protected]~]$redis-trib.rbcheck192.168.31.209:6379Connectingtonode192.168.31.209:6379:OKConnectingtonode192.168.31.210:6379:OKConnectingtonode192.168.31.208:6379:OK>>>PerformingClusterCheck(usingnode192.168.31.209:6379)M:4f11d4265178d72e0ccf7edf0ddabf835e9c56df192.168.31.209:6379slots:5461-10922(5462slots)master0additionalreplica(s)M:40cecda23f32cb3b8ff60752c00514f2d7d9c3d0192.168.31.210:6379slots:0-5460(5461slots)master0additionalreplica(s)M:273aa3c0416e7d1795ce678d56bd2db148613f7e192.168.31.208:6379slots:10923-16383(5461slots)master0additionalreplica(s)[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
选举完成。redis集群恢复。

redis架构


架构细节:
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value

2) redis-cluster选举:容错



(1)领着选举过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.
(2):什么时候整个集群不可用(cluster_state:fail),当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误
a:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成进群的slot映射[0-16383]不完成时进入fail状态.
b:如果进群超过半数以上master挂掉,无论是否有slave集群进入fail状态.


http://www.bkjia.com/PHPjc/1183904.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1183904.htmlTechArticleredis master-slave 切换演练 redis master-slave 切换演练 环境 IP地址 端口 属性 192.168.31.208 6379 master 192.168.31.208 6378 slave 192.168.31.209 6379 master 192.16...

本文源自: 环亚娱乐

Copyright © 2005-2017 http://www.rheroesinc.com 环亚娱乐_环亚娱乐AG旗舰厅_环亚娱乐AG最佳线路_腾讯娱乐版权所有

扫一扫,加关注