# Operating TiDB Cluster

### 概要
+ In these exercises, you scale the TiKV cluster using TiUP and change the name of TiDB cluster.
+ The following shows the deployment topology diagram of TiDB cluster.

   ![60](https://pingcap-edu.s3.us-west-2.amazonaws.com/quick-demo/TiDBClusterDeploymentTopologyExercise_aa_007_20220914.png)

### 注意
+ Click the private key file [pe-class-key-oregon.pem](lab-controller/class_303_get_ec2_key_for_lab.php?region=oregon) to download it. It is required to log into the Control Machine for this demo.
+ Commands and SQL statements presented on this page are clickable for easy copy and paste.
+ The required IP addresses for specific commands will be substituted with the actual ones automatically once you have set up the lab environment after Step 1.
+ Your environment may be different with these exercises, including but not limited to IP, port, username, password and directory. Example output is displayed in a gray background in this guide.
+ The `$` in your terminal window is an OS prompt. The `tidb>` or `mysql>` in your terminal window is SQL command prompt.

### 前提条件
+ You have downloaded and secured the TiUP Control Machine private key: `chmod 400 [pe-class-key-oregon.pem](lab-controller/class_303_get_ec2_key_for_lab.php?region=oregon)`
+ The `ssh-agent` is running on your laptop. Windows users please refer to this [guide](https://github.com/pingcap/tidb-course-201-lab/blob/master/README_303_v65_en_class_setup_guide_for_student.md).
+ You have added identity to authentication agent: `ssh-add [pe-class-key-oregon.pem](lab-controller/class_303_get_ec2_key_for_lab.php?region=oregon)`

## 演習 1
### Scaling out a TiKV Cluster

### 概要
+ In this exercise, you add a TiDB node to the `monitor1` node. You will perform all tasks on Control Machine (monitor1_address).

### 所要時間
+ This exercise should take you approximately 20 minutes to complete.

### タスク
+ Prepare a yaml file called `scale-out-tikv.yaml` for scaling out TiKV.
+ Scale out the TiKV.

### 手順
1. Login as user ec2-user on monitor1_address. Enter the following command at the terminal prompt and receive the results shown. The returned message may be slightly different from what is displayed here:
   ```command
   $ ssh -A ec2-user@monitor1_address
   ```
   ```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. Use a text editor to edit the `scale-out-tikv.yaml` file. Modify `scale-out-tikv.yaml` file to contain settings similar to those that follow (replace the IP addresses with the correct ones assigned to you). Press the <kbd>ESC</kbd> and type `:wq` to quit to the terminal:
   ```command
   $ vi scale-out-tikv.yaml
   ```
   ```output
   tikv_servers:
     - host: <HOST_MONITOR1_PRIVATE_IP>
       ssh_port: 22
       port: 20160
       status_port: 20180
       deploy_dir: /tidb-deploy/tikv-20160
       data_dir: /tidb-data/tikv-20160
       log_dir: /tidb-deploy/tikv-20160/log
   ```

3. Run the `tiup cluster scale-out` command to scale out TiKV cluster. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster scale-out tidb-test scale-out-tikv.yaml
   ```
   ``` output
   ...
   Do you want to continue? [y/N]: (default=N) y
   ...
   Scaled cluster `tidb-test` out successfully
   ```

4. Check the status of the deployed TiDB cluster and the new TiKV node (IP address: `HOST_MONITOR1_PRIVATE_IP`). There are 4 TiKV nodes now. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster display tidb-test
   ```
   ```output
   tiup is checking updates for component cluster ...
   Starting component `cluster`: /home/ec2-user/.tiup/components/cluster/v1.12.1/tiup-cluster display tidb-test
   Cluster type:       tidb
   Cluster name:       tidb-test
   Cluster version:    v6.5.0
   Deploy user:        tidb
   SSH type:           builtin
   Dashboard URL:      http://10.90.1.122:2379/dashboard
   Grafana URL:        http://10.90.4.214:3000
   ID                 Role          Host         Ports                            OS/Arch       Status   Data Dir                      Deploy Dir
   --                 ----          ----         -----                            -------       ------   --------                      ----------
   10.90.4.214:9093   alertmanager  10.90.4.214  9093/9094                        linux/x86_64  Up       /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.214:3000   grafana       10.90.4.214  3000                             linux/x86_64  Up       -                             /tidb-deploy/grafana-3000
   10.90.1.122:2379   pd            10.90.1.122  2379/2380                        linux/x86_64  Up|L|UI  /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.2.243:2379   pd            10.90.2.243  2379/2380                        linux/x86_64  Up       /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.3.25:2379    pd            10.90.3.25   2379/2380                        linux/x86_64  Up       /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.214:9090   prometheus    10.90.4.214  9090/12020                       linux/x86_64  Up       /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.133:4000   tidb          10.90.1.133  4000/10080                       linux/x86_64  Up       -                             /tidb-deploy/tidb-4000
   10.90.2.92:4000    tidb          10.90.2.92   4000/10080                       linux/x86_64  Up       -                             /tidb-deploy/tidb-4000
   10.90.4.103:9000   tiflash       10.90.4.103  9000/8123/3930/20170/20292/8234  linux/x86_64  Up       /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
   10.90.1.98:20160   tikv          10.90.1.98   20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.2.235:20160  tikv          10.90.2.235  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.148:20160  tikv          10.90.3.148  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.4.214:20160  tikv          10.90.4.214  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   Total nodes: 13
   ```


## 演習 2
### Scaling in a TiKV Cluster

### 概要
+ In this exercise, you remove a TiDB node from the `monitor1` node. You will perform all tasks on the Control Machine (monitor1_address).

### 所要時間
+ This exercise should take you approximately 20 minutes to complete.

### タスク
+ Scale in the TiKV cluster.
+ Delete the stopped TiKV node.

### 手順
1. Run the `tiup cluster scale-in` command to scale in TiKV cluster. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster scale-in tidb-test --node ${HOST_MONITOR1_PRIVATE_IP}:20160
   ```
   ```output
   ...
   Do you want to continue? [y/N]:(default=N) y
   The component `[tikv]` will become tombstone, maybe exists in several minutes or hours, after that you can use the prune command to clean it
   Do you want to continue? [y/N]:(default=N) y
   ...
   Scaled cluster `tidb-test` in successfully
   ```

2. Check the status of the deployed TiDB cluster. Observe that the status of the TiKV node (IP address: `HOST_MONITOR1_PRIVATE_IP`) has changed to `Tombstone`.
   ```command
   $ tiup cluster display tidb-test
   ```
   ```output
   tiup is checking updates for component cluster ...
   ...
   ID                 Role          Host         Ports                            OS/Arch       Status     Data Dir                      Deploy Dir
   --                 ----          ----         -----                            -------       ------     --------                      ----------
   ...
   10.90.4.214:20160  tikv          10.90.4.214  20160/20180                      linux/x86_64  Tombstone  /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   Total nodes: 13
   There are some nodes can be pruned: 
   	Nodes: [10.90.4.214:20160]
   	You can destroy them with the command: `tiup cluster prune tidb-test`
   ```

3. Execute the `tiup cluster prune tidb-test` to clean up node data. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster prune tidb-test
   ```
   ```output
   ...
   Do you confirm this action? [y/N]:(default=N) y
   ...
   Destroy success
   ```

4. Check the status of the deployed TiDB cluster, confirm that the TiKV node (IP address: `HOST_MONITOR1_PRIVATE_IP`) no longer exists. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster display tidb-test
   ```
   ```output
   tiup is checking updates for component cluster ...
   Starting component `cluster`: /home/ec2-user/.tiup/components/cluster/v1.12.1/tiup-cluster display tidb-test
   Cluster type:       tidb
   Cluster name:       tidb-test
   Cluster version:    v6.5.0
   Deploy user:        tidb
   SSH type:           builtin
   Dashboard URL:      http://10.90.1.122:2379/dashboard
   Grafana URL:        http://10.90.4.214:3000
   ID                 Role          Host         Ports                            OS/Arch       Status   Data Dir                      Deploy Dir
   --                 ----          ----         -----                            -------       ------   --------                      ----------
   10.90.4.214:9093   alertmanager  10.90.4.214  9093/9094                        linux/x86_64  Up       /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
   10.90.4.214:3000   grafana       10.90.4.214  3000                             linux/x86_64  Up       -                             /tidb-deploy/grafana-3000
   10.90.1.122:2379   pd            10.90.1.122  2379/2380                        linux/x86_64  Up|L|UI  /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.2.243:2379   pd            10.90.2.243  2379/2380                        linux/x86_64  Up       /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.3.25:2379    pd            10.90.3.25   2379/2380                        linux/x86_64  Up       /tidb-data/pd-2379            /tidb-deploy/pd-2379
   10.90.4.214:9090   prometheus    10.90.4.214  9090/12020                       linux/x86_64  Up       /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
   10.90.1.133:4000   tidb          10.90.1.133  4000/10080                       linux/x86_64  Up       -                             /tidb-deploy/tidb-4000
   10.90.2.92:4000    tidb          10.90.2.92   4000/10080                       linux/x86_64  Up       -                             /tidb-deploy/tidb-4000
   10.90.4.103:9000   tiflash       10.90.4.103  9000/8123/3930/20170/20292/8234  linux/x86_64  Up       /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
   10.90.1.98:20160   tikv          10.90.1.98   20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.2.235:20160  tikv          10.90.2.235  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   10.90.3.148:20160  tikv          10.90.3.148  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
   Total nodes: 12
   ```

## 演習 3 
### Renaming Cluster

### 概要
+ In this exercise, you rename the TiDB cluster.

### 所要時間
+ This exercise should take you approximately 10 minutes to complete. You will perform all tasks on the Control Machine (monitor1_address).

### タスク
+ Change the name of TiDB cluster to `tidb-prod`.
+ Check the status of the deployed TiDB cluster and view the name of TiDB cluster.
+ Change the name of TiDB cluster back to `tidb-test`.
+ Check the status of the deployed TiDB cluster and view the name of TiDB cluster.
+ Close all open terminal windows.

### 手順
1. Change the name of TiDB cluster to `tidb-prod`. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster rename tidb-test tidb-prod
   ```
   ```output
   ...
   Do you confirm this action? [y/N]:(default=N) y
   ...
   Do you want to continue? [y/N]:(default=N) y
   ...
   Reloaded cluster `tidb-prod` successfully
   ```

2. Check the status of the deployed TiDB cluster and view the name of TiDB cluster. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster display tidb-prod
   ```
   ```output
   tiup is checking updates for component cluster ...
   Starting component `cluster`: /home/ec2-user/.tiup/components/cluster/v1.12.1/tiup-cluster display tidb-prod
   Cluster type:       tidb
   Cluster name:       tidb-prod
   ...
   ```

3. Change the name of TiDB cluster back to `tidb-test`. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster rename tidb-prod tidb-test
   ```
   ```output
   ...
   Do you confirm this action? [y/N]:(default=N) y
   ...
   Do you want to continue? [y/N]:(default=N) y
   ...
   Reloaded cluster `tidb-test` successfully
   ```

4. Check the status of the deployed TiDB cluster and view the name of TiDB cluster. Enter the following command at the terminal prompt and receive the results shown:
   ```command
   $ tiup cluster display tidb-test
   ```
   ```output
   tiup is checking updates for component cluster ...
   Starting component `cluster`: /home/ec2-user/.tiup/components/cluster/v1.12.1/tiup-cluster display tidb-prod
   Cluster type:       tidb
   Cluster name:       tidb-test
   ...
   ```

5. Close all open terminal windows.

## 演習終わり