## 模块 2: 部署 TiDB 集群

### 步骤 1: 查看 `solution-topology-ten-nodes.yaml`

+ `solution-topology-ten-nodes.yaml` 是用于部署 TiDB 集群的 yaml 文件。以下显示的 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","storage-performance"]
      replication.max-replicas: 3
  pd_servers:
    - host: 10.90.2.84
      client_port: 2379
      peer_port: 2380
      advertise_client_addr: "http://54.251.28.224:2379"
      config:
        server.labels:
          region: seattle
          zone: az1
    - host: 10.90.2.134
      client_port: 2379
      peer_port: 2380
      advertise_client_addr: "http://54.179.198.121:2379"
      config:
        server.labels:
          region: seattle
          zone: az2
    - host: 10.90.2.214
      client_port: 2379
      peer_port: 2380
      advertise_client_addr: "http://13.215.194.132:2379"
      config:
        server.labels:
          region: seattle
          zone: az3
  tidb_servers:
    - host: 10.90.2.124
      port: 4000
      status_port: 10080
      deploy_dir: /tidb-deploy/tidb-4000
      log_dir: /tidb-deploy/tidb-4000/log
    - host: 10.90.2.219
      port: 4000
      status_port: 10080
      deploy_dir: /tidb-deploy/tidb-4000
      log_dir: /tidb-deploy/tidb-4000/log
  tikv_servers:
    - host: 10.90.3.239
      port: 20160
      status_port: 20180
      config:
        server.labels:
          region: seattle
          zone: az1
          storage-performance: high
    - host: 10.90.3.119
      port: 20160
      status_port: 20180
      config:
        server.labels:
          region: seattle
          zone: az2
          storage-performance: high
    - host: 10.90.3.181
      port: 20160
      status_port: 20180
      config:
      server.labels:
        region: seattle
          zone: az3
          storage-performance: high
  tiflash_servers:
    - host: 10.90.4.159
      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.93
  grafana_servers:
    - host: 10.90.4.93
  alertmanager_servers:
    - host: 10.90.4.93
  ```

### 步骤 2. 检查节点上是否安装了 `numactl`

+ 所有节点上都应该已安装 `numactl`。我们只需登录到一个 `TiKV` 节点进行检查。

  ```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
  ```

+ 返回控制机:

  ```bash
  exit
  ```

### 步骤 3. 检查集群部署的系统设置的先决条件

  ```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-test` 的 TiDB 集群

  ```bash
  tiup cluster deploy tidb-test 7.5.0 ./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-test` deployed successfully, you can start it with command: `tiup cluster start tidb-test --init`
  ```
