## 模块 2：部署 TiDB 集群

### 步骤 1. 查看拓扑文件

+ 查看提供的拓扑文件 `solution-topology-ten-nodes.yaml`，它用于部署 TiDB 集群。请注意，`sample_output`中显示的 IP 地址会与你的不同。

   ```bash
   cat solution-topology-ten-nodes.yaml
   ```

   ```output
   global:
     user: "ec2-user"
     ssh_port: 22
     deploy_dir: "/tidb-deploy"
     data_dir: "/tidb-data"
     arch: "amd64"
   server_configs:
     pd:
       replication.location-labels: ["region","zone"]
       replication.max-replicas: 3
   pd_servers:
     - host: 10.90.3.184
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://35.88.150.142:2379"
       config:
         server.labels:
           region: ${LAB:REGION_NAME}
           zone: az1
     - host: 10.90.3.28
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://18.236.135.17:2379"
       config:
         server.labels:
           region: ${LAB:REGION_NAME}
           zone: az1
     - host: 10.90.3.247
       client_port: 2379
       peer_port: 2380
       advertise_client_addr: "http://34.219.57.252:2379"
       config:
         server.labels:
           region: ${LAB:REGION_NAME}
           zone: az1
   tidb_servers:
     - host: 10.90.1.164
       port: 4000
       status_port: 10080
       deploy_dir: /tidb-deploy/tidb-4000
       log_dir: /tidb-deploy/tidb-4000/log
     - host: 10.90.1.14
       port: 4000
       status_port: 10080
       deploy_dir: /tidb-deploy/tidb-4000
       log_dir: /tidb-deploy/tidb-4000/log
   tikv_servers:
     - host: 10.90.3.197
       port: 20160
       status_port: 20180
       config:
         server.labels:
           region: ${LAB:REGION_NAME}
           zone: az1
     - host: 10.90.3.39
       port: 20160
       status_port: 20180
       config:
         server.labels:
           region: ${LAB:REGION_NAME}
           zone: az1
     - host: 10.90.3.82
       port: 20160
       status_port: 20180
       config:
         server.labels:
           region: ${LAB:REGION_NAME}
           zone: az1
   tiflash_servers:
     - host: 10.90.4.194
       tcp_port: 9000
       flash_service_port: 3930
       deploy_dir: /tidb-deploy/tiflash-9000
       data_dir: /tidb-data/tiflash-9000
       log_dir: /tidb-deploy/tiflash-9000/log
   monitoring_servers:
     - host: 10.90.4.156
   grafana_servers:
     - host: 10.90.4.156
   alertmanager_servers:
     - host: 10.90.4.156
   ```

### 步骤 2. 安装所需软件包

+ 确保在所有集群节点上安装 `numactl`在课堂环境中该任务应已完成，所以无需在所有节点上实际执行安装）。例如，在终端提示符下输入以下命令在 `TiKV server 1` 上安装 `numactl` （如果提示进行 SSH 连接，输入 `yes`）：

   ```bash
   ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP}
   ```

   ```bash
   sudo yum -y install numactl
   ```

   ```output
   Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
   amzn2-core                                                                                                                                 | 3.6 kB  00:00:00
   60 packages excluded due to repository priority protections
   Package numactl-2.0.9-7.amzn2.x86_64 already installed and latest version
   Nothing to do
   ```

+ 从 `TiKV server 1` 注销并返回 TiUP 控制机的提示符：

   ```bash
   exit
   ```

### 步骤 3. 使用 `tiup cluster check` 检查系统要求

+ 检查系统设置以满足集群部署前提条件。在终端提示符下输入以下命令并查看显示的结果。你会发现有些项目未通过系统检查。我们将在下一步解决此问题。

   ```bash
   tiup cluster check ./solution-topology-ten-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem
   ```

   ```output
   + Detect CPU Arch Name
     - Detecting node 10.90.2.90 Arch info ... ⠙ Shell: host=10.90.2.90, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.231 Arch info ... ⠙ Shell: host=10.90.2.231, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.138 Arch info ... ⠙ Shell: host=10.90.2.138, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.242 Arch info ... ⠙ Shell: host=10.90.2.242, sudo=false, command=`uname -m`
   ...
   10.90.2.97   sysctl        Fail    fs.file-max = 376482, should be greater than 1000000
   10.90.2.97   sysctl        Fail    net.core.somaxconn = 4096, should be greater than 32768
   10.90.2.97   sysctl        Fail    net.ipv4.tcp_syncookies = 1, should be 0
   10.90.2.97   sysctl        Fail    vm.swappiness = 60, should be 0
   10.90.2.97   service       Pass    service firewalld not found, ignore
   10.90.2.97   command       Pass    numactl: policy: default
   10.90.2.97   timezone      Pass    time zone is the same as the first PD machine: UTC
   10.90.2.97   os-version    Pass    OS is Amazon Linux 2
   10.90.2.97   memory        Pass    memory size is 4096MB
   10.90.2.97   limits        Fail    soft limit of 'nofile' for user 'ec2-user' is not set or too low
   10.90.2.97   limits        Fail    hard limit of 'nofile' for user 'ec2-user' is not set or too low
   10.90.2.97   limits        Fail    soft limit of 'stack' for user 'ec2-user' is not set or too low
   10.90.2.97   selinux       Pass    SELinux is disabled
   10.90.2.97   thp           Fail    THP is enabled, please disable it for best performance
   ```

### 步骤 4. 对未通过的检查运行自动修复

+ 在终端提示符下输入以下命令并查看显示的结果。

   ```bash
   tiup cluster check ./solution-topology-ten-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --apply
   ```

   ```output
   ...
   10.90.3.79   cpu-governor  Warn    Unable to determine current CPU frequency governor policy, auto fixing not supported
   10.90.3.79   sysctl        Fail    will try to set 'fs.file-max = 1000000'
   10.90.3.79   sysctl        Fail    will try to set 'net.core.somaxconn = 32768'
   10.90.3.79   sysctl        Fail    will try to set 'net.ipv4.tcp_syncookies = 0'
   10.90.3.79   sysctl        Fail    will try to set 'vm.swappiness = 0'
   + Try to apply changes to fix failed checks
     - Applying changes on 10.90.2.231 ... Done
     - Applying changes on 10.90.2.138 ... Done
     - Applying changes on 10.90.2.242 ... Done
     - Applying changes on 10.90.2.158 ... Done
     - Applying changes on 10.90.1.56 ... Done
     - Applying changes on 10.90.4.138 ... Done
     - Applying changes on 10.90.2.90 ... Done
     - Applying changes on 10.90.1.169 ... Done
     - Applying changes on 10.90.4.126 ... Done
     - Applying changes on 10.90.2.95 ... Done
   ```

### 步骤 5. 部署一个名为 `tidb-demo` 的 TiDB 集群

+ 在终端提示符下输入以下命令并查看显示的结果。我们在命令中包含 `--yes`。如果不使用`--yes`，在部署过程中会提示你检查集群拓扑。

   ```bash
   tiup cluster deploy tidb-demo ${LAB:TIDB_VERSION} ./solution-topology-ten-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --yes
   ```

   ```output
   + Detect CPU Arch Name
     - Detecting node 10.90.2.90 Arch info ... ⠙ Shell: host=10.90.2.90, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.231 Arch info ... ⠙ Shell: host=10.90.2.231, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.138 Arch info ... ⠙ Shell: host=10.90.2.138, sudo=false, command=`uname -m`
     - Detecting node 10.90.2.242 Arch info ... ⠙ Shell: host=10.90.2.242, sudo=false, command=`uname -m`
   ...
   Cluster `tidb-demo` deployed successfully, you can start it with command: `tiup cluster start tidb-demo --init`
   ```
