tidb安装

1.准备机器

组件 IP 操作系统版本
TIKV 192.168.3.81,192.168.3.82,192.168.3.83 CentOS 7.5
PD 192.168.3.84 CentOS 7.5
TIDB 192.168.3.84 CentOS 7.5
中控机 192.168.3.85 CentOS 7.5

2.在中控机安装系统依赖包

yum -y install epel-release git curl sshpass
yum -y install python-pip

3.在中控机上创建tidb用户,并生成ssh key

以root用户登录中控机,执行以下命令创建tidb用户
useradd -m -d /home/tidb tidb
设置tidb用户密码
passwd tidb
配置tidb用户 sudo 免密码,将tidb ALL=(ALL) NOPASSWD: ALL添加到文件末尾即可。

vim /etc/sudoers
tidb ALL=(ALL) NOPASSWD: ALL

生成 ssh key: 执行 su 命令从 root 用户切换到 tidb 用户下。
su - tidb

创建 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:eCHuCQdBeRSql3DlJK27AKjbpGb0HuXcvkNXE/iVjok tidb@tidb05_192.168.3.85
The key's randomart image is:
+---[RSA 2048]----+
|   .++=.  .   .  |
|    .Bo  . . o   |
|. . +oo . o *    |
|o  +.+ o E * .   |
|... +o+ S . .    |
|..o.=+.+ .       |
|.=.o ++..        |
|oo..o ..         |
|o ..   oo        |
+----[SHA256]-----+

4.在中控机器上下载 TiDB-Ansible

以 tidb 用户登录中控机并进入 /home/tidb 目录。
使用以下命令从 Github TiDB-Ansible 项目上下载 TiDB-Ansible 相应分支,默认的文件夹名称为tidb-ansible。

  • 下载 2.0 版本:

git clone -b release-2.0 https://github.com/pingcap/tidb-ansible.git

注:请务必按文档操作,将 tidb-ansible 下载到 /home/tidb 目录下,权限为 tidb 用户,不要下载到 /root 下,否则会遇到权限问题。

5.在中控机器上安装 Ansible 及其依赖

以tidb用户登录中控机,请务必按以下方式通过 pip 安装 Ansible 及其相关依赖的指定版本,否则会有兼容问题。安装完成后,可通过ansible --versoin查看 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
You should consider upgrading via the 'pip install --upgrade pip' command.
[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$ ansible --version
ansible 2.6.15
  config file = /home/tidb/tidb-ansible/ansible.cfg
  configured module search path = [u'/home/tidb/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

6.在中控机上配置部署机器 ssh 互信及 sudo 规则

以 tidb 用户登录中控机,将你的部署目标机器 IP 添加到 hosts.ini 文件 [servers] 区块下。

[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$cd /home/tidb/tidb-ansible/
[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$cat hosts.ini 
[servers]
192.168.3.81
192.168.3.82
192.168.3.83
192.168.3.84

[all:vars]
username = tidb
ntp_server = pool.ntp.org

执行以下命令,按提示输入部署目标机器root用户密码。该步骤将在部署目标机器上创建tidb用户,并配置 sudo 规则,配置中控机与部署目标机器之间的 ssh 互信。
ansible-playbook -i hosts.ini create_users.yml -u root -k

7.在部署目标机器上安装 NTP 服务

该步骤将在部署目标机器上使用系统自带软件源联网安装并启动 NTP 服务,服务使用安装包默认的 NTP server 列表,见配置文件 /etc/ntp.conf 中 server 参数,如果使用默认的 NTP server,你的机器需要连接外网。 为了让 NTP 尽快开始同步,启动 NTP 服务前,系统会 ntpdate hosts.ini 文件中的 ntp_server 一次,默认为 pool.ntp.org,也可替换为你的 NTP server。

cd /home/tidb/tidb-ansible
ansible-playbook -i hosts.ini deploy_ntp.yml -u tidb -b

8.在部署目标机器上配置 CPUfreq 调节器模式

为了让 CPU 发挥最大性能,请将 CPUfreq 调节器模式设置为performanc模式。

你可以通过 cpupower 命令查看系统支持的调节器模式:

[tidb@centos7_05 tidb-ansible]$ cpupower frequency-info --governors
analyzing CPU 0:
  available cpufreq governors: performance powersave

本例中系统支持设置 performance 和 powersave 模式。如果返回 “Not Available”,表示当前系统不支持配置 CPUfreq,跳过该步骤即可。

你可以通过cpupower 命令查看系统当前的 CPUfreq 调节器模式:

本例中当前配置是 powersave 模式,你可以通过以下命令设置为 performance 模式。
你也可以通过以下命令在部署目标机器上批量设置:

$ ansible -i hosts.ini all -m shell -a "cpupower frequency-set --governor performance" -u tidb -b

9.在部署目标机器上添加数据盘 ext4 文件系统挂载参数

部署目标机器数据盘请格式化成 ext4 文件系统,挂载时请添加 nodelalloc 和 noatime 挂载参数。nodelalloc是必选参数,否则 Ansible 安装时检测无法通过,noatime 是可选建议参数。

下面以 /dev/sdb 数据盘为例:

查看数据盘

fdisk -l
Disk /dev/sdb: 21.5 GB

创建分区表

parted -s -a optimal /dev/sdb mklabel gpt -- mkpart primary ext4 1 -1

格式化文件系统

mkfs.ext4 /dev/sdb

查看数据盘分区 UUID,本例中 sdb 的 UUID 为 b42e4005-afdc-4abe-822c-3d766f01b7f5。

[root@tidb01_192.168.3.81 ~]#lsblk -f
NAME            FSTYPE      LABEL           UUID                                   MOUNTPOINT
sda                                                                                
├─sda1          xfs                         97c182dd-9521-4ab9-99f0-a485dfa9559d   /boot
└─sda2          LVM2_member                 faVDCu-Eej4-EGru-Zvt0-c6BK-6MaU-gY8cgk 
├─centos-root xfs                         e9acfba6-573a-4b6a-9aac-2af2cb69f28d   /
├─centos-swap swap                        ccd05e39-541c-4919-80f8-238e89119e82   [SWAP]
└─centos-home xfs                         7bca643f-5eec-4a9f-b6e1-39af9a435e13   /home
sdb             ext4                        b42e4005-afdc-4abe-822c-3d766f01b7f5   
sr0             iso9660     CentOS 7 x86_64 2018-05-03-20-55-23-00        

编辑 /etc/fstab 文件,添加 nodelalloc 挂载参数

vi /etc/fstab
UUID=b42e4005-afdc-4abe-822c-3d766f01b7f5 /data ext4 defaults,nodelalloc,noatime 0 2

挂载数据盘

mkdir /data
mount -a

执行以下命令,如果文件系统为 ext4,并且挂载参数中包含 nodelalloc 表示生效:

[root@tidb01_192.168.3.81 ~]#mount -t ext4
/dev/sdb on /data type ext4 (rw,noatime,nodelalloc,data=ordered)

10.分配机器资源,编辑 inventory.ini 文件

以 tidb 用户登录中控机,inventory.ini 文件路径为 /home/tidb/tidb-ansible/inventory.ini。
标准 TiDB 集群需要 6 台机器:

  • 2 个 TiDB 节点
  • 3 个 PD 节点
  • 3 个 TiKV 节点,第一台 TiDB 机器同时用作监控机

默认情况下,单台机器上只需部署一个 TiKV 实例。如果你的 TiKV 部署机器 CPU 及内存配置是部署建议的两倍或以上,并且拥有两块 SSD 硬盘或单块容量超 2T 的 SSD 硬盘,
可以考虑部署两实例,但不建议部署两个以上实例。

单机单 TiKV 实例集群拓扑

Name HOST IP Services
tidb01_192.168.3.81 192.168.3.81 TiKV1
tidb02_192.168.3.82 192.168.3.82 TiKV2
tidb03_192.168.3.83 192.168.3.83 TiKV3
tidb04_192.168.3.84 192.168.3.84 tidb/pd
[tidb_servers]
192.168.3.84

[tikv_servers]
192.168.3.81
192.168.3.82
192.168.3.83

[pd_servers]
192.168.3.84

[monitoring_servers]
192.168.3.84

[grafana_servers]
192.168.3.84

[monitored_servers]
192.168.3.81
192.168.3.82
192.168.3.83
192.168.3.84

11.部署目录调整

部署目录通过 deploy_dir 变量控制,默认全局变量已设置为 /home/tidb/deploy,对所有服务生效。如数据盘挂载目录为 /data,可设置为 /data/deploy,样例如下:

vim /home/tidb/tidb-ansible/inventory.ini
## Global variables
[all:vars]
deploy_dir = /data/deploy

12.部署任务

ansible-playbook 执行 Playbook 时默认并发为 5,部署目标机器较多时可添加 -f 参数指定并发,如 ansible-playbook deploy.yml -f 10

①确认 tidb-ansible/inventory.ini 文件中 ansible_user = tidb,本例使用 tidb 用户作为服务运行用户,配置如下:

## Connection
# ssh via normal user
ansible_user = tidb

ansible_user 不要设置成 root 用户,tidb-ansible 限制了服务以普通用户运行。

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

[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$ansible -i inventory.ini all -m shell -a 'whoami'
192.168.3.81 | SUCCESS | rc=0 >>
tidb

192.168.3.84 | SUCCESS | rc=0 >>
tidb

192.168.3.82 | SUCCESS | rc=0 >>
tidb

192.168.3.83 | SUCCESS | rc=0 >>
tidb

执行以下命令如果所有 server 返回 root 表示 tidb 用户 sudo 免密码配置成功。

[tidb@tidb05_192.168.3.85 ~/tidb-ansible]$ansible -i inventory.ini all -m shell -a 'whoami' -b
192.168.3.83 | SUCCESS | rc=0 >>
root

192.168.3.81 | SUCCESS | rc=0 >>
root

192.168.3.82 | SUCCESS | rc=0 >>
root

192.168.3.84 | SUCCESS | rc=0 >>
root

②执行 local_prepare.yml playbook,联网下载 TiDB binary 到中控机:
ansible-playbook local_prepare.yml

③初始化系统环境,修改内核参数
ansible-playbook bootstrap.yml

报错

TASK [check_system_optional : Preflight check - Check TiDB server's CPU] *************************************
fatal: [192.168.3.84]: FAILED! => {"changed": false, "msg": "This machine does not have sufficient CPU to run TiDB, at least 8 cores."}

NO MORE HOSTS LEFT *******************************************************************************************
        to retry, use: --limit @/home/tidb/tidb-ansible/retry_files/bootstrap.retry

PLAY RECAP ***************************************************************************************************
192.168.3.81               : ok=29   changed=11   unreachable=0    failed=0   
192.168.3.82               : ok=29   changed=11   unreachable=0    failed=0   
192.168.3.83               : ok=29   changed=11   unreachable=0    failed=0   
192.168.3.84               : ok=29   changed=11   unreachable=0    failed=1   
localhost                  : ok=1    changed=0    unreachable=0    failed=0   


ERROR MESSAGE SUMMARY ****************************************************************************************
[192.168.3.84]: Ansible FAILED! => playbook: bootstrap.yml; TASK: check_system_optional : Preflight check - Check TiDB server's CPU; message: {"changed": false, "msg": "This machine does not have sufficient CPU to run TiDB, at least 8 cores."}

Ask for help:
Contact us: support@pingcap.com
It seems that you encounter some problems. You can send an email to the above email address, attached with the tidb-ansible/inventory.ini and tidb-ansible/log/ansible.log files and the error message, or new issue on https://github.com/pingcap/tidb-ansible/issues. We'll try our best to help you deploy a TiDB cluster. Thanks. :-)

vim bootstrap.yml 修改该文件 并注释掉以下的内容

- name: check system
  hosts: all
  any_errors_fatal: true
  roles:
    - check_system_static
   # - { role: check_system_optional, when: not dev_mode|default(false) }

如果是非SSD 测试的话 ,最好将如下的内容注释掉

- name: tikv_servers machine benchmark
  hosts: tikv_servers
  gather_facts: false
  roles:
   # - { role: machine_benchmark, when: not dev_mode|default(false) }

④部署 TiDB 集群软件
ansible-playbook deploy.yml
报错

TASK [check_system_dynamic : Preflight check - NTP service] ***********************************************************************************************************
fatal: [192.168.3.81]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}
fatal: [192.168.3.82]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}
fatal: [192.168.3.83]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}
fatal: [192.168.3.84]: FAILED! => {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

NO MORE HOSTS LEFT ****************************************************************************************************************************************************
        to retry, use: --limit @/home/tidb/tidb-ansible/retry_files/deploy.retry

PLAY RECAP ************************************************************************************************************************************************************
192.168.3.81               : ok=4    changed=0    unreachable=0    failed=1   
192.168.3.82               : ok=3    changed=0    unreachable=0    failed=1   
192.168.3.83               : ok=3    changed=0    unreachable=0    failed=1   
192.168.3.84               : ok=3    changed=0    unreachable=0    failed=1   
localhost                  : ok=1    changed=0    unreachable=0    failed=0   


ERROR MESSAGE SUMMARY *************************************************************************************************************************************************
[192.168.3.81]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

[192.168.3.82]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

[192.168.3.83]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

[192.168.3.84]: Ansible FAILED! => playbook: deploy.yml; TASK: check_system_dynamic : Preflight check - NTP service; message: {"changed": false, "msg": "Make sure NTP service is running and ntpstat is synchronised to NTP server. See https://github.com/pingcap/docs/blob/master/op-guide/ansible-deployment.md#how-to-check-whether-the-ntp-service-is-normal ."}

Ask for help:
Contact us: support@pingcap.com
It seems that you encounter some problems. You can send an email to the above email address, attached with the tidb-ansible/inventory.ini and tidb-ansible/log/ansible.log files and the error message, or new issue on https://github.com/pingcap/tidb-ansible/issues. We'll try our best to help you deploy a TiDB cluster. Thanks. :-)

使用以下命令可使 NTP 服务尽快开始同步

sudo systemctl stop ntpd.service
sudo ntpdate pool.ntp.org
sudo systemctl start ntpd.service
ntpstat

⑤启动 TiDB 集群
ansible-playbook start.yml

13测试集群

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

使用 MySQL 客户端连接测试,TCP 4000 端口是 TiDB 服务默认端口。

mysql -u root -h 192.168.3.84 -P 4000

通过浏览器访问监控平台。

地址:http://192.168.3.84:3000 默认帐号密码是:admin/admin

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容