2021-05-14

Redis 集群伸缩原理

Redis 节点分别维护自己负责的槽和对应的数据。伸缩原理:Redis 槽和对应数据在不同节点之间移动

环境:CentOS7 搭建 Redis 集群

一、集群扩容

1. 手动扩容

(1) 准备节点 9007,并加入集群

192.168.11.40:9001> cluster meet 192.168.11.40 9007

【注意】若 cluster meet 加入已存在于其它集群的节点,会导致集群合并,造成数据错乱!。建议使用 redis-cli 的 add-node:

# 若节点已加入其它集群或包含数据,会报错add-node new_host:new_port existing_host:existing_port   --cluster-slave # 直接添加为从节点   --cluster-master-id <arg> # 从节点对应的主节点id

(2) 迁移槽和数据

  • 槽在迁移过程中集群可以正常提供读写服务
  • 首先确定原有节点的哪些槽需要迁移到新节点。确保每个节点负责相似数量的槽,保证各节点的数据均匀
  • 槽是 Redis 集群管理数据的基本单位。数据迁移是逐槽进行

槽迁移流程:

槽迁移流程

  • 目标节点准备导入槽的数据:目标节点执行cluster setslot {slot} importing {sourceNodeId}
  • 源节点准备迁出槽的数据:源节点执行cluster setslot {slot} migrating {targetNodeId}
  • 获取 count 个属于槽 slot 的键:源节点执行cluster getkeysinslot {slot} {count}
  • 迁移键:源节点执行migrate {targetIp} {targetPort} "" 0 {timeout} keys {keys...},把键通过流水线(pipeline)机制批量迁移到目标节点。Redis3.0.6 后才支持批量迁移
  • 重复上两步,直到槽下所有的键值数据迁移到目标节点
  • 向集群所有主节点通知槽被分配给目标节点:集群内所有主节点执行cluster setslot {slot} node {targetNodeId}

内部伪代码:

def move_slot(source,target,slot): # 目标节点准备导入槽 target.cluster("setslot",slot,"importing",source.nodeId); # 目标节点准备全出槽 source.cluster("setslot",slot,"migrating",target.nodeId); while true :  # 批量从源节点获取键  keys = source.cluster("getkeysinslot",slot,pipeline_size);  if keys.length == 0:   # 键列表为空时,退出循环   break;  # 批量迁移键到目标节点  source.call("migrate",target.host,target.port,"",0,timeout,"keys",keys);  # 向集群所有主节点通知槽被分配给目标节点  for node in nodes:   if node.flag == "slave":    continue;   node.cluster("setslot",slot,"node",target.nodeId);

(3) 将 9001 的槽 4096 迁移到 9007 中

准备数据

192.168.11.40:9001> set key:test:5028 value:5028192.168.11.40:9001> set key:test:68253 value:68253

目标节点准备工作

192.168.11.40:9007> cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 master - 0 1620928869000 2 connected 0-5461bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d 192.168.11.40:9007@19007 myself,master - 0 1620928868000 0 connected...# 9007 准备导入槽 4096 的数据192.168.11.40:9007> cluster setslot 4096 importing 8ccdb0963411ebd05ce21952bdd4b7597825afdcOK# 槽 4096 已开启导入状态192.168.11.40:9007> cluster nodesbb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d 192.168.11.40:9007@19007 myself,master - 0 1620928959000 0 connected [4096-<-8ccdb0963411ebd05ce21952bdd4b7597825afdc]...

源节点准备工作

# 9001 准备导出槽 4096 数据192.168.11.40:9001> cluster setslot 4096 migrating bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80dOK# 槽 4096 已开启导出状态192.168.11.40:9001> cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 myself,master - 0 1620929179000 2 connected 0-5461 [4096->-bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d]...

导出数据

# 获取 100 个属于槽 4096 的键192.168.11.40:9001> cluster getkeysinslot 4096 1001) "key:test:5028"2) "key:test:68253"# 查看数据192.168.11.40:9001> mget key:test:5028 key:test:682531) "value:5028"2) "value:68253"# 迁移这2个键:migrate 命令保证了每个键迁移过程的原子性192.168.11.40:9001> migrate 192.168.11.40 9007 "" 0 5000 keys key:test:5028 key:test:68253OK# 再次查询会报 ASK 错误:引导客户端找到数据所在的节点192.168.11.40:9001> mget key:test:5028 key:test:68253(error) ASK 4096 192.168.11.40:9007

通知所有主节点:槽 4096 指派给 9007

192.168.11.40:9001> cluster setslot 4096 node bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d192.168.11.40:9002> cluster setslot 4096 node bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d192.168.11.40:9003> cluster setslot 4096 node bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d192.168.11.40:9007> cluster setslot 4096 node bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d

查看最终结果

192.168.11.40:9007> cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 master - 0 1620931743303 7 connected 0-4095 4097-5461bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d 192.168.11.40:9007@19007 myself,master - 0 1620931741000 8 connected 4096...

2. 使用 redis-cli 扩容

redis-cli 提供了槽重分片功能

reshard 命令参数详解:

reshard host:port # 集群内任意节点地址   --cluster-from <arg> # 源节点id,逗号分隔   --cluster-to <arg> # 目标节点id,只有一个   --cluster-slots <arg> # 迁移多少个槽   --cluster-yes # 确认执行reshard   --cluster-timeout <arg> # 每次 migrate 操作的超时时间,默认 60000ms   --cluster-pipeline <arg> # 每次批量迁移键的数量,默认 10   --cluster-replace

将 9001、9002、9003 的槽迁移到 9007,共迁移 4096 个

$ /usr/local/redis/bin/redis-cli --cluster reshard 192.168.11.40:9001M: 8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001 slots:[0-4095],[4097-5461] (5461 slots) master 1 additional replica(s)M: bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d 192.168.11.40:9007 slots:[4096] (1 slots) master...[OK] All 16384 slots covered.How many slots do you want to move (from 1 to 16384)? 4096What is the receiving node ID? bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80dPlease 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: 8ccdb0963411ebd05ce21952bdd4b7597825afdcSource node #2: 5786e3237c7fa413ed22465d15be721f95e72cfaSource node #3: 85ceb9826e8aa003169c46fb4ba115c72002d4f9Source node #4: done Moving slot 0 from 8ccdb0963411ebd05ce21952bdd4b7597825afdc ... Moving slot 12287 from 85ceb9826e8aa003169c46fb4ba115c72002d4f9Do you want to proceed with the proposed reshard plan (yes/no)? yesMoving slot 0 from 192.168.11.40:9001 to 192.168.11.40:9007:...Moving slot 12287 from 192.168.11.40:9003 to 192.168.11.40:9007:

查看最终结果

192.168.11.40:9007> cluster nodes8ccdb0963411ebd05ce21952bdd4b7597825afdc 192.168.11.40:9001@19001 master - 0 1620933907753 7 connected 1366-4095 4097-54615786e3237c7fa413ed22465d15be721f95e72cfa 192.168.11.40:9002@19002 master - 0 1620933906733 1 connected 6827-1092285ceb9826e8aa003169c46fb4ba115c72002d4f9 192.168.11.40:9003@19003 master - 0 1620933905000 3 connected 12288-16383bb1bb0f5f9e0ee67846ba8ec94a38da700e2e80d 192.168.11.40:9007@19007 myself,master - 0 1620933900000 8 connected 0-1365 4096 5462-6826 10923-12287...

检查节点之间槽的均衡性

$ /usr/local/redis/bin/redis-cli --cluster rebalance 192.168.11.40:9001...[OK] All 16384 slots covered.*** No rebalancing needed! All nodes are within the 2.00% threshold.

迁移之后所有主节点负责的槽数量差异在 2% 以内,因此集群节点数据相对均匀,无需调整

二、集群收缩

节点下线流程

1. 迁移槽

执行 reshard 三次,将数据平均分布到其他三个节点

2. 忘记节点

60s 内对所有节点执行如下操作:(不建议)

# 执行后,会将该节点加入禁用列表(持续 60s),不再向其发送 Gossip 消息cluster forget {nodeId}

建议使用 redis-cli 的 del-node 忘记节点:

/usr/local/redis/bin/redis-cli --cluster del-node {host:port} {nodeId}

内部伪代码

def delnode_cluster_cmd(downNode): # 下线节点不允许包含slots if downNode.slots.length != 0  exit 1 end # 向集群内节点发送cluster forget for n in nodes:  if n.id == downNode.id:   # 不能对自己做forget操作   continue;  # 如果下线节点有从节点则把从节点指向其他主节点  if n.replicate && n.replicate.nodeId == downNode.id :   # 指向拥有最少从节点的主节点   master = get_master_with_least_replicas();   n.cluster("replicate",master.nodeId);  #发送忘记节点命令  n.cluster('forget',downNode.id) # 节点关闭 downNode.shutdown();

若主从节点都要下线,先下线从,避免全量复制









原文转载:http://www.shaoqun.com/a/742787.html

跨境电商:https://www.ikjzd.com/

邮政电话:https://www.ikjzd.com/w/202

prime:https://www.ikjzd.com/w/129


Redis节点分别维护自己负责的槽和对应的数据。伸缩原理:Redis槽和对应数据在不同节点之间移动环境:CentOS7搭建Redis集群一、集群扩容1.手动扩容(1)准备节点9007,并加入集群192.168.11.40:9001>clustermeet192.168.11.409007【注意】若clustermeet加入已存在于其它集群的节点,会导致集群合并,造成数据错乱!。建议使用red
折扣网:https://www.ikjzd.com/w/74
worldfirst:https://www.ikjzd.com/w/289
马士基集团:https://www.ikjzd.com/w/1296
Wooplr:https://www.ikjzd.com/w/1935
老公的哥哥常偷窥我们夫妻爱爱:http://lady.shaoqun.com/a/272046.html
亚马逊卖家后台新变化!账户健康指标调整!:https://www.ikjzd.com/home/123059

No comments:

Post a Comment