# 실습 1: TiUP를 사용한 TiDB 클러스터 배포

## 개요

>[!NOTE]
> 이 실습은 [TiDB 인증 교육: TiUP를 사용한 TiDB 관리](https://www.pingcap.com/education/course-tidb-database-administration-for-self-hosted-deployments/) 과정에도 포함되어 있습니다.

이 실습에서는 TiUP 유틸리티를 설치하고, TiUP를 사용하여 TiDB 클러스터를 배포하며, TiDB 클러스터의 실행 상태를 확인합니다. TiDB 서버에 연결하고, TiDB 클러스터를 시작/중지한 후 구성과 파일을 확인합니다. 아래 다이어그램은 TiDB 클러스터의 배포 토폴로지를 보여줍니다.

  ![60%](https://lab-static.pingcap.com/quick-demo/TiDBClusterDeploymentTopologyExercise_aa_007_20220914.png)

### 사전 요구사항

+ **macOS** 사용자:
  + 개인 키 파일 [pe-class-key-${LAB:REGION_NAME}.pem](/api/labs/demo_001/vm_key?region_name=${LAB:REGION_NAME})을 클릭하여 다운로드하세요. 이 파일은 본 실습에서 원격 Linux에 로그인하는 데 필요합니다.
  + 다운로드 폴더에서 개인 키 파일을 `~/.ssh/` 폴더에 저장하세요.
  + 다음 명령을 사용하여 권한을 설정하고 파일이 제 위치에 있는지 확인하세요.

    ```bash
    chmod 400 ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem
    ls -lF ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem
    ```
    ```output
    ...
    -r--------@ 1 username staff 1674 Jun 21 2023 /Users/username/.ssh/pe-class-key-${LAB:REGION_NAME}.pem
    ```



+ **Windows 10** 사용자:
  + 개인 키 파일 [pe-class-key-${LAB:REGION_NAME}.pem](/api/labs/demo_001/vm_key?region_name=${LAB:REGION_NAME})을 클릭하여 다운로드하세요. 이 파일은 본 실습에서 원격 Linux에 로그인하는 데 필요합니다.
  + 다운로드 폴더에서 개인 키 파일을 `%userprofile%\.ssh` 폴더에 저장하세요.
  + 다음 명령을 사용하여 파일이 제 위치에 있는지 확인하세요. PowerShell이 아닌 CMD를 사용해야 합니다.
  
    ```cmd
    dir %userprofile%\.ssh\pe-class-key-${LAB:REGION_NAME}.pem
    ```
    ```output
    ...
    2024/11/14 10:22 1,674 pe-class-key-${LAB:REGION_NAME}.pem
    1 Files(s) 1,674 bytes
    ...
    ```

+ **Windows 8** 또는 이전 버전 사용자:
  + 개인 키 파일 [pe-class-key-${LAB:REGION_NAME}.ppk](/api/labs/demo_001/vm_key?region_name=${LAB:REGION_NAME}&key_type=ppk)를 클릭하여 다운로드하세요. 이 파일은 본 실습에서 원격 Linux에 로그인하는 데 필요합니다.
  + [PuTTY](https://www.putty.org/)를 다운로드하여 설치하세요.

### 소요 시간

+ 이 실습은 완료하는 데 약 60분이 소요됩니다.

> [!TIP]
> 실습 환경이 배포되면 하단 바의 **다음**을 클릭하여 **모듈 1**을 시작하세요.

## 모듈 1: TiUP 설치

### 단계 1. 원격 Linux에 로그인

+ 로컬에서 터미널 또는 SSH 클라이언트를 열고 원격 Linux에 로그인하세요. 연결 자격 증명이 없는 경우 <kbd>사전 요구사항</kbd> 섹션을 확인하세요.

+ **macOS** 사용자:

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${LAB:HOST_MONITOR1_PUBLIC_IP}
  ```
  ```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/
  ```

+ **Windows 10** 사용자

  ```cmd
  ssh -i %userprofile%\.ssh\pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${LAB:HOST_MONITOR1_PUBLIC_IP}
  ```
  ```output
  Last login: Mon Jun  3 01:38:55 2024 from ...
  [ec2-user@ip-10-90-1-234 ~]$
  ```

+ **Windows 8** 또는 이전 버전 사용자:
  + PuTTY를 실행하고 `Session` -> `Host Name`에 `ec2-user@${LAB:HOST_MONITOR1_PUBLIC_IP}`를 입력하세요.
  + `Connection` -> `SSH` -> `Auth` -> `Credentials` -> `Private key file for authentication`에서 키 파일의 위치를 찾아 지정하세요.
  + 이제 원격 Linux에 연결할 수 있습니다.

### 단계 2. IP 주소에 대한 `hosts-env.sh` 파일 확인

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

### 단계 3. 터미널 환경에서 IP 주소 변수 설정

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

### 단계 4. 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
  ===============================================
  ```

### 단계 5. TiUP 환경 변수 설정

  ```bash
  source .bash_profile
  ```

### 단계 6. TiUP 클러스터 컴포넌트가 설치되었는지 확인

  ```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. 개인 키 파일의 권한이 읽기 전용인지 확인

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

### 단계 8. TiUP 미러 설정

  ```bash
  tiup mirror set tidb-community-server-7.5.0-linux-amd64
  tiup mirror show
  ```
  ```output
  Successfully set mirror to /home/ec2-user/tidb-community-server-7.5.0-linux-amd64
  /home/ec2-user/tidb-community-server-7.5.0-linux-amd64
  ```

## 모듈 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}
  ```
  ```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/
  ```

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

## 모듈 3: 배포된 TiDB 클러스터의 상태 확인

### 단계 1. TiUP로 관리되는 클러스터 목록 보기

  ```bash
  tiup cluster list
  ```
  ```output
  Name       User      Version  Path                                                     PrivateKey
  ----       ----      -------  ----                                                     ----------
  tidb-test  ec2-user  v7.5.0   /home/ec2-user/.tiup/storage/cluster/clusters/tidb-test  /home/ec2-user/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa
  ```

### 단계 2. 배포된 TiDB 클러스터의 상태 확인

  ```bash
  tiup cluster display tidb-test
  ```
  ```output
  Cluster type:       tidb
  Cluster name:       tidb-test
  Cluster version:    v7.5.0
  Deploy user:        ec2-user
  SSH type:           builtin
  Grafana URL:        http://10.90.4.138:3000
  ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----                            -------       ------  --------                      ----------
  10.90.4.138:9093   alertmanager  10.90.4.138  9093/9094                        linux/x86_64  Down    /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.138:3000   grafana       10.90.4.138  3000                             linux/x86_64  Down    -                             /tidb-deploy/grafana-3000
  10.90.2.138:2379   pd            10.90.2.138  2379/2380                        linux/x86_64  Down    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.231:2379   pd            10.90.2.231  2379/2380                        linux/x86_64  Down    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.90:2379    pd            10.90.2.90   2379/2380                        linux/x86_64  Down    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.138:9090   prometheus    10.90.4.138  9090/12020                       linux/x86_64  Down    /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.1.169:4000   tidb          10.90.1.169  4000/10080                       linux/x86_64  Down    -                             /tidb-deploy/tidb-4000
  10.90.1.56:4000    tidb          10.90.1.56   4000/10080                       linux/x86_64  Down    -                             /tidb-deploy/tidb-4000
  10.90.4.126:9000   tiflash       10.90.4.126  9000/8123/3930/20170/20292/8234  linux/x86_64  N/A     /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
  10.90.2.158:20160  tikv          10.90.2.158  20160/20180                      linux/x86_64  N/A     /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.2.242:20160  tikv          10.90.2.242  20160/20180                      linux/x86_64  N/A     /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.2.95:20160   tikv          10.90.2.95   20160/20180                      linux/x86_64  N/A     /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  Total nodes: 12
  ```

### 단계 3. 표준 시작 모드로 TiDB 클러스터 시작

  ```bash
  tiup cluster start tidb-test
  ```
  ```output
  ...
  Started cluster `tidb-test` successfully
  ```

### 단계 4. 시작된 TiDB 클러스터의 상태 확인

  ```bash
  tiup cluster display tidb-test
  ```
  ```output
  Cluster type:       tidb
  Cluster name:       tidb-test
  Cluster version:    v7.5.0
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://34.222.39.121:2379/dashboard
  Grafana URL:        http://10.90.4.138:3000
  ID                 Role          Host         Ports                            OS/Arch       Status  Data Dir                      Deploy Dir
  --                 ----          ----         -----                            -------       ------  --------                      ----------
  10.90.4.138:9093   alertmanager  10.90.4.138  9093/9094                        linux/x86_64  Up      /tidb-data/alertmanager-9093  /tidb-deploy/alertmanager-9093
  10.90.4.138:3000   grafana       10.90.4.138  3000                             linux/x86_64  Up      -                             /tidb-deploy/grafana-3000
  10.90.2.138:2379   pd            10.90.2.138  2379/2380                        linux/x86_64  Up|L    /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.231:2379   pd            10.90.2.231  2379/2380                        linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.2.90:2379    pd            10.90.2.90   2379/2380                        linux/x86_64  Up      /tidb-data/pd-2379            /tidb-deploy/pd-2379
  10.90.4.138:9090   prometheus    10.90.4.138  9090/12020                       linux/x86_64  Up      /tidb-data/prometheus-9090    /tidb-deploy/prometheus-9090
  10.90.1.169:4000   tidb          10.90.1.169  4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.1.56:4000    tidb          10.90.1.56   4000/10080                       linux/x86_64  Up      -                             /tidb-deploy/tidb-4000
  10.90.4.126:9000   tiflash       10.90.4.126  9000/8123/3930/20170/20292/8234  linux/x86_64  Up      /tidb-data/tiflash-9000       /tidb-deploy/tiflash-9000
  10.90.2.158:20160  tikv          10.90.2.158  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.2.242:20160  tikv          10.90.2.242  20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  10.90.2.95:20160   tikv          10.90.2.95   20160/20180                      linux/x86_64  Up      /tidb-data/tikv-20160         /tidb-deploy/tikv-20160
  Total nodes: 12
  ```

### 단계 5. TiDB PD Dashboard와 Grafana Prometheus Dashboard로 클러스터 상태 확인

+ [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard)로 이동하여 클러스터 상태를 확인하세요. 사용자 이름과 비밀번호는 TiDB root 사용자와 동일합니다. 표준 시작을 선택한 경우 비밀번호는 비어 있습니다.

   ![60%](https://lab-static.pingcap.com/quick-demo/TiDBDashboard_aa_001_20220615.png)

+ [Grafana Prometheus Dashboard](http://${LAB:HOST_MONITOR1_PUBLIC_IP}:3000)로 이동하여 클러스터 상태를 확인하세요. 기본 사용자 이름과 비밀번호는 모두 `admin`입니다. TiDB 포트 상태와 부하 모니터링 정보를 확인하려면 `tidb-test-Overview`를 클릭하세요.

   ![40%](https://lab-static.pingcap.com/quick-demo/Grafana_aa_001_20220615.png)

## 모듈 4: TiDB 서버에 연결

### 단계 1. TiDB에 연결

  ```bash
  mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root
  ```

### 단계 2. TiDB 클러스터의 버전 확인
  ```sql
  SELECT TIDB_VERSION()\G
  ```
  ```output
  *************************** 1. row ***************************
  TIDB_VERSION(): Release Version: v7.5.0
  Edition: Community
  Git Commit Hash: 069631e2ecfedc000ffb92c67207bea81380f020
  Git Branch: heads/refs/tags/v7.5.0
  UTC Build Time: 2023-11-24 08:50:14
  GoVersion: go1.21.3
  Race Enabled: false
  TiKV Min Version: 7.2.0-alpha
  Check Table Before Drop: false
  Store: tikv
  1 row in set (0.00 sec)
  ```

### 단계 3. 제어 머신 터미널로 종료

  ```sql
  EXIT;
  ```
  ```output
  Bye
  ```

## 모듈 5: TiDB 클러스터 중지

### 단계 1. TiDB 클러스터 중지

  ```bash
  tiup cluster stop tidb-test
  ```
  ```output
  Will stop the cluster tidb-test with nodes: , roles: .
  Do you want to continue? [y/N]:(default=N) y
  + [ Serial ] - SSHKeySet: privateKey=/home/ec2-user/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa, publicKey=/home/ec2-user/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa.pub
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.95
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.90
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.158
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.169
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.1.56
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.126
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.138
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.138
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.4.138
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.138
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.242
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.231
  + [ Serial ] - StopCluster
  ...
  Stopped cluster `tidb-test` successfully
  ```

## 모듈 6: 구성 파일 검토

### 단계 1. 구성 파일 디렉토리의 내용 검토

  ```bash
  tiup cluster edit-config tidb-test
  ```
  ```output
  global:
    user: ec2-user
    ssh_port: 22
    ssh_type: builtin
    deploy_dir: /tidb-deploy
    data_dir: /tidb-data
    os: linux
    arch: amd64
    systemd_mode: system
  ...
  alertmanager_servers:
  - host: 10.90.4.138
    ssh_port: 22
    web_port: 9093
    cluster_port: 9094
    deploy_dir: /tidb-deploy/alertmanager-9093
    data_dir: /tidb-data/alertmanager-9093
    log_dir: /tidb-deploy/alertmanager-9093/log
    arch: amd64
    os: linux
  ```

+ `:q!`를 입력하여 터미널로 나가세요.

### 단계 2. TiDB 구성 파일 디렉토리의 파일 목록 보기

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_DB1_PRIVATE_IP} ls -l /tidb-deploy/tidb-4000/
  ```
  ```output
  total 0
  drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 bin
  drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 conf
  drwxr-xr-x 3 ec2-user ec2-user 90 May 25 04:21 log
  drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 scripts
  ```

### 단계 3. TiKV 구성 파일 디렉토리의 파일 목록 보기

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-deploy/
  ```
  ```output
  total 0
  drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 monitor-9100
  drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 tikv-20160
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-deploy/tikv-20160/
  ```
  ```output
  total 0
  drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 bin
  drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 conf
  drwxr-xr-x 2 ec2-user ec2-user 45 May 25 04:20 log
  drwxr-xr-x 2 ec2-user ec2-user 25 May 25 04:16 scripts
  ```

### 단계 4. TiKV 데이터 파일 디렉토리의 파일 목록 보기

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-data/
  ```
  ```output
  total 0
  drwxr-xr-x 2 ec2-user ec2-user   6 May 25 04:16 monitor-9100
  drwxr-xr-x 6 ec2-user ec2-user 164 May 25 04:20 tikv-20160
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_KV1_PRIVATE_IP} ls -l /tidb-data/tikv-20160/
  ```
  ```output
  total 1048672
  drwxr-xr-x 2 ec2-user ec2-user        134 May 25 04:20 db
  drwxr-xr-x 4 ec2-user ec2-user         33 May 25 04:20 import
  -rw-r--r-- 1 ec2-user ec2-user      20330 May 25 04:20 last_tikv.toml
  -rw-r--r-- 1 ec2-user ec2-user          0 May 25 04:20 LOCK
  -rw-r--r-- 1 ec2-user ec2-user          0 May 25 04:20 raftdb.info
  drwxr-xr-x 2 ec2-user ec2-user         82 May 25 04:20 raft-engine
  -rw-r--r-- 1 ec2-user ec2-user      75538 May 25 04:20 rocksdb.info
  drwxr-xr-x 2 ec2-user ec2-user          6 May 25 04:21 snap
  -rw-r--r-- 1 ec2-user ec2-user 1073741824 May 25 04:20 space_placeholder_file
  ```

### 단계 5. PD 구성 파일 디렉토리의 파일 목록 보기

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-deploy/
  ```
  ```output
  total 0
  drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 monitor-9100
  drwxr-xr-x 6 ec2-user ec2-user 55 May 25 04:16 pd-2379
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-deploy/pd-2379/
  ```
  ```output
  total 0
  drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 bin
  drwxr-xr-x 2 ec2-user ec2-user 21 May 25 04:16 conf
  drwxr-xr-x 2 ec2-user ec2-user 41 May 25 04:20 log
  drwxr-xr-x 2 ec2-user ec2-user 23 May 25 04:16 scripts
  ```

### 단계 6. PD 데이터 파일 디렉토리의 파일 목록 보기

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-data/
  ```
  ```output
  total 0
  drwxr-xr-x 2 ec2-user ec2-user  6 May 25 04:16 monitor-9100
  drwxr-xr-x 5 ec2-user ec2-user 57 May 25 04:20 pd-2379
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_PD1_PRIVATE_IP} ls -l /tidb-data/pd-2379/
  ```
  ```output  
  total 0
  drwxr-xr-x 2 ec2-user ec2-user 85 May 25 04:20 hot-region
  drwx------ 4 ec2-user ec2-user 29 May 25 04:20 member
  drwxr-xr-x 2 ec2-user ec2-user 85 May 25 04:20 region-meta
  ```

## 실습 완료