TIDB集群搭建-流水线

官方文档: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

  1. 关闭ssh服务的dns反解析,很重要,关系到后期是否能成功启动。

     vim /etc/ssh/sshd_config
     UseDNS no
     systemctl reload sshd.service
  2. 此次实验选择的中控机是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

  1. 在中控机器上下载 TiDB-Ansible 2.1,此处是93服务器

 [yx@pd3 server]$ git clone -b release-2.1 https://github.com/pingcap/tidb-ansible.git
  1. 以 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互信规则

  1. 在中控机上配置部署机器 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
  1. 执行以下命令,按提示输入部署目标机器 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. :-)
  1. 创建完成后可以ssh试下能否免密登录其他机器。sudo也是不要密码的。

  2. 可选项:如果其他服务器都是新的,没有安装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. :-)

 

分配角色,并完成初始化以及部署

  1. 对照你的角色分配表,配置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	//集群名称
  1. 执行以下命令如果所有 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
  1. 执行以下命令如果所有 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
  1. 执行 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. :-)
  1. 初始化系统环境,修改内核参数,因为本次实验所采用硬盘为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
  1. 开始初始化系统参数

[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. :-)
  1. 部署 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. :-)
  1. 启动 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部署到另外一台服务器上,以防第一台中控机挂掉

  1. 部署第二台中控机,记住这个是在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集群

  1. 测试连接 TiDB 集群,推荐在 TiDB 前配置负载均衡来对外统一提供 SQL 接口。

  2. 使用 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';
  1. 通过浏览器访问监控平台。

http://192.168.9.93:3000 默认帐号密码是:admin/admin

  1. 查看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中进行任何操作

  1. 此处新添加的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
  1. 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)
  1. 启动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
  1. 启动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
  1. 此时在Workers 下已经出现了我们新添加的92 slave

通过tidb-ansible添加Tispark

  1. 切换到93服务器,也就是中控机。编辑inventory.ini文件

[yx@pd3 tidb-ansible]$ vim inventory.ini 
[spark_master]
192.168.9.93

[spark_slaves]
192.168.9.91
  1. 初始化新添加的Tispark

[yx@pd3 tidb-ansible]$ ansible-playbook bootstrap.yml
  1. 部署Tispark的slave

[yx@pd3 tidb-ansible]$ ansible-playbook deploy.yml -T 600
  1. 启动集群

[yx@pd3 tidb-ansible]$ ansible-playbook start.yml
[yx@pd3 tidb-ansible]$ ansible-playbook start.yml -l 192.168.9.91 //此方法没有经过测试,但感觉可行
  1. 记得在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
  1. 编辑 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
  1. 执行以下命令,按提示输入部署目标机器 root 用户密码。该步骤将在部署目标机器上创建 yx 用户,并配置 sudo 规则,配置中控机与部署目标机器之间的 ssh 互信。

[yx@dbpd2 tidb-ansible]$ ansible-playbook -i hosts.ini create_users.yml -u root -k -l 192.168.9.97
  1. 在新的kv上安装ntp服务并启动

[yx@dbpd2 tidb-ansible]$ ansible-playbook -i hosts.ini deploy_ntp.yml -u yx -b -T 600 -l 192.168.9.97
  1. 初始化新增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
  1. 部署并启动新增节点:

[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
  1. 滚动升级监控:

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
  1. 查看TIDB监控

PD扩容

  1. 此处新加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
  1. 添加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
  1. 初始化新增的PD节点

[yx@dbpd2 tidb-ansible]$ ansible-playbook bootstrap.yml -l 192.168.9.98
  1. 部署新增节点

[yx@dbpd2 tidb-ansible]$ ansible-playbook deploy.yml -l 192.168.9.98
  1. 登录新增的 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
  1. 登录中控机,使用 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"
    ]
  }
}
  1. 滚动升级整个集群

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update.yml
  1. 启动监控服务

[yx@dbpd2 tidb-ansible]$ ansible-playbook start.yml -l 192.168.9.93
  1. 更新 Prometheus 配置并重启

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
  1. 打开浏览器访问监控平台:http://192.168.9.93:3000,监控整个集群和新增节点的状态。

PD缩容

  1. 关闭一个节点,此处为98

[yx@dbpd2 tidb-ansible]$ ansible-playbook stop.yml -l 192.168.9.98
  1. 然后编辑文件,将该服务器删除

[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	//删除
  1. 更新 Prometheus 配置并重启

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
  1. 查看监控,应该会少一个pd节点,此处监控更新时间略长

TIKV缩容

  1. 使用 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!
  1. 使用 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"
  }
}
  1. 下线成功后,停止 该kv上的服务

[yx@dbpd2 tidb-ansible]$ ansible-playbook stop.yml -l 192.168.9.97
  1. 编辑 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	//删除
  1. 查看监控,此时会提示TIKV断掉

  1. 更新 Prometheus 配置并重启

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
  1. 此处TIKV已经成功下线

TIDB缩容

  1. 使用 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!
  1. 使用 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"
    ]
  }
}
  1. 下线成功后,停止91的TIDB服务

[yx@dbpd2 tidb-ansible]$ ansible-playbook stop.yml -l 192.168.9.91
  1. 编辑 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
  1. 滚动升级整个集群

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update.yml
  1. 更新 Prometheus 配置并重启

[yx@dbpd2 tidb-ansible]$ ansible-playbook rolling_update_monitor.yml --tags=prometheus
  1. 查看监控,TIDB已经更新完成

TIDB数据的导出与导入

导出

  1. 安装依赖包,建议

sudo  yum install glib2-devel mysql-devel zlib-devel pcre-devel zlib gcc-c++ gcc cmake -y
  1. 下载mydumper源码包

git clone https://github.com/maxbube/mydumper.git
cd mydumper/
cmake .
make && sudo make install
which mydumper 
/usr/local/bin/mydumper
  1. 导出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 与导数据的机器之间时区设置不一致的情况,禁止自动转换。

 

导入

  1. 安装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下
  1. 导入到TIDB中

./bin/loader -h 192.168.9.92 -u root -P 4000 -t 3 -d ~/mayc/p2peye-20190612-tidb/

 

安装过程中遇到的问题

手动安装NTP服务

  1. 先检测 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
    
  2. 安装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
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章