TiDB集群部署最细化教程-亲测有效

TiDB-TiUP集群部署

本次部署使用环境:

CentOS Linux release 7.9.2009 (Core)
TiDB 6.5.1
三台服务器:
(内)172.19.172.179
(内)172.19.172.180
(内)172.19.172.182

一、部署前准备

1.1 查询官方文档部署环境及配置要求-此处不赘述
1.2 检测及关闭系统 swap
echo "vm.swappiness = 0">> /etc/sysctl.conf
swapoff -a && swapon -a
sysctl -p
1.3 检测及关闭目标部署机器的防火墙
  1. 检查防火墙状态

    sudo firewall-cmd --state
    sudo systemctl status firewalld.service
    
  2. 关闭防火墙服务

    sudo systemctl stop firewalld.service
    
  3. 关闭防火墙自动启动服务

    sudo systemctl disable firewalld.service
    
  4. 检查防火墙状态

    sudo systemctl status firewalld.service
    
1.4 检测及安装 NTP 服务

执行以下命令,如果输出 running 表示 NTP 服务正在运行:

sudo systemctl status ntpd.service

输出结果:
ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2017-12-18 13:13:19 CST; 3s ago
  • 若返回报错信息 Unit ntpd.service could not be found.,尝试执行以下命令,以查看与 NTP 进行时钟同步所使用的系统配置是 chronyd 还是 ntpd

    sudo systemctl status chronyd.service
    
    输出结果:显示使用的chronyd
    chronyd.service - NTP client/server
       Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
       Active: active (running) since Fri 2023-03-24 09:13:26 CST; 1 weeks 4 days ago
         Docs: man:chronyd(8)
               man:chrony.conf(5)
     Main PID: 565 (chronyd)
       CGroup: /system.slice/chronyd.service
               └─565 /usr/sbin/chronyd
    

    若发现系统既没有配置 chronyd 也没有配置 ntpd,则表示系统尚未安装任一服务。此时,应先安装其中一个服务,并保证它可以自动启动,默认使用 ntpd;自行百度安装chronydntpd,此处不赘述

1.5 手动配置 SSH 互信及 sudo 免密码
  1. root 用户依次登录到部署目标机器创建 tidb 用户并设置登录密码.

    useradd tidb && \
    passwd tidb
    
    如已有tidb用户,直接执行
    passwd tidb 修改密码
    
  2. 执行以下命令,将 tidb ALL=(ALL) NOPASSWD: ALL 添加到文件末尾,即配置好 sudo 免密码.

    打开文件,进行编辑
    visudo
    默认添加,并保存
    tidb ALL=(ALL) NOPASSWD: ALL
    
  3. tidb 用户登录到中控机,执行以下命令。将 10.0.1.1 替换成你的部署目标机器 IP,按提示输入部署目标机器 tidb 用户密码,执行成功后即创建好 SSH 互信,其他机器同理。新建的 tidb 用户下没有 .ssh 目录,需要执行生成 rsa 密钥的命令来生成 .ssh 目录。如果要在中控机上部署 TiDB 组件,需要为中控机和中控机自身配置互信。

    创建秘钥文件,可直接一路回车
    ssh-keygen -t rsa
    将秘钥文件copy至目标主机
    ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.1.1
    
    • 如果出现ERROR: failed to open ID file '/home/ismail/.pub': No such file 报错,检查文件名称、当前执行用户、目标主机地址
    • 如果中控机也是集群主机的其中一个,也需要按照以上命令进行配置,让其自身能正常通信
  4. tidb 用户登录中控机,通过 ssh 的方式登录目标机器 IP。如果不需要输入密码并登录成功,即表示 SSH 互信配置成功。

    免密访问目标主机
    ssh 10.0.1.1
    更换用户后表明成功
    [tidb@10.0.1.1 ~]$
    
  5. tidb 用户登录到部署目标机器后,执行以下命令,不需要输入密码并切换到 root 用户,表示 tidb 用户 sudo 免密码配置成功。

    切换用户
    sudo -su root
    切换成功
    [root@10.0.1.1 tidb]#
    

二、部署过程

在中控机上部署 TiUP 组件有两种方式:在线部署和离线部署。本文使用的是在线安装方式,离线安装查看官方文档.

1.在线部署

以普通用户身份或root用户登录中控机。以 tidb 用户为例,后续安装 TiUP 及集群管理操作均通过该用户完成:

  1. 执行如下命令安装 TiUP 工具:

    curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
    
  2. 按如下步骤设置 TiUP 环境变量:

    • 重新声明全局环境变量:

      source .bash_profile
      
    • 确认 TiUP 工具是否安装:

      which tiup
      
      输出结果
      /root/.tiup/bin/tiup
      
  3. 安装 TiUP cluster 组件:

    tiup cluster
    
  4. 如果已经安装,则更新 TiUP cluster 组件至最新版本:

    tiup update --self && tiup update cluster
    

    预期输出 “Update successfully!” 字样。

  5. 验证当前 TiUP cluster 版本信息。执行如下命令查看 TiUP cluster 组件版本:

    tiup --binary cluster
    

    输出结果"/root/.tiup/components/cluster/v1.12.0/tiup-cluster"字样

2. 初始化集群拓扑文件

执行如下命令,生成集群初始化配置文件:该文件是默认的配置文件,可根据需要自定配置内容

tiup cluster template > topology.yaml

执行 vi topology.yaml,查看配置文件的内容:

global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/tidb-deploy"
  data_dir: "/tidb-data"
server_configs: {}
pd_servers:
  - host: 10.0.1.4
  - host: 10.0.1.5
  - host: 10.0.1.6
tidb_servers:
  - host: 10.0.1.7
  - host: 10.0.1.8
  - host: 10.0.1.9
tikv_servers:
  - host: 10.0.1.1
  - host: 10.0.1.2
  - host: 10.0.1.3
monitoring_servers:
  - host: 10.0.1.4
grafana_servers:
  - host: 10.0.1.4
alertmanager_servers:
  - host: 10.0.1.4

我的配置文件内容:

global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/tidb-deploy"
  data_dir: "/tidb-data"
  
# # Monitored variables are applied to all the machines.
monitored:
  node_exporter_port: 9100
  blackbox_exporter_port: 9115

# # Server configs are used to specify the runtime configuration of TiDB components.
server_configs:
  tidb:
    log.level: debug
    log.slow-query-file: tidb-slow.log
  tikv:
    readpool.storage.use-unified-pool: false
    readpool.coprocessor.use-unified-pool: true
  pd:
    schedule.leader-schedule-limit: 4
    schedule.region-schedule-limit: 2048
    schedule.replica-schedule-limit: 64
  # tiflash:
  # tiflash-learner:

# # Server configs are used to specify the configuration of PD Servers.
pd_servers:
  # # The ip address of the PD Server.
  - host: 172.19.172.179
    name: pd-207
  - host: 172.19.172.180
    name: pd-90
  - host: 172.19.172.182
    name: pd-126
    
# # Server configs are used to specify the configuration of TiDB Servers.
tidb_servers:
  # # The ip address of the TiDB Server.
  - host: 172.19.172.179
  - host: 172.19.172.180
  - host: 172.19.172.182

# # Server configs are used to specify the configuration of TiKV Servers.
tikv_servers:
  # # The ip address of the TiKV Server.
  - host: 172.19.172.179
  - host: 172.19.172.180
  - host: 172.19.172.182

# # Server configs are used to specify the configuration of Prometheus Server.
monitoring_servers:
  # # The ip address of the Monitoring Server.
  - host: 172.19.172.179

# # Server configs are used to specify the configuration of Grafana Servers.  
grafana_servers:
  # # The ip address of the Grafana Server.
  - host: 172.19.172.179
3. 执行部署命令

执行部署命令前,先使用 checkcheck --apply 命令检查和自动修复集群存在的潜在风险:

  1. 检查集群存在的潜在风险:

    tiup cluster check ./topology.yaml --user root [-p] [-i /home/root/.ssh/gcp_rsa]
    
  2. 自动修复集群存在的潜在风险:

    tiup cluster check ./topology.yaml --apply --user root [-p] [-i /home/root/.ssh/gcp_rsa]
    
  3. 部署 TiDB 集群:tidb-test为集群名称,可自定

    tiup cluster deploy tidb-test v6.5.1 ./topology.yaml --user root [-p] [-i /home/root/.ssh/gcp_rsa]
    

以上部署示例中:

  • tidb-test 为部署的集群名称。
  • v6.5.1 为部署的集群版本,可以通过执行 tiup list tidb 来查看 TiUP 支持的最新可用版本。
  • 初始化配置文件为 topology.yaml
  • --user root 表示通过 root 用户登录到目标主机完成集群部署,该用户需要有 ssh 到目标机器的权限,并且在目标机器有 sudo 权限。也可以用其他有 ssh 和 sudo 权限的用户完成部署。
  • [-i] 及 [-p] 为可选项,如果已经配置免密登录目标机,则不需填写。否则选择其一即可,[-i] 为可登录到目标机的 root 用户(或 --user 指定的其他用户)的私钥,也可使用 [-p] 交互式输入该用户的密码。

预期日志结尾输出 Deployed cluster tidb-test successfully 关键词,表示部署成功。

5. 查看 TiUP 管理的集群情况
tiup cluster list

TiUP 支持管理多个 TiDB 集群,该命令会输出当前通过 TiUP cluster 管理的所有集群信息,包括集群名称、部署用户、版本、密钥信息等。

输出结果内容:

tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.0/tiup-cluster list
Name       User  Version  Path                                            PrivateKey
----       ----  -------  ----                                            ----------
tidb-test  tidb  v6.5.1   /root/.tiup/storage/cluster/clusters/tidb-test /root/.tiup/storage/cluster/clusters/tidb-test/ssh/id_rsa
6. 检查部署的 TiDB 集群情况

例如,执行如下命令检查 tidb-test 集群情况:tidb-test为集群名称

tiup cluster display tidb-test

预期输出包括 tidb-test 集群中实例 ID、角色、主机、监听端口和状态(由于还未启动,所以状态为 Down/inactive)、目录信息。

未启动时的日志输出

Cluster type:       tidb
Cluster name:       tidb-test
Cluster version:    v6.5.1
Deploy user:        tidb
SSH type:           builtin
Grafana URL:        http://172.19.172.179:3000
ID                    Role        Host            Ports        OS/Arch       Status  Data Dir                    Deploy Dir
--                    ----        ----            -----        -------       ------  --------                    ----------
172.19.172.179:3000   grafana     172.19.172.179  3000         linux/x86_64  Down    -                           /tidb-deploy/grafana-3000
172.19.172.179:2379   pd          172.19.172.179  2379/2380    linux/x86_64  Down    /tidb-data/pd-2379          /tidb-deploy/pd-2379
172.19.172.180:2379   pd          172.19.172.180  2379/2380    linux/x86_64  Down    /tidb-data/pd-2379          /tidb-deploy/pd-2379
172.19.172.182:2379   pd          172.19.172.182  2379/2380    linux/x86_64  Down    /tidb-data/pd-2379          /tidb-deploy/pd-2379
172.19.172.179:9090   prometheus  172.19.172.179  9090/12020   linux/x86_64  Down    /tidb-data/prometheus-9090  /tidb-deploy/prometheus-9090
172.19.172.179:4000   tidb        172.19.172.179  4000/10080   linux/x86_64  Down    -                           /tidb-deploy/tidb-4000
172.19.172.180:4000   tidb        172.19.172.180  4000/10080   linux/x86_64  Down    -                           /tidb-deploy/tidb-4000
172.19.172.182:4000   tidb        172.19.172.182  4000/10080   linux/x86_64  Down    -                           /tidb-deploy/tidb-4000
172.19.172.179:20160  tikv        172.19.172.179  20160/20180  linux/x86_64  N/A     /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
172.19.172.180:20160  tikv        172.19.172.180  20160/20180  linux/x86_64  N/A     /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
172.19.172.182:20160  tikv        172.19.172.182  20160/20180  linux/x86_64  N/A     /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
7. 启动集群

安全启动是 TiUP cluster 从 v1.9.0 起引入的一种新的启动方式,采用该方式启动数据库可以提高数据库安全性。推荐使用安全启动。

安全启动后,TiUP 会自动生成 TiDB root 用户的密码,并在命令行界面返回密码。

注意

  • 使用安全启动方式后,不能通过无密码的 root 用户登录数据库,你需要记录命令行返回的密码进行后续操作。
  • 该自动生成的密码只会返回一次,如果没有记录或者忘记该密码,请参照忘记 root 密码修改密码。

方式一:安全启动

tiup cluster start tidb-test --init

预期结果如下,表示启动成功。

Started cluster `tidb-test` successfully
The root password of TiDB database has been changed.
The new password is: '*********'.
Copy and record it to somewhere safe, it is only displayed once, and will not be stored.
The generated password can NOT be get and shown again.
[root@node207 ~]# tiup cluster display tidb-test
tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.0/tiup-cluster display tidb-test
Cluster type:       tidb
Cluster name:       tidb-test
Cluster version:    v6.5.1
Deploy user:        tidb
SSH type:           builtin
Dashboard URL:      http://172.19.172.179:2379/dashboard
Grafana URL:        http://172.19.172.179:3000
ID                    Role        Host            Ports        OS/Arch       Status   Data Dir                    Deploy Dir
--                    ----        ----            -----        -------       ------   --------                    ----------
172.19.172.179:3000   grafana     172.19.172.179  3000         linux/x86_64  Up       -                           /tidb-deploy/grafana-3000
172.19.172.179:2379   pd          172.19.172.179  2379/2380    linux/x86_64  Up|L|UI  /tidb-data/pd-2379          /tidb-deploy/pd-2379
172.19.172.180:2379   pd          172.19.172.180  2379/2380    linux/x86_64  Up       /tidb-data/pd-2379          /tidb-deploy/pd-2379
172.19.172.182:2379   pd          172.19.172.182  2379/2380    linux/x86_64  Up       /tidb-data/pd-2379          /tidb-deploy/pd-2379
172.19.172.179:9090   prometheus  172.19.172.179  9090/12020   linux/x86_64  Up       /tidb-data/prometheus-9090  /tidb-deploy/prometheus-9090
172.19.172.179:4000   tidb        172.19.172.179  4000/10080   linux/x86_64  Up       -                           /tidb-deploy/tidb-4000
172.19.172.180:4000   tidb        172.19.172.180  4000/10080   linux/x86_64  Up       -                           /tidb-deploy/tidb-4000
172.19.172.182:4000   tidb        172.19.172.182  4000/10080   linux/x86_64  Up       -                           /tidb-deploy/tidb-4000
172.19.172.179:20160  tikv        172.19.172.179  20160/20180  linux/x86_64  Up       /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
172.19.172.180:20160  tikv        172.19.172.180  20160/20180  linux/x86_64  Up       /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
172.19.172.182:20160  tikv        172.19.172.182  20160/20180  linux/x86_64  Up       /tidb-data/tikv-20160       /tidb-deploy/tikv-20160

方式二:普通启动

tiup cluster start tidb-test

预期结果输出 Started cluster tidb-test successfully,表示启动成功。使用普通启动方式后,可通过无密码的 root 用户登录数据库。

8. 验证集群运行状态
tiup cluster display tidb-test

预期结果输出:各节点 Status 状态信息为 Up 说明集群状态正常。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值