扩容的内部逻辑如同部署类似,TiUP cluster 会先保证节点的 SSH 连接,在目标节点上创建必要的目录,然后执行部署并且启动服务。其中 PD 节点的扩容会通过 join 方式加入到集群中,并且会更新与 PD 有关联的服务的配置;其他服务直接启动加入到集群中。所有服务在扩容时都会做正确性验证,最终返回是否扩容成功。

    1. 新建 scale.yaml 文件,添加 TiKV 和 PD 节点 IP。
    1. 执行扩容操作。TiUP cluster 根据 scale.yaml 文件中声明的端口、目录等信息在集群中添加相应的节点。
    1. $ tiup cluster scale-out tidb-test scale.yaml

    执行完成之后可以通过 tiup cluster display tidb-test 命令检查扩容后的集群状态。

    1.3.1.2 缩容

    • 对 TiKV 及 Binlog 组件的操作
      • TiUP cluster 通过 API 将其下线后直接退出而不等待下线完成
      • 等之后再执行集群操作相关的命令时会检查是否存在已经下线完成的 TiKV 或者 Binlog 节点。如果不存在,则继续执行指定的操作;如果存在,则执行如下操作:
        • 停止已经下线掉的节点的服务
        • 清理已经下线掉的节点的相关数据文件
        • 更新集群的拓扑,移除已经下线掉的节点
    • 对其他组件的操作
      • PD 组件的下线通过 API 将指定节点从集群中 delete 掉(这个过程很快),然后停掉指定 PD 的服务并且清除该节点的相关数据文件
      • 下线其他组件时,直接停止并且清除节点的相关数据文件
    1. 缩容需要指定至少两个参数,一个是集群名字,另一个是节点 ID。比如我想要将 172.16.5.140 上的 TiKV 干掉。首先我们通过 display 命令查看当前集群节点的信息。
    1. Starting /root/.tiup/components/cluster/v0.4.5/cluster display prod-cluster
    2. TiDB Cluster: prod-cluster
    3. TiDB Version: v3.0.12
    4. ID Role Host Ports Status Data Dir Deploy Dir
    5. -- ---- ---- ----- ------ -------- ----------
    6. 172.16.5.134:2379 pd 172.16.5.134 2379/2380 Healthy|L data/pd-2379 deploy/pd-2379
    7. 172.16.5.139:2379 pd 172.16.5.139 2379/2380 Healthy data/pd-2379 deploy/pd-2379
    8. 172.16.5.140:2379 pd 172.16.5.140 2379/2380 Healthy data/pd-2379 deploy/pd-2379
    9. 172.16.5.134:9090 prometheus 172.16.5.134 9090 Up data/prometheus-9090 deploy/prometheus-9090
    10. 172.16.5.134:4000 tidb 172.16.5.134 4000/10080 Up - deploy/tidb-4000
    11. 172.16.5.139:4000 tidb 172.16.5.139 4000/10080 Up - deploy/tidb-4000
    12. 172.16.5.140:4000 tidb 172.16.5.140 4000/10080 Up - deploy/tidb-4000
    13. 172.16.5.134:20160 tikv 172.16.5.134 20160/20180 Up data/tikv-20160 deploy/tikv-20160
    14. 172.16.5.139:20160 tikv 172.16.5.139 20160/20180 Up data/tikv-20160 deploy/tikv-20160
    1. 执行缩容操作。
    1. [root@localhost ~]# tiup cluster scale-in --help
    2. Scale in a TiDB cluster
    3. Usage:
    4. cluster scale-in <cluster-name> [flags]
    5. Flags:
    6. -h, --help help for scale-in
    7. -N, --node strings Specify the nodes
    8. --transfer-timeout int Timeout in seconds when transferring PD and TiKV store leaders (default 300)
    9. -y, --yes Skip the confirmation of destroying
    10. Global Flags:
    11. --ssh-timeout int Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection. (default 5)