## 模块 1：安装 TiUP 并熟悉实验环境

### 步骤 1. 连接到远程 Linux 机器

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

### 步骤 2. 获取实验资源信息

+ 保持在 `ec2-user` 主目录下，查看 `hosts-env.sh` 文件的内容以获取 IP 地址信息。你的输出可能与 `sample_output` 不同：

   ```bash
   cat hosts-env.sh
   ```

   ```output
   export HOST_MONITOR1_PRIVATE_IP=10.90.4.19
   export HOST_MONITOR1_PUBLIC_IP=44.234.33.106
   export HOST_CM_PRIVATE_IP=10.90.4.19
   export HOST_CM_PUBLIC_IP=44.234.33.106
   export HOST_PD1_PRIVATE_IP=10.90.1.197
   export HOST_PD1_PUBLIC_IP=18.236.76.37
   export HOST_PD2_PRIVATE_IP=10.90.3.218
   export HOST_PD2_PUBLIC_IP=18.237.226.109
   export HOST_PD3_PRIVATE_IP=10.90.2.84
   export HOST_PD3_PUBLIC_IP=18.237.105.199
   export HOST_DB1_PRIVATE_IP=10.90.3.216
   export HOST_DB1_PUBLIC_IP=34.222.212.46
   export HOST_DB2_PRIVATE_IP=10.90.2.245
   export HOST_DB2_PUBLIC_IP=54.188.31.139
   export HOST_KV1_PRIVATE_IP=10.90.1.94
   export HOST_KV1_PUBLIC_IP=35.87.137.198
   export HOST_KV2_PRIVATE_IP=10.90.2.38
   export HOST_KV2_PUBLIC_IP=35.91.214.228
   export HOST_KV3_PRIVATE_IP=10.90.3.112
   export HOST_KV3_PUBLIC_IP=54.184.160.176
   export HOST_TIFLASH1_PRIVATE_IP=10.90.4.28
   export HOST_TIFLASH1_PUBLIC_IP=34.223.94.92
   ```

+ 在你连接的 Linux 上设置节点 IP 地址变量。

   ```bash
   source ./hosts-env.sh
   ```

+ 你也可以随时点击本实验指南顶部的 <kbd>资源</kbd> 选项卡，以访问所有实验资源信息。

### 步骤 3. 下载并安装 TiUP - TiDB 集群管理工具

+ 在终端提示符下输入以下命令来安装 `tiup` 工具。请注意，`tiup` 在你的环境中可能已预装，也可能未预装。

   ```bash
   curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
   ```

   ```output
   curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
     % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                    Dload  Upload   Total   Spent    Left  Speed
   100 5152k  100 5152k    0     0  14.8M      0 --:--:-- --:--:-- --:--:-- 14.8M
   Successfully set mirror to https://tiup-mirrors.pingcap.com
   Detected shell: bash
   Shell profile:  /home/ec2-user/.bash_profile
   Installed path: /home/ec2-user/.tiup/bin/tiup
   ===============================================
   Have a try:     tiup playground
   ===============================================
   ```

### 步骤 4. 设置 TiUP 环境变量

+ 在终端提示符下输入以下命令。

   ```bash
   source .bash_profile
   ```

### 步骤 5. 将 TiUP 镜像更改为本地文件夹

+ 确保将本地 TiUP 镜像设置为已连接 Linux 上的 `tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64` 文件夹。它已为本次实践实验 [cloned](https://docs.pingcap.com/tidb/stable/production-deployment-using-tiup) 完成。

   ```bash
   tiup mirror set tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
   tiup mirror show
   ```

   ```output
   Successfully set mirror to /home/ec2-user/tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
   /home/ec2-user/tidb-community-server-${LAB:TIDB_VERSION}-linux-amd64
   ```

+ 有关设置本地 TiUP 镜像的更多信息，请参考 [TiUP 文档](https://docs.pingcap.com/zh/tidb/stable/production-deployment-using-tiup)。现在，部署 TiDB 集群的环境已准备就绪，请继续下一步实践。

### 步骤 6. 通过执行 `tiup cluster` 来验证 TiUP 集群组件已安装

+ 在终端提示符下输入以下命令并查看显示的结果。请注意，如果尚未安装， `tiup cluster` 命令可以从配置的 TiUP 镜像中安装 `cluster` 组件。

   ```bash
   tiup cluster
   ```

   ```output
   Checking updates for component cluster... Deploy a TiDB cluster for production

   Usage:
   tiup cluster [command]

   Available Commands:
   check       Perform preflight checks for the cluster.
   deploy      Deploy a cluster for production
   start       Start a TiDB cluster
   stop        Stop a TiDB cluster
   restart     Restart a TiDB cluster
   scale-in    Scale in a TiDB cluster
   scale-out   Scale out a TiDB cluster
   destroy     Destroy a specified cluster
   clean       (EXPERIMENTAL) Cleanup a specified cluster
   upgrade     Upgrade a specified TiDB cluster
   display     Display information of a TiDB cluster
   prune       Destroy and remove instances that is in tombstone state
   list        List all clusters
   audit       Show audit log of cluster operation
   import      Import an exist TiDB cluster from TiDB-Ansible
   edit-config Edit TiDB cluster config
   show-config Show TiDB cluster config
   reload      Reload a TiDB cluster's config and restart if needed
   patch       Replace the remote package with a specified package and restart the service
   rename      Rename the cluster
   enable      Enable a TiDB cluster automatically at boot
   disable     Disable automatic enabling of TiDB clusters at boot
   replay      Replay previous operation and skip successed steps
   template    Print topology template
   tls         Enable/Disable TLS between TiDB components
   meta        backup/restore meta information
   rotatessh   rotate ssh keys on all nodes
   help        Help about any command
   completion  Generate the autocompletion script for the specified shell

   Flags:
   -c, --concurrency int     max number of parallel tasks allowed (default 5)
      --format string       (EXPERIMENTAL) The format of output, available values are [default, json] (default "default")
   -h, --help                help for tiup
      --ssh string          (EXPERIMENTAL) The executor type: 'builtin', 'system', 'none'.
      --ssh-timeout uint    Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection. (default 5)
   -v, --version             version for tiup
      --wait-timeout uint   Timeout in seconds to wait for an operation to complete, ignored for operations that don't fit. (default 120)
   -y, --yes                 Skip all confirmations and assumes 'yes'

   Use "tiup cluster help [command]" for more information about a command.
   ```

### 步骤 7. 查看已为你设置好公/私钥

+ 请注意，在本实验中，公/私钥对由实验环境准备。在实验结束后，你必须在自己的环境中自行使用 `ssh-keygen` 准备公/私钥对。

+ 私钥位于 `~/.ssh` 目录下，且私钥文件的权限为只读。你将使用此私钥来部署 TiDB 集群。

   ```bash
   ls -l /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem
   ```

   ```output
   -r-------- 1 ec2-user ec2-user 1674 May 25 04:09 /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem
   ```

+ 在本实验的每个节点上，对等公钥位于 `~/.ssh/authorized_keys` 文件中，因此你可以无密码通过 SSH 连接到任何节点。

   ```bash
   cat ~/.ssh/authorized_keys | grep ${LAB:REGION_NAME}
   ```

   ```output
   ssh-rsa AAAAB3Nza..........FvuLk3f5/Wsdx7jdNrP pe-class-key-${LAB:REGION_NAME}
   ```
