## 模块 1: 部署 TiDB 集群及 TiProxy

### 步骤 1. 登录到远程 Linux

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

### 步骤 2. 检查集群拓扑 YAML 文件

  ```bash
  cat eleven-nodes.yaml
  ```

  ```output
  global:
    user: "ec2-user"
    ssh_port: 22
    deploy_dir: "/tidb-deploy"
    data_dir: "/tidb-data"
    arch: "amd64"
  server_configs:
    pd:
      replication.max-replicas: 3
    tidb:
      security.auto-tls: true
      graceful-wait-before-shutdown: 10
  pd_servers:
    - host: 10.90.3.160
      client_port: 2379
      peer_port: 2380
      advertise_client_addr: "http://35.92.30.186:2379"
    - host: 10.90.1.162
      client_port: 2379
      peer_port: 2380
      advertise_client_addr: "http://52.34.128.150:2379"
    - host: 10.90.2.223
      client_port: 2379
      peer_port: 2380
      advertise_client_addr: "http://54.245.183.8:2379"
  tidb_servers:
    - host: 10.90.1.157
      port: 4000
      status_port: 10080
      deploy_dir: /tidb-deploy/tidb-4000
      log_dir: /tidb-deploy/tidb-4000/log
    - host: 10.90.2.6
      port: 4000
      status_port: 10080
      deploy_dir: /tidb-deploy/tidb-4000
      log_dir: /tidb-deploy/tidb-4000/log
  tikv_servers:
    - host: 10.90.3.132
      port: 20160
      status_port: 20180
    - host: 10.90.2.171
      port: 20160
      status_port: 20180
    - host: 10.90.1.222
      port: 20160
      status_port: 20180
  monitoring_servers:
    - host: 10.90.4.33
  grafana_servers:
    - host: 10.90.4.33
  alertmanager_servers:
    - host: 10.90.4.33
  tiproxy_servers:
    - host: 10.90.1.153
  ```

### 步骤 3. 修改系统设置以部署 TiDB 集群

  ```bash
  tiup mirror set tidb-community-server-7.5.4-linux-amd64
  tiup cluster check eleven-nodes.yaml \
    --user ec2-user \
    -i /home/ec2-user/.ssh/pe-class-key-${LAB:REGION_NAME}.pem \
    --apply
  ```

  ```output
  ...
  + Try to apply changes to fix failed checks
   - Applying changes on 10.90.1.162 ... ⠧ Sysctl: host=10.90.1.162 vm.swappiness = 0
   - Applying changes on 10.90.2.223 ... ⠧ Sysctl: host=10.90.2.223 vm.swappiness = 0
   - Applying changes on 10.90.3.132 ... ⠧ Sysctl: host=10.90.3.132 vm.swappiness = 0
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
   - Applying changes on 10.90.1.162 ... Done
   - Applying changes on 10.90.2.223 ... Done
  + Try to apply changes to fix failed checks
   - Applying changes on 10.90.1.162 ... Done
  + Try to apply changes to fix failed checks
  + Try to apply changes to fix failed checks
   - Applying changes on 10.90.1.162 ... Done
   - Applying changes on 10.90.2.223 ... Done
   - Applying changes on 10.90.3.132 ... Done
   - Applying changes on 10.90.2.171 ... Done
   - Applying changes on 10.90.1.222 ... Done
   - Applying changes on 10.90.4.33 ... Done
   - Applying changes on 10.90.3.160 ... Done
   - Applying changes on 10.90.1.153 ... Done
   - Applying changes on 10.90.2.210 ... Done
   - Applying changes on 10.90.1.157 ... Done
   - Applying changes on 10.90.2.6 ... Done
  ```

### 步骤 4. 部署 TiDB 集群

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

  ```output
  ...
  Cluster `tidb-demo` deployed successfully, you can start it with command: `tiup cluster start tidb-demo --init`
  ```

### 步骤 5. 启动已部署的 TiDB 集群

+ 启动集群。

  ```bash
  tiup cluster start tidb-demo
  ```

  ```output
  ...
  Started cluster `tidb-demo` successfully
  ```

+ 运行命令以获取集群状态。集群包含 3 台 PD 服务器、3 台 TiKV 服务器、2 台 TiDB 服务器和 1 台 TiProxy 服务器。你的环境和示例输出中的 IP 地址细节可能不同，但子网结构是相同的。

> [!TIP]
> 如果发现有组件处于 `Down` 状态，请等待几秒钟后再次运行 display 命令。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v7.5.4
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://13.158.51.172:2379/dashboard
  Grafana URL:        http://10.90.4.109:3000
  ID                 Role          Host         Ports        OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----        -------       ------  --------                      ----------
  10.90.4.109:9093   alertmanager  10.90.4.109  9093/9094    linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.109:3000   grafana       10.90.4.109  3000         linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.1.128:2379   pd            10.90.1.128  2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.1.51:2379    pd            10.90.1.51   2379/2380    linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.1.89:2379    pd            10.90.1.89   2379/2380    linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.109:9090   prometheus    10.90.4.109  9090/12020   linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.2.250:4000   tidb          10.90.2.250  4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.2.97:4000    tidb          10.90.2.97   4000/10080   linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.1.105:20160  tikv          10.90.1.105  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.50:20160   tikv          10.90.1.50   20160/20180  linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.1.58:6000    tiproxy       10.90.1.58   6000/3080    linux/x86_64  Up      -                             /tidb-deploy/tiproxy-6000
  Total nodes: 12
  ```
