3-redis集群高级操作

视频课程

https://www.bilibili.com/video/av57628808?t=2968

将7006节点增加到redis集群中(水平扩展)

我们在原始集群基础上再增加一主(7006)一从(7007),增加节点后的集群参见下图,新增节点用虚线框表示

学会使用命令帮助行解决问题

root@ubuntu-130:/opt/redis/7007#  ./redis-cli  --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

1.create:创建一个集群环境host1:port1 ... hostN:portN

2.call:可以执行redis命令

3.add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port

4.del-node:移除一个节点

5.reshard:重新分片

6.check:检查集群状态  

将7006节点增加到集群中

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster add-node 192.168.13.130:7006 192.168.13.130:7003

先查看集群节点信息,7006端口已经在集群中了

root@ubuntu-130:/opt/redis/7007# ./redis-cli -h 192.168.13.130 -p 7006 -c
192.168.13.130:7006> CLUSTER nodes
eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004@17004 slave 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 0 1570091944000 2 connected
7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005@17005 slave 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 0 1570091945229 3 connected
9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006@17006 myself,master - 0 1570091942000 0 connected
d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000@17000 master - 0 1570091943000 1 connected 0-5460
0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001@17001 master - 0 1570091944000 2 connected 5461-10922
604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003@17003 slave d2612e7669a28dfeaf03a90b661b8e2bc966af64 0 1570091944215 1 connected
3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002@17002 master - 0 1570091943207 3 connected 10923-16383

增加7007节点,并将它作为7006的slaver

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster add-node 192.168.13.130:7007 192.168.13.130:7003 --cluster-slave --cl
>>> Adding node 192.168.13.130:7007 to cluster 192.168.13.130:7003
>>> Performing Cluster Check (using node 192.168.13.130:7003)
S: 604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003
   slots: (0 slots) slave
   replicates d2612e7669a28dfeaf03a90b661b8e2bc966af64
M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
   slots: (0 slots) master
S: 7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005
   slots: (0 slots) slave
   replicates 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004
   slots: (0 slots) slave
   replicates 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.13.130:7007 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 192.168.13.130:7006.
[OK] New node added correctly.
root@ubuntu-130:/opt/redis/7007#

查看节点信息可以知道,7007已经变更为7006的slaver了

槽位迁移 (扩容集群)

重新分配槽位

./redis-cli --cluster reshard 192.168.13.130:7000
>>> Performing Cluster Check (using node 192.168.13.130:7000)
M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004
   slots: (0 slots) slave
   replicates 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
   slots: (0 slots) master
   1 additional replica(s)
S: 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e 192.168.13.130:7007
   slots: (0 slots) slave
   replicates 9696373588ef54337542627daa48d30a9d3808f5
M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005
   slots: (0 slots) slave
   replicates 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003
   slots: (0 slots) slave
   replicates d2612e7669a28dfeaf03a90b661b8e2bc966af64
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#这里指定迁移槽位数量
How many slots do you want to move (from 1 to 16384)? 4096
#这里指定给那个nodeID分配
What is the receiving node ID? 9696373588ef54337542627daa48d30a9d3808f5
#下面指定分配策略
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: d2612e7669a28dfeaf03a90b661b8e2bc966af64
Source node #2: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
Source node #3: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
Source node #4: done
#下面是迁移计划
Ready to move 4096 slots.
  Source nodes:
    M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
  Destination node:
    M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
       slots: (0 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 5461 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5462 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5463 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5464 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5465 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5466 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5467 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
    Moving slot 5468 from 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
Moving slot 12282 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12283 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12284 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12285 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12286 from 192.168.13.130:7002 to 192.168.13.130:7006:
Moving slot 12287 from 192.168.13.130:7002 to 192.168.13.130:7006:

(ps:输入all为从所有主节点(7000,7001,7002)中分别抽取相应的槽数指定到新节点中,抽取的总槽数为4096个) 

迁移计划是将7000的0~1364槽位,7001的5461-6826,7002的10923-12287迁移到7006节点

槽位迁回:

将7006的0-1364的1065个槽位迁移到7000节点上

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster reshard 192.168.13.130:7000 --cluster-from 9696373588ef54337542627daa48d30a9d3808f5 --cluster-to d2612e7669a28dfeaf03a90b661b8e2bc966af64 --cluster-slots 1365
>>> Performing Cluster Check (using node 192.168.13.130:7000)
M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004
   slots: (0 slots) slave
   replicates 0bd95547be6cefa80dd4047039ccd2a6fe6896f0
M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e 192.168.13.130:7007
   slots: (0 slots) slave
   replicates 9696373588ef54337542627daa48d30a9d3808f5
M: 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005
   slots: (0 slots) slave
   replicates 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c
M: 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003
   slots: (0 slots) slave
   replicates d2612e7669a28dfeaf03a90b661b8e2bc966af64
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Ready to move 1365 slots.
  Source nodes:
    M: 9696373588ef54337542627daa48d30a9d3808f5 192.168.13.130:7006
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
       1 additional replica(s)
  Destination node:
    M: d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
  Resharding plan:
    Moving slot 0 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 1 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 2 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 3 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 4 from 9696373588ef54337542627daa48d30a9d3808f5
    Moving slot 5 from 9696373588ef54337542627daa48d30a9d3808f5

查看迁回情况

删除节点

查看7006没有槽位后,就可以删除节点了,否则会删除时失败

oot@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e >>> Removing node 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 9696373588ef54337542627daa48d30a9d3808f5 >>> Removing node 9696373588ef54337542627daa48d30a9d3808f5 from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli -h 192.168.13.130 -p 7000 -c
192.168.13.130:7000> CLUSTER nodes
eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004@17004 slave 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 0 15700955 15366 10 connected
3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002@17002 master - 0 1570095516375 11 connected 10923-16383
7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005@17005 slave 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 0 15700955 14000 11 connected
0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001@17001 master - 0 1570095514335 10 connected 5461-10922
d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000@17000 myself,master - 0 1570095515000 9 connected 0-5460
604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003@17003 slave d2612e7669a28dfeaf03a90b661b8e2bc966af64 0 15700955 15000 9 connected

删除节点7006和7007

建议先删除slaver节点7007再删除7006,否则当删除7006会将7007变为主节点的(槽位数据都会自动迁移)

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e >>> Removing node 18adc88658b01a2f991d6f5ecc8bd6f7a1fe6a6e from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster del-node 192.168.13.130:7000 9696373588ef54337542627daa48d30a9d3808f5 >>> Removing node 9696373588ef54337542627daa48d30a9d3808f5 from cluster 192.168.13.130:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
root@ubuntu-130:/opt/redis/7007# ./redis-cli -h 192.168.13.130 -p 7000 -c
192.168.13.130:7000> CLUSTER nodes
eab93120449b1ef90dfd082941b2cdacc76890c2 192.168.13.130:7004@17004 slave 0bd95547be6cefa80dd4047039ccd2a6fe6896f0 0 15700955 15366 10 connected
3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 192.168.13.130:7002@17002 master - 0 1570095516375 11 connected 10923-16383
7a7d5a7b766f3baa018d240b8b8d6868b34bb56a 192.168.13.130:7005@17005 slave 3e0eb4d1e36b2ea8dcc6b862bb2c89e92847d50c 0 15700955 14000 11 connected
0bd95547be6cefa80dd4047039ccd2a6fe6896f0 192.168.13.130:7001@17001 master - 0 1570095514335 10 connected 5461-10922
d2612e7669a28dfeaf03a90b661b8e2bc966af64 192.168.13.130:7000@17000 myself,master - 0 1570095515000 9 connected 0-5460
604fe9ed88ad20584821ae39f14223faa5b21286 192.168.13.130:7003@17003 slave d2612e7669a28dfeaf03a90b661b8e2bc966af64 0 15700955 15000 9 connected

学会利用help帮助工具

root@ubuntu-130:/opt/redis/7007# ./redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help

 Redis集群选举原理分析

当slave发现自己的master变为FAIL状态时,便尝试进行Failover,以期成为新的master。由于挂掉的master可能会有多个slave,从而存在多个slave竞争成为master节点的过程, 其过程如下:

1.slave发现自己的master变为FAIL

2.将自己记录的集群currentEpoch加1,并广播FAILOVER_AUTH_REQUEST 信息

3.其他节点收到该信息,只有master响应,判断请求者的合法性,并发送FAILOVER_AUTH_ACK,对每一个epoch只发送一次ack

4.尝试failover的slave收集FAILOVER_AUTH_ACK

5.超过半数后变成新Master

6.广播Pong通知其他集群节点。

从节点并不是在主节点一进入 FAIL 状态就马上尝试发起选举,而是有一定延迟,一定的延迟确保我们等待FAIL状态在集群中传播,slave如果立即尝试选举,其它masters或许尚未意识到FAIL状态,可能会拒绝投票

•延迟计算公式:

 DELAY = 500ms + random(0 ~ 500ms) + SLAVE_RANK * 1000ms

•SLAVE_RANK表示此slave已经从master复制数据的总量的rank。Rank越小代表已复制的数据越新。这种方式下,持有最新数据的slave将会首先发起选举(理论上)。

jedis操作redis集群原理

 

redis的99.9%集群可用原理分析:

因为java的jedisCluster创建redis集群时,jedisCluster会为每一个节点创建jedisPool,当node1挂了,jedisCluster会写入失败,虽然node1的slaver会转成master,但该切换时间段客户端写入的数据还是会丢失)

Redis集群选举原理分析

当slave发现自己的master变为FAIL状态时,便尝试进行Failover,以期成为新的master。由于挂掉的master可能会有多个slave,从而存在多个slave竞争成为master节点的过程, 其过程如下:

1.slave发现自己的master变为FAIL

2.将自己记录的集群currentEpoch加1,并广播FAILOVER_AUTH_REQUEST 信息

3.其他节点收到该信息,只有master响应,判断请求者的合法性,并发送FAILOVER_AUTH_ACK,对每一个epoch只发送一次ack

4.尝试failover的slave收集FAILOVER_AUTH_ACK

5.超过半数后变成新Master

6.广播Pong通知其他集群节点。

 

从节点并不是在主节点一进入 FAIL 状态就马上尝试发起选举,而是有一定延迟,一定的延迟确保我们等待FAIL状态在集群中传播,slave如果立即尝试选举,其它masters或许尚未意识到FAIL状态,可能会拒绝投票

•延迟计算公式:

 DELAY = 500ms + random(0 ~ 500ms) + SLAVE_RANK * 1000ms

•SLAVE_RANK表示此slave已经从master复制数据的总量的rank。Rank越小代表已复制的数据越新。这种方式下,持有最新数据的slave将会首先发起选举(理论上)。

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值