# 在线扩容（TiDB/TiKV/PD）

__*TiDB 在线扩容*__
__*TiDB Server 在线扩容*__
__*TiKV 在线扩容*__
__*PD 在线扩容*__

+ 第一步:编辑扩容配置文件（ scale-out topology ）
```
vi scale-out.yaml

 - host: 10.0.1.5
    ssh_port: 22
    port: 4000
    status_port: 10080
    deploy_dir: /data/deploy/install/deploy/tidb-4000
    log_dir: /data/deploy/install/log/tidb-4000
```
+ 第二步:运行扩容命令
  + `tiup cluster scale-out <cluster-name> scale-out.yaml`

+ 第三步:确认新节点是否加入
  + `tiup cluster display <cluster-name>`

-------------------------------------------------------------
TiDB 集群可以在不中断线上服务的情况下进行扩容。通过使用 TiUP 扩容集群中的 TiDB、TiKV、PD 或者 TiFlash 节点。如果没有安装 TiUP,请参考 《 TiDB Cluster 部署 》课程。

由于 TiDB / PD / TiKV 的扩容方法相似,这里,我们以向集群中扩容一个 IP 地址为:10.0.1.5 的节点为例进行说明。

第一步:编辑扩容配置文件（ scale-out topology ）

vi scale-out.yaml

tidb_servers 的扩容配置文件为:
tidb_servers:
  - host: 10.0.1.5
    ssh_port: 22
    port: 4000
    status_port: 10080
    deploy_dir: /data/deploy/install/deploy/tidb-4000
    log_dir: /data/deploy/install/log/tidb-4000

tikv_servers 的扩容配置文件为:
tikv_servers:
  - host: 10.0.1.5
    ssh_port: 22
    port: 20160
    status_port: 20180
    deploy_dir: /data/deploy/install/deploy/tikv-20160
    data_dir: /data/deploy/install/data/tikv-20160
    log_dir: /data/deploy/install/log/tikv-20160

pd_servers 的扩容配置文件为:
pd_servers:
  - host: 10.0.1.5
    ssh_port: 22
    name: pd-1
    client_port: 2379
    peer_port: 2380
    deploy_dir: /data/deploy/install/deploy/pd-2379
    data_dir: /data/deploy/install/data/pd-2379
    log_dir: /data/deploy/install/log/pd-2379

可以使用 tiup cluster edit-config <cluster-name> 查看当前集群的配置信息,因为其中的 global 和 server_configs 参数配置默认会被 scale-out.yaml 继承,因此也会在 scale-out.yaml 中生效。

第二步:运行扩容命令

执行 scale-out 命令前,先使用 check 及 check --apply 命令,检查和自动修复集群存在的潜在风险:
（1）检查集群存在的潜在风险:
tiup cluster check <cluster-name> scale-out.yaml --cluster --user root [-p] [-i /home/root/.ssh/gcp_rsa]

（2）自动修复集群存在的潜在风险:
tiup cluster check <cluster-name> scale-out.yaml --cluster --apply --user root [-p] [-i /home/root/.ssh/gcp_rsa]

运行扩容命令:
tiup cluster scale-out <cluster-name> scale-out.yaml

执行成功后会出现:"scaled cluster <cluster-name> out successfully"表示扩容成功。至于 TiKV集群会自动进行数据平衡（rebalance）。

第三步:确认新节点是否加入

检查集群状态确认新节点加入,如下:
tiup cluster display <cluster-name>

请参考文档:https://docs.pingcap.com/zh/tidb/stable/scale-tidb-using-tiup