美文网首页
TiDB~tikv缩容

TiDB~tikv缩容

作者: 开心的蛋黄派 | 来源:发表于2024-07-06 11:03 被阅读0次

0. 基础知识

  • Peer:Region是一个虚拟的逻辑概念,每个Region分配一个ID,即region_id。一个Region下默认有3个副本,每个副本称为peer,并各分配一个peer_id。每个peer位于不同的TiKV实例上。
  • Store:指的是TiDB集群中的1个TiKV或TiFlash实例。
  • Store的生命周期中包含多种状态:Tikv下线是异步过程,其状态会经历UP、Offline、Tombstone三个阶段。

1. 确认缩容节点

  • 确定需要下线的TiKV节点。

2. 修改PD参数,设置权重

tiup pd-ctl -i -u http://127.0.0.1:2379 config set leader-schedule-limit [新的限制值]
tiup pd-ctl -i -u http://127.0.0.1:2379 config set region-schedule-limit [新的限制值]
tiup pd-ctl -i -u http://127.0.0.1:2379 config set max-pending-peer-count [新的限制值]
  • 调整leader-schedule-limitregion-schedule-limitmax-pending-peer-count参数以加快Region迁移速度。
  • 使用pd-ctl store weight <store_id> <leader_weight> <region_weight>命令设置待下线节点的leader_weightregion_weight为0。
  • 对于Leader转移,也可以使用pd-ctl scheduler add evict-leader-scheduler store_id命令添加evict调度方式,以驱逐待下线store上的Leader。

3. 执行缩容操作

tiup cluster scale-in cluster-name -N 下线节点ID
  • 使用tiup cluster scale-in命令执行缩容操作。
    4. 关注集群状态

  • 监控Region和Leader的分布情况。

  • 检查TiKV节点的状态。

  • 监控集群整体的资源使用情况。

5. 确认

  • 使用display命令或相关监控工具确认集群状态。
  • 监控TiKV状态,确保其为Tombstone且拥有Region为0。

6. 清理

tiup pd-ctl -i -u http://127.0.0.1:2379 store --state Tombstone
tiup pd-ctl -i -u http://127.0.0.1:2379 store --remove-tombstone [StoreID]
  • 清理处于Tombstone状态的TiKV Store。

7. TiKV Store状态说明

  • Up:提供服务。
  • Disconnect:与PD心跳丢失超过20秒,超过max-store-down-time后变为Down
  • Down:与集群失去连接超过max-store-down-time,默认30分钟。
  • Offline:手动下线,搬离所有Region至其他Up状态Store,变为Tombstone后可清理。
  • Tombstone:完全下线,可安全清理。

8. 无法正常缩容的原因及解决方案

  • 原因:存留有Region处于pending offline状态。
  • 解决方案:
    1. 使用tiup pd-ctl查看有问题TiKV的具体Region信息:

      tiup pd-ctl -u http://pd-ip:2379 store [StoreID]
      

      关注regions列,找出没有迁移成功的Region。

    2. 使用curl命令判断Region是否为空:

      curl http://tidb-server-ip:10080/regions/[RegionID]
      

      检查返回的JSON响应,确认Region是否为空。

    3. 如果为空,尝试从PD和TiKV层进行删除:

      • 使用tiup pd-ctl移除具体的Region:
        tiup pd-ctl -u http://pd-ip:2379 operator add remove-peer [RegionID] [StoreID]
        
      • 直接删除整个Store(谨慎操作,确保Store上已无Region):
        tiup pd-ctl -u http://pd-ip:2379 store delete [StoreID]
        
      • 在TiKV层使用tikv-ctl命令进行删除(慎用,通常不建议直接操作TiKV层):
        tikv-ctl --db /path/to/tikv/data tombstone -r [RegionID] --force
        
        注意:/path/to/tikv/data需要替换为实际的TiKV数据目录路径。

相关文章

网友评论

      本文标题:TiDB~tikv缩容

      本文链接:https://www.haomeiwen.com/subject/scbgcjtx.html