官方文檔:TIDB簡介
TiDB 是 PingCAP 公司設計的開源分佈式 HTAP (Hybrid Transactional and Analytical Processing) 數據庫,結合了傳統的 RDBMS 和 NoSQL 的最佳特性。TiDB 兼容 MySQL,支持無限的水平擴展,具備強一致性和高可用性。TiDB 的目標是爲 OLTP (Online Transactional Processing) 和 OLAP (Online Analytical Processing) 場景提供一站式的解決方案。
官方鏈接:https://pingcap.com/docs-cn/v3.0/overview/
系統的初始化在此不在贅述,直接貼出本次實驗所用系統
[root@localhost yum.repos.d]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.6.1810 (Core) Release: 7.6.1810 Codename: Core
## 這個是本次實驗所用系統的角色分配
Host | Hostname | Tidb | Pd | Tikv | Grafana | Tispark | Tidb-ansible | cpu | ram |
---|---|---|---|---|---|---|---|---|---|
192.168.2.91 | tbpd1 | √ | √ | 2*12 | 16G | ||||
192.168.2.92 | tbpd2 | √ | √ | 2*12 | 16G | ||||
192.168.9.93 | pd3 | √ | √ | √ | √ | 2*12 | 32G | ||
192.168.9.94 | tikv1 | √ | 2*12 | 16G | |||||
192.168.9.95 | tikv2 | √ | 2*12 | 16G | |||||
192.168.9.96 | tikv3 | √ | 2*12 | 16G |
正式開始搭建TIDB集羣
-
一些約定
-
賬戶分爲root和yx,密碼均爲123
-
所有服務安裝目錄均爲/home/yx/server
-
關閉ssh服務的dns反解析,很重要,關係到後期是否能成功啓動。
vim /etc/ssh/sshd_config UseDNS no systemctl reload sshd.service
-
此次實驗選擇的中控機是93,因此首先切換到93服務器
-
安裝依賴包
yum -y install epel-release git curl sshpass yum -y install python-pip
-
普通用戶免密登錄root,新增一行
vim /etc/sudoers yx ALL=(ALL) NOPASSWD: ALL
-
以yx用戶登錄中控機,生成ssh key, 提示 Enter passphrase 時直接回車即可。執行成功後,ssh 私鑰文件爲 /home/yx/.ssh/id_rsa, ssh 公鑰文件爲 /home/yx/.ssh/id_rsa.pub。
-
如果是已有公鑰的服務器,此處直接拷貝id_rsa.pub這個文件到其他五臺服務器的/home/yx/.ssh/authorized_keys文件下就好。
[yx@pd3 ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/yx/.ssh/id_rsa): Created directory '/home/yx/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/yx/.ssh/id_rsa. Your public key has been saved in /home/yx/.ssh/id_rsa.pub. The key fingerprint is: SHA256:nEIhIFBHMJTJsSC20wQcRNnxTzznXx2OkjEQpbrMF3Y [email protected] The key's randomart image is: +---[RSA 2048]----+ |XX%X= . oo. | |o=B=.... o | | o.. ..+ o o . | | . .o.=. + + .| | .oS+ E o o | | o.o + o | | + . . | | . | | | +----[SHA256]-----+於版本的選擇
關於版本的選擇
tidb-ansible 分支 | TiDB 版本 | 備註 |
---|---|---|
release-2.0 | 2.0 版本 | 最新 2.0 穩定版本,可用於生產環境。 |
release-2.1 | 2.1 版本 | 最新 2.1 穩定版本,可用於生產環境(建議)。 |
3.0 版本 | v3.0.0-beta、v3.0.0-beta.1 等 | 目前是 beta 版本,不建議用於生產環境。 |
latest 版本 | None | 包含最新特性,每日更新,不建議用於生產環境。 |
安裝tidb-ansible
-
在中控機器上下載 TiDB-Ansible 2.1,此處是93服務器
[yx@pd3 server]$ git clone -b release-2.1 https://github.com/pingcap/tidb-ansible.git
-
以 yx用戶登錄中控機,請務必按以下方式通過 pip 安裝 Ansible 及其相關依賴的指定版本,否則會有兼容問題。
cd /home/yx/server/tidb-ansible sudo pip install -r ./requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ansible --version ansible 2.6.17
配置ansible的ssh互信規則
-
在中控機上配置部署機器 ssh 互信及 sudo 規則
-
此處有兩個選擇,要麼按照我的操作步驟來,要麼手動拷貝中控機的公鑰到其他服務器上,做免祕鑰認證
-
編輯hosts.ini文件,添加組和username
[yx@pd3 tidb-ansible]$ vim hosts.ini [servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 [all:vars] username = yx //官方是要創建tidb用戶,此處統一使用yx用戶 ntp_server = pool.ntp.org
-
執行以下命令,按提示輸入部署目標機器 root 用戶密碼。該步驟將在部署目標機器上創建 yx 用戶,並配置 sudo 規則,配置中控機與部署目標機器之間的 ssh 互信。
[yx@pd3 tidb-ansible]$ ansible-playbook -i hosts.ini create_users.yml -u root -k SSH password: ... PLAY RECAP ********************************************************************************************************************************************************************************** 192.168.9.91 : ok=3 changed=2 unreachable=0 failed=0 192.168.9.92 : ok=3 changed=2 unreachable=0 failed=0 192.168.9.93 : ok=3 changed=2 unreachable=0 failed=0 192.168.9.94 : ok=3 changed=2 unreachable=0 failed=0 192.168.9.95 : ok=3 changed=2 unreachable=0 failed=0 192.168.9.96 : ok=3 changed=2 unreachable=0 failed=0 Congrats! All goes well. :-)
-
創建完成後可以ssh試下能否免密登錄其他機器。sudo也是不要密碼的。
-
可選項:如果其他服務器都是新的,沒有安裝NTP服務也沒有做時間同步,那麼就執行以下命令在整個集羣上安裝NTP服務,反之直接略過。
[yx@pd3 tidb-ansible]$ ansible-playbook -i hosts.ini deploy_ntp.yml -u yx -b -T 600 ... PLAY RECAP ********************************************************************************************************************************************************************************** 192.168.9.91 : ok=6 changed=3 unreachable=0 failed=0 192.168.9.92 : ok=6 changed=3 unreachable=0 failed=0 192.168.9.93 : ok=6 changed=3 unreachable=0 failed=0 192.168.9.94 : ok=6 changed=3 unreachable=0 failed=0 192.168.9.95 : ok=6 changed=3 unreachable=0 failed=0 192.168.9.96 : ok=6 changed=3 unreachable=0 failed=0 Congrats! All goes well. :-)
分配角色,並完成初始化以及部署
-
對照你的角色分配表,配置inventory.ini文件
[yx@pd3 tidb-ansible]$ vim inventory.ini ## TiDB Cluster Part [tidb_servers] 192.168.9.91 192.168.9.92 [tikv_servers] 192.168.9.94 192.168.9.95 192.168.9.96 [pd_servers] 192.168.9.91 192.168.9.92 192.168.9.93 [spark_master] //如果TIspark角色要單獨分離出來,需要在master和slave都寫上,否則默認安裝在TIDB角色上 192.168.9.93 [spark_slaves] [lightning_server] [importer_server] ## Monitoring Part # prometheus and pushgateway servers [monitoring_servers] //監控 192.168.9.93 [grafana_servers] //監控 192.168.9.93 # node_exporter and blackbox_exporter servers [monitored_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 [alertmanager_servers] 192.168.0.1 //刪除掉此行 ...... [all:vars] deploy_dir = /home/yx/server/deploy //tikv,pd,tidb的安裝路徑 ...... ansible_user = yx cluster_name = tidb_colony //集羣名稱
-
執行以下命令如果所有 server 返回 yx表示 ssh 互信配置成功。
[yx@pd3 tidb-ansible]$ ansible -i inventory.ini all -m shell -a 'whoami' 192.168.9.94 | SUCCESS | rc=0 >> yx 192.168.9.93 | SUCCESS | rc=0 >> yx 192.168.9.95 | SUCCESS | rc=0 >> yx 192.168.9.96 | SUCCESS | rc=0 >> yx 192.168.9.91 | SUCCESS | rc=0 >> yx 192.168.9.92 | SUCCESS | rc=0 >> yx
-
執行以下命令如果所有 server 返回
root
表示 yx用戶 sudo 免密碼配置成功。
[yx@pd3 tidb-ansible]$ ansible -i inventory.ini all -m shell -a 'whoami' -b 192.168.9.94 | SUCCESS | rc=0 >> root 192.168.9.92 | SUCCESS | rc=0 >> root 192.168.9.91 | SUCCESS | rc=0 >> root 192.168.9.95 | SUCCESS | rc=0 >> root 192.168.9.96 | SUCCESS | rc=0 >> root 192.168.9.93 | SUCCESS | rc=0 >> root
-
執行
local_prepare.yml
playbook,聯網下載 TiDB binary 到中控機:
[yx@pd3 tidb-ansible]$ ansible-playbook local_prepare.yml ...... PLAY RECAP ********************************************************************************************************************************************************************************** localhost : ok=30 changed=22 unreachable=0 failed=0 Congrats! All goes well. :-)
-
初始化系統環境,修改內核參數,因爲本次實驗所採用硬盤爲SAS的HDD,所以需要將腳本里的一些檢查配置項註釋掉
-
註釋掉硬盤檢查
[yx@pd3 tidb-ansible]$ vim roles/machine_benchmark/tasks/fio_randread.yml 3 #- name: fio randread benchmark on tikv_data_dir disk 4 # shell: "cd {{ fio_deploy_dir }} && ./fio -ioengine=psync -bs=32k -fdatasync=1 -thread -rw=randread -size={{ benchmark_size }} -filename=fio_randread_test.txt -name='fio randread test ' -iodepth=4 -runtime=60 -numjobs=4 -group_reporting --output-format=json --output=fio_randread_result.json" 5 # register: fio_randread ... 12 #- name: get fio randread iops 13 # shell: "python parse_fio_output.py --target='fio_randread_result.json' --read-iops" 14 # register: disk_randread_iops 15 # args: 16 # chdir: "{{ fio_deploy_dir }}/" 17 # 18 #- name: get fio randread summary 19 # shell: "python parse_fio_output.py --target='fio_randread_result.json' --summary" 20 # register: disk_randread_smmary 21 # args: 22 # chdir: "{{ fio_deploy_dir }}/" ... 33 #- name: Preflight check - Does fio randread iops of tikv_data_dir disk meet requirement 34 # fail: 35 # msg: 'fio: randread iops of tikv_data_dir disk is too low: {{ disk_randread_iops.stdout }} < {{ min_ssd_randread_iops }}, it is strongly recommended to use SSD disks for TiKV and P D, or there might be performance issues.' 36 # when: disk_randread_iops.stdout|int < min_ssd_randread_iops|int
-
註釋掉磁盤轉數檢查
[yx@pd3 tidb-ansible]$ vim roles/machine_benchmark/tasks/fio_randread_write_latency.yml 39 #- name: Preflight check - Does fio mixed randread and sequential write latency of tikv_data_dir disk meet requirement - randread 40 # fail: 41 # msg: 'fio mixed randread and sequential write test: randread latency of tikv_data_dir disk is too low: {{ disk_mix_randread_lat.stdout }} ns > {{ max_ssd_mix_randread_lat }} ns, i t is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.' 42 # when: disk_mix_randread_lat.stdout|int > max_ssd_mix_randread_lat|int 43 44 #- name: Preflight check - Does fio mixed randread and sequential write latency of tikv_data_dir disk meet requirement - sequential write 45 # fail: 46 # msg: 'fio mixed randread and sequential write test: sequential write latency of tikv_data_dir disk is too low: {{ disk_mix_write_lat.stdout }} ns > {{ max_ssd_mix_write_lat }} ns, it is strongly recommended to use SSD disks for TiKV and PD, or there might be performance issues.' 47 # when: disk_mix_write_lat.stdout|int > max_ssd_mix_write_lat|int
-
也是磁盤轉數
[yx@pd3 tidb-ansible]$ vim bootstrap.yml 42 # - { role: machine_benchmark, when: not dev_mode|default(false) }
-
註釋掉監控主機檢測
[yx@pd3 tidb-ansible]$ vim deploy.yml 23 # - check_config_static [yx@pd3 tidb-ansible]$ vim bootstrap.yml 21 #- check_config_static [yx@pd3 tidb-ansible]$ vim start.yml 23 #- check_config_static [yx@pd3 tidb-ansible]$ vim stop.yml 23 # - check_config_static
-
開始初始化系統參數
[yx@pd3 tidb-ansible]$ ansible-playbook bootstrap.yml -T 600 ... PLAY RECAP ********************************************************************************************************************************************************************************** 192.168.9.91 : ok=34 changed=8 unreachable=0 failed=0 192.168.9.92 : ok=34 changed=8 unreachable=0 failed=0 192.168.9.93 : ok=33 changed=9 unreachable=0 failed=0 192.168.9.94 : ok=34 changed=8 unreachable=0 failed=0 192.168.9.95 : ok=34 changed=8 unreachable=0 failed=0 192.168.9.96 : ok=34 changed=8 unreachable=0 failed=0 localhost : ok=2 changed=2 unreachable=0 failed=0 Congrats! All goes well. :-)
-
部署 TiDB 集羣軟件
[yx@pd3 tidb-ansible]$ ansible-playbook deploy.yml -T 600 ... PLAY RECAP ********************************************************************************************************************************************************************************** 192.168.9.91 : ok=76 changed=34 unreachable=0 failed=0 192.168.9.92 : ok=76 changed=34 unreachable=0 failed=0 192.168.9.93 : ok=52 changed=23 unreachable=0 failed=0 192.168.9.94 : ok=63 changed=26 unreachable=0 failed=0 192.168.9.95 : ok=63 changed=26 unreachable=0 failed=0 192.168.9.96 : ok=63 changed=26 unreachable=0 failed=0 Congrats! All goes well. :-)
-
啓動 TiDB 集羣
[yx@pd3 tidb-ansible]$ ansible-playbook start.yml PLAY RECAP ********************************************************************************************************************************************************************************** 192.168.9.91 : ok=15 changed=4 unreachable=0 failed=0 192.168.9.92 : ok=15 changed=4 unreachable=0 failed=0 192.168.9.93 : ok=12 changed=3 unreachable=0 failed=0 192.168.9.94 : ok=14 changed=3 unreachable=0 failed=0 192.168.9.95 : ok=14 changed=3 unreachable=0 failed=0 192.168.9.96 : ok=14 changed=3 unreachable=0 failed=0 Congrats! All goes well. :-)
-
如果啓動失敗,記得先執行
ansible-playbook stop.yml 然後再重新start.yml
將tidb-ansible部署到另外一臺服務器上,以防第一臺中控機掛掉
-
部署第二臺中控機,記住這個是在tidb上執行的。本實驗ip爲192.168.9.92,手動拷貝公鑰的時候注意換行問題
yum -y install epel-release git curl sshpass yum -y install python-pip 拷貝第一臺中控機的tidb-ansible到本地 cd /home/yx/server/tidb-ansible sudo pip install -r ./requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ssh-keygen -t rsa ansible-playbook -i hosts.ini create_users.yml -u root -k //這一步應該可以省略掉
2.執行完以上命令就可以在執行ansible-playbook stop.yml -l 192.168.9.95了
查看TIDB和Tispark集羣
-
測試連接 TiDB 集羣,推薦在 TiDB 前配置負載均衡來對外統一提供 SQL 接口。
-
使用 MySQL 客戶端連接測試,TCP 4000 端口是 TiDB 服務默認端口。
mysql -u root -h 192.168.9.91 -P 4000 SET PASSWORD FOR 'root'@'192.168.2.%' = 'Wdty@2014'; GRANT ALL PRIVILEGES ON *.* TO 'wdty'@'192.168.2.%' WITH GRANT OPTION; SET PASSWORD FOR 'wdty'@'192.168.2.%' = 'Wdty@2014';
-
通過瀏覽器訪問監控平臺。
http://192.168.9.93:3000 默認帳號密碼是:admin/admin
-
查看TIspark集羣,本次實驗應該是隻顯示一個93的,截圖之所以多了一個是因爲下一步我將手動添加一臺TIspark slave]()
添加Tispark slave
-
Spark 推薦 32G 內存以上的配額。請在配置中預留 25% 的內存給操作系統。
-
Spark 推薦每臺計算節點配備 CPU 累計 8 到 16 核以上。你可以初始設定分配所有 CPU 核給 Spark。
-
Spark 的具體配置方式也請參考官方說明。以下爲根據 spark-env.sh 配置的範例:
SPARK_EXECUTOR_MEMORY=32g SPARK_WORKER_MEMORY=32g SPARK_WORKER_CORES=8
手動添加Tispark,不用在tidb-ansible中進行任何操作
-
此處新添加的Tispark角色是在92服務器
-
拷貝93的deploy下的整個spark目錄到92的deploy下
[yx@pd3 ~]$ scp -r ~/server/deploy/spark 192.168.9.92:/home/yx/server/deploy/
-
編輯配置文件,添加或修改pd配置
[yx@dbpd2 ~]$ vim ~/server/deploy/spark/conf/spark-defaults.conf ... spark.master spark://192.168.9.93:7077 spark.tispark.pd.addresses 192.168.9.91:2379,192.168.9.92:2379,192.168.9.93:2379 [yx@dbpd2 conf]$ cat spark-env.sh SPARK_MASTER_HOST=192.168.9.93 //最後的這個要寫對,必須是master的ip
-
TiSpark 需要 JDK 1.8+ 以及 Scala 2.11(Spark2.0+ 默認 Scala 版本)。 此處只需要安裝jdk就好
[yx@dbpd2 ~]$ tar zxvf jdk-1.8.tar.gz && mv jdk ~/server/ [yx@dbpd2 ~]$ sudo vim /etc/profile export JAVA_HOME=/home/yx/server/jdk export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH [yx@dbpd2 ~]$ source /etc/profile [yx@dbpd2 ~]$ java -version java version "1.8.0_91" Java(TM) SE Runtime Environment (build 1.8.0_91-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
-
啓動93的Tispark master,進入/home/yx/server/deploy/spark/sbin路徑下,如果TIspark的master以及啓動就不需要再次啓動了。
[yx@pd3 sbin]$ ./start-master.sh starting org.apache.spark.deploy.master.Master, logging to /home/yx/server/deploy/spark/logs/spark-yx-org.apache.spark.deploy.master.Master-1-dbpd2.out
-
啓動92的Tispark slave。
[yx@dbpd2 sbin]$ ./start-slave.sh spark://192.168.9.93:7077 starting org.apache.spark.deploy.worker.Worker, logging to /home/yx/server/deploy/spark/logs/spark-yx-org.apache.spark.deploy.worker.Worker-1-dbpd1.out
-
此時在Workers 下已經出現了我們新添加的92 slave
通過tidb-ansible添加Tispark
-
切換到93服務器,也就是中控機。編輯inventory.ini文件
[yx@pd3 tidb-ansible]$ vim inventory.ini [spark_master] 192.168.9.93 [spark_slaves] 192.168.9.91
-
初始化新添加的Tispark
[yx@pd3 tidb-ansible]$ ansible-playbook bootstrap.yml
-
部署Tispark的slave
[yx@pd3 tidb-ansible]$ ansible-playbook deploy.yml -T 600
-
啓動集羣
[yx@pd3 tidb-ansible]$ ansible-playbook start.yml [yx@pd3 tidb-ansible]$ ansible-playbook start.yml -l 192.168.9.91 //此方法沒有經過測試,但感覺可行
-
記得在http://192.168.9.93:8080/ 頁面驗證是否添加成功。
TIDB的擴容縮容
參考官網:https://pingcap.com/docs-cn/dev/how-to/scale/with-ansible/
-
無論是擴容哪個角色都需要提前執行的初始化命令,此處實驗用的是新服務器,如果是已有服務的注意公鑰
新增節點上執行以下命令 [yx@kv5 ~]$ sudo vim /etc/security/limits.conf yx - nofile 1000000 [yx@kv5 ~]$ sudo vim /etc/pam.d/login #此處系統爲Centos 7 ,路徑是lib64,其它系統有可能是lib session required /lib64/security/pam_limits.so [yx@kv5 ~]$ mkdir ~/.ssh [yx@kv5 ~]$ chmod 700 ~/.ssh/ [yx@kv5 ~]$ chmod 600 ~/.ssh/authorized_keys #此處需要注意,如果中控機只有一臺可以直接將中控機的id_rsa.pub拷貝到新的kv服務器,如果有多臺中控機,記得將它們的公鑰全部拷到新添加的角色服務器。 #在中控機上執行 [yx@dbpd2 tidb-ansible]$ scp -p ~/.ssh/id_rsa.pub [email protected]:/home/yx/.ssh/authorized_keys [yx@kv5 ~]$ chmod 600 ~/.ssh/authorized_keys
KV&TIDB擴容
-
KV和TIDB添加方法完全一樣,注意修改host.ini和inventory.ini文件時ip放在相對應的模組裏就好。
-
注意:如果有兩臺中控機的話,記得保持host.ini和inventory.ini以及ssh密鑰的一致性。
-
新加一臺kv4,ip爲192.168.9.97 。在中控機上執行
[yx@dbpd2 tidb-ansible]$ vim hosts.ini [servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97
-
編輯
inventory.ini
文件,添加節點信息,此處添加的是kv:
[yx@dbpd2 tidb-ansible]$ vim inventory.ini ... [tikv_servers] 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 ... [monitored_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97
-
執行以下命令,按提示輸入部署目標機器
root
用戶密碼。該步驟將在部署目標機器上創建 yx 用戶,並配置 sudo 規則,配置中控機與部署目標機器之間的 ssh 互信。
[yx@dbpd2 tidb-ansible]$ ansible-playbook -i hosts.ini create_users.yml -u root -k -l 192.168.9.97
-
在新的kv上安裝ntp服務並啓動
[yx@dbpd2 tidb-ansible]$ ansible-playbook -i hosts.ini deploy_ntp.yml -u yx -b -T 600 -l 192.168.9.97
-
初始化新增kv
[yx@dbpd2 tidb-ansible]$ ansible-playbook bootstrap.yml -l 192.168.9.97 #如果一次新增多個節點可以用逗號隔開ip,例如 #ansible-playbook bootstrap.yml -l 192.168.9.97,192.168.9.98
-
部署並啓動新增節點:
[yx@dbpd2 tidb-ansible]$ ansible-playbook deploy.yml -l 192.168.9.97 #啓動新增的kv [yx@dbpd2 tidb-ansible]$ ansible-playbook start.yml -l 192.168.9.97
-
滾動升級監控:
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
-
查看TIDB監控
PD擴容
-
此處新加pd服務器ip爲:192.168.9.98
[yx@dbpd2 tidb-ansible]$ vim hosts.ini [servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 192.168.9.98
-
添加PD角色配置
[yx@dbpd2 tidb-ansible]$ vim inventory.ini [pd_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.98 ... [monitored_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 192.168.9.98
-
初始化新增的PD節點
[yx@dbpd2 tidb-ansible]$ ansible-playbook bootstrap.yml -l 192.168.9.98
-
部署新增節點
[yx@dbpd2 tidb-ansible]$ ansible-playbook deploy.yml -l 192.168.9.98
-
登錄新增的 PD 節點,編輯啓動腳本:
{deploy_dir}/scripts/run_pd.sh
[yx@pd4 ~]$ vim ~/server/deploy/scripts/run_pd.sh #刪除第20行 20 --initial-cluster="pd_dbpd1=http://192.168.9.91:2380,pd_dbpd2=http://192.168.9.92:2380,pd_pd3=http://192.168.9.93:2380,pd_pd4=http://192.168.9.98:2380" \ #新加一行,IP 地址 (192.168.9.93 可以是集羣內現有 PD IP 地址中的任意一個。 --join="http://192.168.9.93:2379" #在新增 PD 節點中手動啓動 PD 服務: [yx@pd4 ~]$ bash ~/server/deploy/scripts/start_pd.sh
-
登錄中控機,使用
pd-ctl
檢查新節點是否添加成功:
[yx@dbpd2 ~]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.98:2379" -d member { "header": { "cluster_id": 6699372971722481899 }, "members": [ { "name": "pd_pd3", "member_id": 850721977124358136, "peer_urls": [ "http://192.168.9.93:2380" ], "client_urls": [ "http://192.168.9.93:2379" ] }, { "name": "pd_dbpd2", "member_id": 7856605665486128202, "peer_urls": [ "http://192.168.9.92:2380" ], "client_urls": [ "http://192.168.9.92:2379" ] }, { "name": "pd_dbpd1", "member_id": 9849683487793918155, "peer_urls": [ "http://192.168.9.91:2380" ], "client_urls": [ "http://192.168.9.91:2379" ] }, { "name": "pd_pd4", "member_id": 13092416884020586414, "peer_urls": [ "http://192.168.9.98:2380" ], "client_urls": [ "http://192.168.9.98:2379" ] } ], "leader": { "name": "pd_pd4", "member_id": 13092416884020586414, "peer_urls": [ "http://192.168.9.98:2380" ], "client_urls": [ "http://192.168.9.98:2379" ] }, "etcd_leader": { "name": "pd_pd4", "member_id": 13092416884020586414, "peer_urls": [ "http://192.168.9.98:2380" ], "client_urls": [ "http://192.168.9.98:2379" ] } }
-
滾動升級整個集羣
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update.yml
-
啓動監控服務
[yx@dbpd2 tidb-ansible]$ ansible-playbook start.yml -l 192.168.9.93
-
更新 Prometheus 配置並重啓
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
-
打開瀏覽器訪問監控平臺:
http://192.168.9.93:3000
,監控整個集羣和新增節點的狀態。
PD縮容
-
關閉一個節點,此處爲98
[yx@dbpd2 tidb-ansible]$ ansible-playbook stop.yml -l 192.168.9.98
-
然後編輯文件,將該服務器刪除
[yx@dbpd2 tidb-ansible]$ vim hosts.ini [servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 192.168.9.98 //刪除 [yx@dbpd2 tidb-ansible]$ vim inventory.ini [pd_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.98 //刪除 ... [monitored_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 192.168.9.98 //刪除
-
更新 Prometheus 配置並重啓
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
-
查看監控,應該會少一個pd節點,此處監控更新時間略長
TIKV縮容
-
使用
pd-ctl
從集羣中移除節點,此處移除的是97
#查看 node9 節點的 store id:此處的91是tidb,當前kv狀態是up [yx@dbpd2 tidb-ansible]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.92:2379" -d store ... { "store": { "id": 1001, "address": "192.168.9.97:20160", "version": "2.1.11", "state_name": "Up" }, "status": { "capacity": "441 GiB", "available": "440 GiB", "leader_weight": 1, "region_count": 7, "region_weight": 1, "region_score": 7, "region_size": 7, "start_ts": "2019-06-11T18:48:32+08:00", "last_heartbeat_ts": "2019-06-11T20:45:35.105710941+08:00", "uptime": "1h57m3.105710941s" } } ] } #從集羣中移除 KV4,假如 store id 爲 1001: [yx@dbpd1 tidb-ansible]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.91:2379" -d store delete 1001 Success!
-
使用 Grafana 或者
pd-ctl
檢查節點是否下線成功(下線需要一定時間,下線節點的狀態變爲 Tombstone 就說明下線成功了)
#此狀態變成了Tombstone,說明下線成功,監控的話需要延遲一段時間 [yx@dbpd2 tidb-ansible]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.92:2379" -d store 1001 { "store": { "id": 1001, "address": "192.168.9.97:20160", "state": 2, "version": "2.1.11", "state_name": "Tombstone" }, "status": { "capacity": "441 GiB", "available": "440 GiB", "leader_weight": 1, "region_weight": 1, "start_ts": "2019-06-11T18:48:32+08:00", "last_heartbeat_ts": "2019-06-11T20:48:16.404973464+08:00", "uptime": "1h59m44.404973464s" } }
-
下線成功後,停止 該kv上的服務
[yx@dbpd2 tidb-ansible]$ ansible-playbook stop.yml -l 192.168.9.97
-
編輯
inventory.ini
文件,移除節點信息
[yx@dbpd2 tidb-ansible]$ vim hosts.ini [servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 //刪除 [yx@dbpd2 tidb-ansible]$ vim inventory.ini [tikv_servers] 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 //刪除 ... [monitored_servers] 192.168.9.91 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 192.168.9.97 //刪除
-
查看監控,此時會提示TIKV斷掉
-
更新 Prometheus 配置並重啓
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
-
此處TIKV已經成功下線
TIDB縮容
-
使用
pd-ctl
從集羣中移除節點,此處移除的是91
#查看 PD 節點的 name: [yx@dbpd2 tidb-ansible]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.92:2379" -d member { "header": { "cluster_id": 6699372971722481899 }, "members": [ { "name": "pd_192.168.9.91", "member_id": 850721977124358136, "peer_urls": [ "http://192.168.9.91:2380" ], "client_urls": [ "http://192.168.9.91:2379" ] }, ... #從集羣中移除 91,假如 name 爲 pd_192.168.9.91: [yx@dbpd1 tidb-ansible]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.92:2379" -d member delete name pd_192.168.9.91 Success!
-
使用
pd-ctl
檢查節點是否下線成功(TIDB下線會很快,結果中沒有 91節點信息即爲下線成功)
[yx@dbpd2 tidb-ansible]$ /home/yx/server/tidb-ansible/resources/bin/pd-ctl -u "http://192.168.9.92:2379" -d member { "header": { "cluster_id": 6699372971722481899 }, "members": [ { "name": "pd_192.168.9.92", "member_id": 7856605665486128202, "peer_urls": [ "http://192.168.9.92:2380" ], "client_urls": [ "http://192.168.9.92:2379" ] }, { "name": "pd_192.168.9.93", "member_id": 9849683487793918155, "peer_urls": [ "http://192.168.9.93:2380" ], "client_urls": [ "http://192.168.9.93:2379" ] }, { "name": "pd_192.168.9.98", "member_id": 13092416884020586414, "peer_urls": [ "http://192.168.9.98:2380" ], "client_urls": [ "http://192.168.9.98:2379" ] } ], "leader": { "name": "pd_192.168.9.93", "member_id": 9849683487793918155, "peer_urls": [ "http://192.168.9.91:2380" ], "client_urls": [ "http://192.168.9.91:2379" ] }, "etcd_leader": { "name": "pd_192.168.9.93", "member_id": 9849683487793918155, "peer_urls": [ "http://192.168.9.93:2380" ], "client_urls": [ "http://192.168.9.93:2379" ] } }
-
下線成功後,停止91的TIDB服務
[yx@dbpd2 tidb-ansible]$ ansible-playbook stop.yml -l 192.168.9.91
-
編輯
inventory.ini
文件,移除節點信息
[yx@dbpd2 tidb-ansible]$ vim hosts.ini [servers] 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96 [yx@dbpd2 tidb-ansible]$ vim inventory.ini [tidb_servers] 192.168.9.92 192.168.9.93 ... [monitored_servers] 192.168.9.92 192.168.9.93 192.168.9.94 192.168.9.95 192.168.9.96
-
滾動升級整個集羣
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update.yml
-
更新 Prometheus 配置並重啓
[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
-
查看監控,TIDB已經更新完成
TIDB數據的導出與導入
導出
-
此處推薦使用的工具爲mydumper
-
官方文檔:https://pingcap.com/docs-cn/dev/how-to/migrate/from-mysql/
-
官方文檔:https://pingcap.com/docs-cn/dev/how-to/migrate/from-mysql/
-
安裝依賴包,建議
sudo yum install glib2-devel mysql-devel zlib-devel pcre-devel zlib gcc-c++ gcc cmake -y
-
下載mydumper源碼包
git clone https://github.com/maxbube/mydumper.git cd mydumper/ cmake . make && sudo make install which mydumper /usr/local/bin/mydumper
-
導出MySQL數據庫,本次測試儘量選擇大的數據庫
mydumper -S /var/run/mysqld/mysqld.sock -u root -p 123456 -t 8 -F 64 -B p2peye --skip-tz-utc -o ./mayc/p2peye-20190612-tidb #以下是本次命令詳解 mydumper/loader 全量導入數據最佳實踐 爲了快速的遷移數據 (特別是數據量巨大的庫),可以參考以下建議: mydumper 導出數據至少要擁有 SELECT,RELOAD,LOCK TABLES 權限 使用 mydumper 導出來的數據文件儘可能的小,最好不要超過 64M,可以設置參數 -F 64 loader的 -t 參數可以根據 TiKV 的實例個數以及負載進行評估調整,例如 3個 TiKV 的場景,此值可以設爲 3 *(1 ~ n);當 TiKV 負載過高,loader 以及 TiDB 日誌中出現大量 backoffer.maxSleep 15000ms is exceeded 可以適當調小該值,當 TiKV 負載不是太高的時候,可以適當調大該值。 導入示例及相關配置: - mydumper 導出後總數據量 214G,單表 8 列,20 億行數據 - 集羣拓撲 - TiKV * 12 - TiDB * 4 - PD * 3 - mydumper -F 設置爲 16,Loader -t 參數設置爲 64 結果:導入時間 11 小時左右,19.4 G/小時 從 MySQL 導出數據 我們使用 mydumper 從 MySQL 導出數據,如下: ./bin/mydumper -h 127.0.0.1 -P 3306 -u root -t 16 -F 64 -B test -T t1,t2 --skip-tz-utc -o ./var/test 上面,我們使用 -B test 表明是對 test 這個 database 操作,然後用 -T t1,t2 表明只導出 t1,t2 兩張表。 -t 16 表明使用 16 個線程去導出數據。-F 64 是將實際的 table 切分成多大的 chunk,這裏就是 64MB 一個 chunk。 --skip-tz-utc 添加這個參數忽略掉 MySQL 與導數據的機器之間時區設置不一致的情況,禁止自動轉換。
導入
-
安裝TIDB導入SQL文件的官方插件
wget http://download.pingcap.org/tidb-enterprise-tools-latest-linux-amd64.tar.gz tar zxvf tidb-enterprise-tools-latest-linux-amd64.tar.gz cd tidb-enterprise-tools-latest-linux-amd64/ #解壓後就能用。都在bin下
-
導入到TIDB中
./bin/loader -h 192.168.9.92 -u root -P 4000 -t 3 -d ~/mayc/p2peye-20190612-tidb/
安裝過程中遇到的問題
手動安裝NTP服務
-
先檢測 NTP 服務是否正常(此處步驟可以跳過,只要確保服務器時間是正確同步就好)
執行以下命令輸出
running
表示 NTP 服務正在運行:[root@localhost yum.repos.d]# sudo systemctl status ntpd.service ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since 三 2019-03-20 15:46:01 CST; 2min 26s ago Process: 8009 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 8010 (ntpd) CGroup: /system.slice/ntpd.service └─8010 /usr/sbin/ntpd -u ntp:ntp -g 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listen and drop on 1 v6wildcard :: UDP 123 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listen normally on 2 lo 127.0.0.1 UDP 123 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listen normally on 3 ens160 192.168.13.199 UDP 123 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listen normally on 4 lo ::1 UDP 123 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listen normally on 5 ens160 fe80::cbc3:a5c6:e2ee:5ed UDP 123 3月 20 15:46:01 localhost.localdomain ntpd[8010]: Listening on routing socket on fd #22 for interface updates 3月 20 15:46:01 localhost.localdomain ntpd[8010]: 0.0.0.0 c016 06 restart 3月 20 15:46:01 localhost.localdomain ntpd[8010]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM 3月 20 15:46:01 localhost.localdomain ntpd[8010]: 0.0.0.0 c011 01 freq_not_set
-
安裝NTP服務
sudo yum install ntp ntpdate -y sudo ntpdate pool.ntp.org sudo systemctl start ntpd.service sudo systemctl enable ntpd.service
-
驗證
[root@localhost yum.repos.d]# ntpstat synchronised to NTP server (119.28.206.193) at stratum 3 time correct to within 1011 ms polling server every 64 s