1. 程式人生 > 實用技巧 >TIDB叢集部署文件

TIDB叢集部署文件

TIDB叢集部署文件

安裝部署

準備機器

172.17.0.7 tidb01 centos7.4 運維 8C16G100G 中控機,目標部署機(記憶體不少於16G)
172.17.0.8 pd01 centos7.4 運維 4C8G100G 目標部署機
172.17.0.9 tikv01 centos7.4 運維 8C32G100G 目標部署機
172.17.0.10 tikv02 centos7.4 運維 8C32G100G 目標部署機
172.17.0.11 tikv03 centos7.4 運維 8C32G100G 目標部署機

埠說明

注意提前在青雲把下面埠開放:

TiDB 4000 應用及 DBA 工具訪問通訊埠
TiDB 10080 TiDB 狀態資訊上報通訊埠
TiKV 20160 TiKV 通訊埠
PD 2379 提供 TiDB 和 PD 通訊埠
PD 2380 PD 叢集節點間通訊埠
Pump 8250 Pump 通訊埠
Drainer 8249 Drainer 通訊埠
Prometheus 9090 Prometheus 服務通訊埠
Pushgateway 9091 TiDB,TiKV,PD 監控聚合和上報埠
Node_exporter 9100 TiDB 叢集每個節點的系統資訊上報通訊埠
Blackbox_exporter 9115 Blackbox_exporter 通訊埠,用於 TiDB 叢集埠監控
Grafana 3000 Web 監控服務對外服務和客戶端(瀏覽器)訪問埠
Grafana 8686 grafana_collector 通訊埠,用於將 Dashboard 匯出為 PDF 格式
Kafka_exporter 9308 Kafka_exporter 通訊埠,用於監控 binlog kafka 叢集

軟體版本

tidb-ansible: v3.0.2

1.在中控機上安裝系統依賴包

以 root 使用者登入中控機

#!/bin/bash
#coding:utf-8
#
## 在中控機安裝依賴包
yum -y install epel-release git curl sshpass
yum -y install python2-pip
## 在中控機上建立 tidb 使用者,並生成 ssh key
useradd -m -d /home/tidb tidb
echo "tidb321" | passwd --stdin tidb
cp -a /etc/sudoers{,.bak}
echo "tidb ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
su - tidb

2.生成金鑰對

建立 tidb 使用者 ssh key, 提示 Enter passphrase 時直接回車即可。執行成功後,ssh 私鑰檔案為 /home/tidb/.ssh/id_rsa, ssh 公鑰檔案為 /home/tidb/.ssh/id_rsa.pub

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tidb/.ssh/id_rsa):
Created directory '/home/tidb/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/tidb/.ssh/id_rsa.
Your public key has been saved in /home/tidb/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:eIBykszR1KyECA/h0d7PRKz4fhAeli7IrVphhte7/So [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|=+o+.o. |
|o=o+o.oo |
| .O.=.= |
| . B.B + |
|o B * B S |
| * + * + |
| o + . |
| o E+ . |
|o ..+o. |
+----[SHA256]-----+

3.在中控機器上下載 TiDB-Ansible

以 tidb 使用者登入中控機並進入 /home/tidb 目錄。使用以下命令從 TiDB-Ansible 專案上下載 master 分支的 TiDB-Ansible,預設的資料夾名稱為 tidb-ansible

# su - tidb
$ tag=v3.0.2
$ git clone -b $tag https://github.com/pingcap/tidb-ansible.git

4.在中控機器上安裝 Ansible 及其依賴

以 tidb 使用者登入中控機,請務必按以下方式通過 pip 安裝 Ansible 及其相關依賴的指定版本,否則會有相容問題。安裝完成後,可通過 ansible --version 檢視 Ansible 版本。目前 release-2.0、release-2.1 及 master 版本相容 Ansible 2.4 及 Ansible 2.5 版本,Ansible 及相關依賴版本記錄在 tidb-ansible/requirements.txt 檔案中。

$ cd /home/tidb/tidb-ansible
$ sudo pip install -r ./requirements.txt -i https://mirrors.aliyun.com/pypi/simple

5.在中控機上配置部署機器 ssh 互信及 sudo 規則

以 tidb 使用者登入中控機,將你的部署目標機器 IP 新增到 hosts.ini 檔案 [servers] 區塊下

[tidb@172-17-0-7 tidb-ansible]$ cat hosts.ini 
[servers]
172.17.0.7 ansible_port=52222
172.17.0.8 ansible_port=52222
172.17.0.9 ansible_port=52222
172.17.0.10 ansible_port=52222
172.17.0.11 ansible_port=52222
[all:vars]
username = tidb
ntp_server = pool.ntp.org

執行以下命令,按提示輸入部署目標機器 root 使用者密碼。該步驟將在部署目標機器上建立 tidb 使用者,並配置 sudo 規則,配置中控機與部署目標機器之間的 ssh 互信。

$ ansible-playbook -i hosts.ini create_users.yml -u root -k

以 tidb 使用者登入到中控機,ssh 登入目標機器 IP,不需要輸入密碼並登入成功,表示 ssh 互信配置成功。

[[email protected] ~]$ ssh 172.17.0.7

以 tidb 使用者登入到部署目標機器後,執行以下命令,不需要輸入密碼並切換到 root 使用者,表示 tidb 使用者 sudo 免密碼配置成功。

[[email protected] ~]$ sudo -su root
[[email protected] tidb]#

6.分配機器資源,編輯 inventory.ini 檔案

以 tidb 使用者登入中控機,inventory.ini 檔案路徑為 /home/tidb/tidb-ansible/inventory.ini。

## TiDB Cluster Part
[tidb_servers]
172.17.0.7
[tikv_servers]
172.17.0.9
172.17.0.10
172.17.0.11
[pd_servers]
172.17.0.8
[spark_master]
[spark_slaves]
[lightning_server]
[importer_server]
## Monitoring Part
# prometheus and pushgateway servers
[monitoring_servers]
172.17.0.7
[grafana_servers]
172.17.0.7
# node_exporter and blackbox_exporter servers
[monitored_servers]
172.17.0.7
172.17.0.8
172.17.0.9
172.17.0.10
172.17.0.11
[alertmanager_servers]
172.17.0.7
[kafka_exporter_servers]
## Binlog Part
[pump_servers]
[drainer_servers]
## Group variables
[pd_servers:vars]
# location_labels = ["zone","rack","host"]
## Global variables
[all:vars]
deploy_dir = /home/tidb/deploy
## Connection
# ssh via normal user
ansible_user = tidb
ansible_port=52222
cluster_name = test-cluster
tidb_version = v3.0.2
# process supervision, [systemd, supervise]
process_supervision = systemd
timezone = Asia/Shanghai
enable_firewalld = False
# check NTP service
enable_ntpd = True
set_hostname = False
## binlog trigger
enable_binlog = False
# kafka cluster address for monitoring, example:
# kafka_addrs = "192.168.0.11:9092,192.168.0.12:9092,192.168.0.13:9092"
kafka_addrs = ""
# zookeeper address of kafka cluster for monitoring, example:
# zookeeper_addrs = "192.168.0.11:2181,192.168.0.12:2181,192.168.0.13:2181"
zookeeper_addrs = ""
# enable TLS authentication in the TiDB cluster
enable_tls = False
# KV mode
deploy_without_tidb = False
# wait for region replication complete before start tidb-server.
wait_replication = True
# Optional: Set if you already have a alertmanager server.
# Format: alertmanager_host:alertmanager_port
alertmanager_target = ""
grafana_admin_user = "admin"
grafana_admin_password = "admin"

### Collect diagnosis
collect_log_recent_hours = 2
enable_bandwidth_limit = True
# default: 10Mb/s, unit: Kbit/s
collect_bandwidth_limit = 10000

7.部署任務

1.確認 tidb-ansible/inventory.ini 檔案中 ansible_user = tidb,本例使用 tidb 使用者作為服務執行使用者,配置如下:

## Connection
# ssh via normal user
ansible_user = tidb

執行以下命令如果所有 server 返回 tidb 表示 ssh 互信配置成功。

ansible -i inventory.ini all -m shell -a 'whoami'

執行以下命令如果所有 server 返回 root 表示 tidb 使用者 sudo 免密碼配置成功。

ansible -i inventory.ini all -m shell -a 'whoami' -b

2.執行 local_prepare.yml playbook,聯網下載 TiDB binary 到中控機:

ansible-playbook local_prepare.yml

3.初始化系統環境,修改核心引數

ansible-playbook bootstrap.yml

磁碟壓測通不過,需要把該yml檔案下面註釋

vim bootstrap.yml
# - { role: machine_benchmark, when: not dev_mode|default(false) }

4.部署 TiDB 叢集軟體

ansible-playbook deploy.yml

如果檢測通不過,可以暫時註釋相應yml檔案,不影響叢集部署和啟動。
5.啟動 TiDB 叢集

ansible-playbook start.yml

8.測試叢集

找一臺安裝了mysql工具的節點,執行:

[root@172-17-0-3 ~]# mysql -u root -h 172.17.0.7 -P 4000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-TiDB-v3.0.2 MySQL Community Server (Apache License 2.0)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| INFORMATION_SCHEMA |
| PERFORMANCE_SCHEMA |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)

說明啟動成功,執行正常!
grafana地址:

http://172.17.0.7:3000 預設帳號密碼是:admin/admin

至此,tidb叢集部署完成!