# TiDB 集群的扩展与缩减

## 概述

TiDB采用分布式架构，将计算（SQL层）和存储（TiKV层）分离，便于水平扩展。无需对应用程序造成大规模停机或中断，即可在TiDB集群中添加或删除节点，以调整容量和性能。

  ![56%](https://lab-static.pingcap.com/quick-demo/demo_009_en.png)

### 实验时长

+ 完成此练习大约需要 60 分钟。

### 任务

+ 使用`sysbench`施加测试负载。
+ 在Grafana仪表盘中观察TiDB的性能指标。
+ 水平扩展TiDB的SQL层。
+ 水平扩展TiDB的存储层。

> [!TIP]
> 点击 **开始** 启动实验环境。
> 环境准备就绪后，点击底部工具栏中的 **前进** 进入 **模块 1**。
> 然后，按照实验指南的步骤连接到实践环境并开始你的任务。

## 模块 1: 检查并设置环境

### 步骤 1. 连接至 TiUP 控制机

+ 点击这里 - [终端 1](ssh://monitor1-ssh#1) 打开一个新的终端，连接到为你的实践实验分配的虚拟机。

+ 登录后显示的横幅：

    ```output
     ,     #_
    ~\_  ####_        Amazon Linux 2
    ~~  \_#####\
    ~~     \###|      
    ~~       \#/ ___
    ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
     ~~._.   _/
        _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
      _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
    ```

### 步骤 2. 确认 TiDB 集群状态

+ 通过TiProxy观察集群运行状态，其中包括一个`tidb`服务器和三个`tikv`服务器。

   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    v8.1.0
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://35.91.126.63:2379/dashboard
   Grafana URL:        http://10.90.4.204:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.204:9093   alertmanager  10.90.4.204  9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.204:3000   grafana       10.90.4.204  3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.3.89:2379    pd            10.90.3.89   2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.204:9090   prometheus    10.90.4.204  9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.74:4000    tidb          10.90.1.74   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.1.103:20160  tikv          10.90.1.103  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.30:20160   tikv          10.90.1.30   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.34:20160   tikv          10.90.1.34   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.204:6000   tiproxy       10.90.4.204  6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
   Total nodes: 9
   ```

### 步骤 3. 创建 sysbench 配置

   ```bash
   cat << EOF > sysbench_config
   mysql-host=${HOST_MONITOR1_PRIVATE_IP}
   mysql-port=6000
   mysql-user=root
   mysql-password=
   mysql-db=sbtest
   time=120
   threads=14
   report-interval=5
   EOF
   cat sysbench_config
   ```
   ```output
   mysql-host=10.90.4.204
   mysql-port=6000
   mysql-user=root
   mysql-password=
   mysql-db=sbtest
   time=120
   threads=14
   report-interval=5
   ```

### 步骤 4. 连接TiDB集群并创建空数据库 `sbtest`

   ```bash
   mysql -h ${HOST_MONITOR1_PRIVATE_IP} -P 6000 -uroot
   ```
   ```output
   Welcome to the MySQL monitor.  Commands end with ; or \g.
   Your MySQL connection id is 0
   Server version: 8.0.11-TiDB-v8.1.0 TiDB Server (Apache License 2.0) Community Edition, MySQL 8.0 compatible

   Copyright (c) 2000, 2023, Oracle and/or its affiliates.

   Oracle is a registered trademark of Oracle Corporation and/or its
   affiliates. Other names may be trademarks of their respective
   owners.

   Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

   mysql>
   ```
   ```sql
   CREATE DATABASE sbtest;
   EXIT;
   ```
   ```output
   Query OK, 0 rows affected (0.11 sec)
   Bye
   ```

### 步骤 5. 准备数据

+ 准备`sysbench`数据，需约3分钟完成。
   
   ```bash
   sysbench --config-file=sysbench_config oltp_read_only --tables=6 --table-size=1000000 prepare
   ```
   ```output
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Initializing worker threads...

   Creating table 'sbtest4'...
   Creating table 'sbtest1'...
   Creating table 'sbtest3'...
   Creating table 'sbtest2'...
   Creating table 'sbtest6'...
   Creating table 'sbtest5'...
   Inserting 1000000 records into 'sbtest6'
   Inserting 1000000 records into 'sbtest5'
   Inserting 1000000 records into 'sbtest4'
   Inserting 1000000 records into 'sbtest3'
   Inserting 1000000 records into 'sbtest1'
   Inserting 1000000 records into 'sbtest2'
   Creating a secondary index on 'sbtest6'...
   Creating a secondary index on 'sbtest1'...
   Creating a secondary index on 'sbtest4'...
   Creating a secondary index on 'sbtest2'...
   Creating a secondary index on 'sbtest5'...
   Creating a secondary index on 'sbtest3'...
   ```

## 模块 2: 执行工作负载并观察基线

### 步骤 1. 执行测试工作负载

+ 运行`sysbench`测试工作负载，需2分钟完成。随后在Grafana仪表板中观察`TiDB CPU`指标。默认登陆用户名和密码均为`admin`。

+ 开始`sysbench`工作负载。

   ```bash
   sysbench --config-file=sysbench_config oltp_read_only --tables=6 --table-size=1000000 run
   ```
   ```output
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Running the test with following options:
   Number of threads: 14
   Report intermediate results every 5 second(s)
   Initializing random number generator from current time


   Initializing worker threads...

   Threads started!

   [ 5s ] thds: 14 tps: 1106.04 qps: 17712.85 (r/w/o: 15498.17/0.00/2214.68) lat (ms,95%): 14.73 err/s: 0.00 reconn/s: 0.00
   [ 10s ] thds: 14 tps: 1134.45 qps: 18164.46 (r/w/o: 15895.35/0.00/2269.11) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 15s ] thds: 14 tps: 1127.79 qps: 18036.91 (r/w/o: 15781.72/0.00/2255.19) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 20s ] thds: 14 tps: 1123.20 qps: 17972.54 (r/w/o: 15725.75/0.00/2246.79) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 25s ] thds: 14 tps: 1118.78 qps: 17901.07 (r/w/o: 15663.52/0.00/2237.56) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00
   [ 30s ] thds: 14 tps: 1121.02 qps: 17935.94 (r/w/o: 15693.90/0.00/2242.04) lat (ms,95%): 14.73 err/s: 0.00 reconn/s: 0.00
   [ 35s ] thds: 14 tps: 1121.80 qps: 17946.19 (r/w/o: 15702.99/0.00/2243.20) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00
   [ 40s ] thds: 14 tps: 1132.40 qps: 18117.60 (r/w/o: 15852.60/0.00/2265.00) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 45s ] thds: 14 tps: 1125.60 qps: 18013.78 (r/w/o: 15762.38/0.00/2251.40) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 50s ] thds: 14 tps: 1127.81 qps: 18038.28 (r/w/o: 15782.87/0.00/2255.41) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 55s ] thds: 14 tps: 1138.60 qps: 18227.42 (r/w/o: 15950.02/0.00/2277.40) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 60s ] thds: 14 tps: 1132.79 qps: 18117.01 (r/w/o: 15851.64/0.00/2265.38) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 65s ] thds: 14 tps: 1125.40 qps: 18010.87 (r/w/o: 15760.06/0.00/2250.81) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 70s ] thds: 14 tps: 1129.00 qps: 18057.99 (r/w/o: 15799.99/0.00/2258.00) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 75s ] thds: 14 tps: 1121.60 qps: 17951.22 (r/w/o: 15708.02/0.00/2243.20) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 80s ] thds: 14 tps: 1127.20 qps: 18032.66 (r/w/o: 15778.25/0.00/2254.41) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 85s ] thds: 14 tps: 1127.99 qps: 18047.72 (r/w/o: 15791.93/0.00/2255.79) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 90s ] thds: 14 tps: 1124.00 qps: 17982.04 (r/w/o: 15733.83/0.00/2248.20) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00
   [ 95s ] thds: 14 tps: 1131.20 qps: 18100.34 (r/w/o: 15837.94/0.00/2262.39) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 100s ] thds: 14 tps: 1133.19 qps: 18137.49 (r/w/o: 15870.90/0.00/2266.59) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 105s ] thds: 14 tps: 1132.61 qps: 18120.69 (r/w/o: 15855.48/0.00/2265.21) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 110s ] thds: 14 tps: 1127.39 qps: 18036.31 (r/w/o: 15781.72/0.00/2254.59) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 115s ] thds: 14 tps: 1121.41 qps: 17944.32 (r/w/o: 15701.70/0.00/2242.61) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 120s ] thds: 14 tps: 1126.40 qps: 18023.22 (r/w/o: 15770.02/0.00/2253.20) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   SQL statistics:
       queries performed:
           read:                            1892856
           write:                           0
           other:                           270408
           total:                           2163264
       transactions:                        135204 (1126.59 per sec.)
       queries:                             2163264 (18025.46 per sec.)
       ignored errors:                      0      (0.00 per sec.)
       reconnects:                          0      (0.00 per sec.)

   General statistics:
       total time:                          120.0103s
       total number of events:              135204

   Latency (ms):
            min:                                    9.71
            avg:                                   12.42
            max:                                  226.04
            95th percentile:                       14.21
            sum:                              1679909.05

   Threads fairness:
       events (avg/stddev):           9657.4286/184.44
       execution time (avg/stddev):   119.9935/0.00
   ```

### 步骤 2. 观察基线

+ 登录Grafana仪表盘，进入`Home`，然后选择`tidb-demo`，接着选择`tidb-demo-Performance-Overview`。在`Performance Overview`下，向下滚动至`TiDB CPU`面板，注意观察由`sysbench`工作负载产生的指标。
 
  ![56](https://lab-static.pingcap.com/quick-demo/demo-009-status-01.png)

## 模块 3: 扩展 SQL 层

+ 在集群中添加一个TiDB服务器，扩展SQL层。重新执行相同的工作负载，比较`TiDB CPU`消耗，与之前的步骤相比，观察其比例降低。

### 步骤 1. 检查 TiDB 服务器的扩展设置
   
   ```bash
   cat scale-out-tidb.yaml
   ```
   ```output
   tidb_servers:
     - host: 10.90.1.48
       port: 4000
       status_port: 10080
       deploy_dir: /tidb-deploy/tidb-4000
       log_dir: /tidb-deploy/tidb-4000/log
   ```

### 步骤 2. 添加一个 TiDB 服务器到现有集群

   ```bash
   ~/.tiup/bin/tiup cluster scale-out tidb-demo scale-out-tidb.yaml --user ec2-user -i ~/.ssh/pe-class-key-oregon.pem -y
   ```
   ```output
   ...
   Scaled cluster `tidb-demo` out successfully
   ```

### 步骤 3. 确认 TiDB 集群状态

+ 确保 TiDB 集群中有两个`tidb`服务器。SQL 层的规模从1扩展至2。
   
   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    v8.1.0
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://54.148.94.44:2379/dashboard
   Grafana URL:        http://10.90.4.172:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.172:9093   alertmanager  10.90.4.172  9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.172:3000   grafana       10.90.4.172  3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.3.139:2379   pd            10.90.3.139  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.172:9090   prometheus    10.90.4.172  9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.2.230:4000   tidb          10.90.2.230  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.2.75:4000    tidb          10.90.2.75   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.3.102:20160  tikv          10.90.3.102  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.171:20160  tikv          10.90.3.171  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.56:20160   tikv          10.90.3.56   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.172:6000   tiproxy       10.90.4.172  6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
   Total nodes: 10
   ```

### 步骤 4. 重新执行相同的`sysbench`测试工作负载，需2分钟完成。
   
   ```bash
   sysbench --config-file=sysbench_config oltp_read_only --tables=6 --table-size=1000000 run
   ```
   ```output
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Running the test with following options:
   Number of threads: 14
   Report intermediate results every 5 second(s)
   Initializing random number generator from current time


   Initializing worker threads...

   Threads started!

   [ 5s ] thds: 14 tps: 1138.22 qps: 18237.17 (r/w/o: 15958.53/0.00/2278.65) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 10s ] thds: 14 tps: 1128.66 qps: 18053.90 (r/w/o: 15796.19/0.00/2257.71) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 15s ] thds: 14 tps: 1146.20 qps: 18341.64 (r/w/o: 16049.03/0.00/2292.60) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 20s ] thds: 14 tps: 1133.40 qps: 18125.81 (r/w/o: 15859.41/0.00/2266.40) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 25s ] thds: 14 tps: 1142.40 qps: 18288.81 (r/w/o: 16004.01/0.00/2284.80) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 30s ] thds: 14 tps: 1138.60 qps: 18210.79 (r/w/o: 15933.19/0.00/2277.60) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 35s ] thds: 14 tps: 1147.39 qps: 18363.10 (r/w/o: 16068.31/0.00/2294.79) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 40s ] thds: 14 tps: 1138.80 qps: 18220.87 (r/w/o: 15943.26/0.00/2277.61) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 45s ] thds: 14 tps: 1137.80 qps: 18204.20 (r/w/o: 15928.80/0.00/2275.40) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
   [ 50s ] thds: 14 tps: 1149.21 qps: 18380.90 (r/w/o: 16082.68/0.00/2298.21) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 55s ] thds: 14 tps: 1138.80 qps: 18227.03 (r/w/o: 15949.23/0.00/2277.80) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 60s ] thds: 14 tps: 1144.20 qps: 18305.17 (r/w/o: 16016.77/0.00/2288.40) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 65s ] thds: 14 tps: 1150.61 qps: 18409.75 (r/w/o: 16108.93/0.00/2300.82) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 70s ] thds: 14 tps: 1146.38 qps: 18348.26 (r/w/o: 16054.91/0.00/2293.36) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 75s ] thds: 14 tps: 1146.61 qps: 18336.94 (r/w/o: 16043.92/0.00/2293.02) lat (ms,95%): 13.70 err/s: 0.00 reconn/s: 0.00
   [ 80s ] thds: 14 tps: 1140.80 qps: 18264.44 (r/w/o: 15982.63/0.00/2281.80) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 85s ] thds: 14 tps: 1140.60 qps: 18242.54 (r/w/o: 15961.55/0.00/2280.99) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 90s ] thds: 14 tps: 1141.40 qps: 18260.07 (r/w/o: 15977.06/0.00/2283.01) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 95s ] thds: 14 tps: 1148.80 qps: 18390.78 (r/w/o: 16093.18/0.00/2297.60) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 100s ] thds: 14 tps: 1136.80 qps: 18181.97 (r/w/o: 15908.38/0.00/2273.60) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 105s ] thds: 14 tps: 1146.60 qps: 18341.07 (r/w/o: 16048.46/0.00/2292.61) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 110s ] thds: 14 tps: 1142.60 qps: 18286.77 (r/w/o: 16001.37/0.00/2285.40) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   [ 115s ] thds: 14 tps: 1145.40 qps: 18321.19 (r/w/o: 16030.00/0.00/2291.20) lat (ms,95%): 13.70 err/s: 0.00 reconn/s: 0.00
   [ 120s ] thds: 14 tps: 1147.79 qps: 18365.31 (r/w/o: 16069.92/0.00/2295.39) lat (ms,95%): 13.95 err/s: 0.00 reconn/s: 0.00
   SQL statistics:
       queries performed:
           read:                            1919484
           write:                           0
           other:                           274212
           total:                           2193696
       transactions:                        137106 (1142.42 per sec.)
       queries:                             2193696 (18278.68 per sec.)
       ignored errors:                      0      (0.00 per sec.)
       reconnects:                          0      (0.00 per sec.)

   General statistics:
       total time:                          120.0126s
       total number of events:              137106

   Latency (ms):
            min:                                    9.26
            avg:                                   12.25
            max:                                  224.92
            95th percentile:                       13.95
            sum:                              1679939.18

   Threads fairness:
       events (avg/stddev):           9793.2857/474.34
       execution time (avg/stddev):   119.9957/0.00
   ```

### 步骤 5. 观察改变的指标

+ 再次从Grafana仪表板观察`TiDB CPU`指标，并点击面板中的`avg`角标。因为SQL层现在有两个`tidb`服务器，所以与先前的步骤相比，平均`TiDB CPU`消耗比例相应减少，单个`tidb`服务器的平均CPU消耗减至配置前的50%。

  ![56](https://lab-static.pingcap.com/quick-demo/demo-009-status-02.png)

## 模块 4: 扩展存储层

+ 在执行`sysbench`工作负载时对存储层进行扩展。
   
### 步骤 1. 打开新的终端窗口(Terminal 2)，并登陆至 monitor1_address

+ 点击这里 - [终端 2](ssh://monitor1-ssh#2) 打开一个新的终端，连接到为你的实践实验分配的虚拟机。

+ 登录后显示的横幅：

    ```output
     ,     #_
    ~\_  ####_        Amazon Linux 2
    ~~  \_#####\
    ~~     \###|      
    ~~       \#/ ___
    ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
     ~~._.   _/
        _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
      _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/
    ```

### 步骤 2. 执行测试工作负载

+ [终端 2](ssh://monitor1-ssh#2) 中，执行`sysbench`工作负载。在此过程中无需等待完成，继续执行接下来的步骤。

   ```bash
   sysbench --config-file=sysbench_config oltp_read_only --tables=6 --table-size=1000000 run
   ```
   ```output
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Running the test with following options:
   Number of threads: 14
   Report intermediate results every 5 second(s)
   Initializing random number generator from current time

   Initializing worker threads...

   Threads started!
   ...
   ```

### 步骤 3. 添加一个TiKV服务器到集群

+ 返回[终端 1](ssh://monitor1-ssh#1)，扩展存储层，添加一个TiKV服务器。
   
   ```bash
   cat scale-out-tikv.yaml
   ```
   ```output
   tikv_servers:
     - host: 10.90.1.245
       port: 20160
       status_port: 20180
   ```
   ```bash
   ~/.tiup/bin/tiup cluster scale-out tidb-demo scale-out-tikv.yaml --user ec2-user -i ~/.ssh/pe-class-key-oregon.pem -y
   ```
   ```output
   ...
   Scaled cluster `tidb-demo` out successfully
   ```

### 步骤 4. 确认 TiDB 集群状态

+ 确保 TiDB 集群中有四个`tikv`服务器。TiKV 层的规模从3扩展至4。

   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    v8.1.0
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://35.91.126.63:2379/dashboard
   Grafana URL:        http://10.90.4.204:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.204:9093   alertmanager  10.90.4.204  9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.204:3000   grafana       10.90.4.204  3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.3.89:2379    pd            10.90.3.89   2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.204:9090   prometheus    10.90.4.204  9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.48:4000    tidb          10.90.1.48   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.1.74:4000    tidb          10.90.1.74   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.1.103:20160  tikv          10.90.1.103  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.245:20160  tikv          10.90.1.245  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.30:20160   tikv          10.90.1.30   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.1.34:20160   tikv          10.90.1.34   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.204:6000   tiproxy       10.90.4.204  6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
   Total nodes: 11
   ```

## 模块 5: 缩减SQL层

+ 在`sysbench`工作负载运行期间缩减SQL层。

### 步骤 1. 执行测试工作负载

+ 切回[终端 2](ssh://monitor1-ssh#2) 并再次执行`sysbench`工作负载。在工作负载进行时继续执行下一步操作，无需等待其完成。
   
   ```bash
   sysbench --config-file=sysbench_config oltp_read_only --tables=6 --table-size=1000000 run
   ```
   ```output
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Running the test with following options:
   Number of threads: 14
   Report intermediate results every 5 second(s)
   Initializing random number generator from current time

   Initializing worker threads...

   Threads started!
   ...
   ```

### 步骤 2. 从集群中删除一个 TiDB 服务器

+ 在[终端 1](ssh://monitor1-ssh#1) 中，随机删除一个TiDB服务器，缩减SQL层。
   
   ```bash
   RANDOM_TIDB_NODE=`~/.tiup/bin/tiup cluster display tidb-demo | grep 4000 | sed 's/ .*$//' | head -n 1`
   ~/.tiup/bin/tiup cluster scale-in tidb-demo --node ${RANDOM_TIDB_NODE} -y
   ```
   ```output
   ...
   Scaled cluster `tidb-demo` in successfully
   ```

### 步骤 4. 确认 TiDB 集群的状态

+ 确保TiDB集群中有一个`tidb`服务器。SQL层的规模从2缩减至1。

   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    v8.1.0
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://54.185.182.184:2379/dashboard
   Grafana URL:        http://10.90.4.216:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.216:9093   alertmanager  10.90.4.216  9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.216:3000   grafana       10.90.4.216  3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.3.191:2379   pd            10.90.3.191  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.216:9090   prometheus    10.90.4.216  9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.16:4000    tidb          10.90.1.16   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.3.112:20160  tikv          10.90.3.112  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.213:20160  tikv          10.90.3.213  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.40:20160   tikv          10.90.3.40   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.71:20160   tikv          10.90.3.71   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.216:6000   tiproxy       10.90.4.216  6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
   Total nodes: 10
   ```

## 模块 6: 缩减存储层

+ 在`sysbench`工作负载进行期间缩减存储层。
   
### 步骤 1. 执行测试工作负载

+ 切换至[终端 2](ssh://monitor1-ssh#2)，再次执行`sysbench`工作负载。在工作负载进行期间，继续执行后续步骤，无需等待其完成。
   
   ```bash
   sysbench --config-file=sysbench_config oltp_read_only --tables=6 --table-size=1000000 run
   ```
   ```output
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Running the test with following options:
   Number of threads: 14
   Report intermediate results every 5 second(s)
   Initializing random number generator from current time

   Initializing worker threads...

   Threads started!
   ...
   ```

### 步骤 2. 从集群中删除一个 TiKV 服务器

+ 在[终端 1](ssh://monitor1-ssh#1) 中，随机删除一个TiKV服务器，缩减存储层。
   
   ```bash
   RANDOM_TIKV_NODE=`~/.tiup/bin/tiup cluster display tidb-demo | grep 20160 | sed 's/ .*$//' | head -n 1`
   ~/.tiup/bin/tiup cluster scale-in tidb-demo --node ${RANDOM_TIKV_NODE} -y
   ```
   ```output
   ...
   Scaled cluster `tidb-demo` in successfully
   ```

+ 销毁已删除的TiKV节点。

   ```bash
   ~/.tiup/bin/tiup cluster prune tidb-demo -y
   ```
   ```output
   ...
   Destroy success
   ```

### 步骤 3. 确认 TiDB 集群状态

+ 确保TiDB集群中有三个`tikv`服务器。存储层的规模从4降至3。
  
   ```bash
   ~/.tiup/bin/tiup cluster display tidb-demo
   ```
   ```output
   Cluster type:       tidb
   Cluster name:       tidb-demo
   Cluster version:    v8.1.0
   Deploy user:        ec2-user
   SSH type:           builtin
   Dashboard URL:      http://54.185.182.184:2379/dashboard
   Grafana URL:        http://10.90.4.216:3000
   ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
   --                 ----          ----         -----        -------       ------  --------                      ----------
   10.90.4.216:9093   alertmanager  10.90.4.216  9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.216:3000   grafana       10.90.4.216  3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
   10.90.3.191:2379   pd            10.90.3.191  2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.216:9090   prometheus    10.90.4.216  9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.16:4000    tidb          10.90.1.16   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
   10.90.3.213:20160  tikv          10.90.3.213  20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.40:20160   tikv          10.90.3.40   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.71:20160   tikv          10.90.3.71   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.216:6000   tiproxy       10.90.4.216  6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
   Total nodes: 9
   ```

## 结论

TiDB集群的SQL层（TiDB服务器）和存储层（TiKV服务器）可以根据需要独立进行扩展/缩减。该过程对于应用程序的操作和维护团队来说是无缝的，尤其是在TiDB集群前有负载均衡器的情况下更为有效。在我们的实验室中，TiProxy有效地承担了这一角色。在水平扩展过程中，您的应用程序会继续运行。

TiDB在计算层最大支持512个节点，每个节点可处理最多1000个并发连接。

TiDB服务器和TiKV服务器有所不同。TiDB服务器是无状态的，而TiKV服务器是有状态的。为更好地利用TiDB的伸缩性，您可以参加[TiDB培训和认证](https://tidb.io/education)提供的课程，进一步了解最佳实践。