【TiDB原理与实战详解】2、部署与节点的扩/缩容~学不会? 不存在的!

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云原生内存数据库 Tair,内存型 2GB
简介: TiUP 是 TiDB 4.0 引入的集群运维工具,TiUP cluster 用于部署、管理 TiDB 集群,支持 TiDB、TiFlash、TiDB Binlog 等组件。本文介绍使用 TiUP 部署生产环境的具体步骤,包括节点规划、工具安装、配置文件修改及集群部署等。同时,提供了常用命令和安全优化方法,并详细说明了如何进行集群的扩缩容操作,以及时区设置等维护工作。

三、TiUP 部署生产环境

TiUP 是 TiDB 4.0 版本引入的集群运维工具,TiUP cluster 是 TiUP 提供的使用 Golang 编写的集群管理组件,通过 TiUP cluster 组件就可以进行日常的运维工作,包括部署、启动、关闭、销毁、弹性扩缩容、升级 TiDB 集群,以及管理 TiDB 集群参数。

目前 TiUP 可以支持部署 TiDB、TiFlash、TiDB Binlog、TiCDC,以及监控系统。本文将介绍不同集群拓扑的具体部署步骤。

1、节点规划:

2、TiUP工具下载安装

wget https://tiup-mirrors.pingcap.com/install.sh 
sh install.sh 
source .bash_profile

# 安装cluster组件
tiup cluster 
tiup update --self && tiup update cluster

3、TiUP 配置文件修改

vim  test.yaml
# # Global variables are applied to all deployments and used as the default value of 
# # the deployments if a specific deployment value is missing.
global:
  user: "tidb"
  ssh_port: 22
  deploy_dir: "/data/tidb-deploy"
  data_dir: "/data/tidb-data"

# # Monitored variables are applied to all the machines.
monitored:
  node_exporter_port: 9100
  blackbox_exporter_port: 9115

server_configs:
  tidb:
    log.slow-threshold: 300
    binlog.enable: true
    binlog.ignore-error: true

  tikv:
    readpool.storage.use-unified-pool: false
    readpool.coprocessor.use-unified-pool: true

  pd:
    replication.enable-placement-rules: true
    replication.location-labels: ["host"]

  tiflash:
    logger.level: "info"

pd_servers:
  - host: 10.10.8.107

tidb_servers:
  - host: 10.10.8.107

tikv_servers:
  - host: 10.10.8.107
    port: 20160
    status_port: 20180
    config:
      server.labels: {
   
    host: "logic-host-1" }

  - host: 10.10.8.107
    port: 20161 
    status_port: 20181 
    config: 
      server.labels: {
   
    host: "logic-host-2" }

  - host: 10.10.8.107
    port: 20162
    status_port: 20182
    config:
      server.labels: {
   
    host: "logic-host-3" }

tiflash_servers:
  - host: 10.10.8.107

monitoring_servers:
  - host: 10.10.8.107

grafana_servers:
  - host: 10.10.8.107

alertmanager_servers:
  - host: 10.10.8.107

注解:

user: "tidb":表示通过 tidb 系统用户(部署会自动创 建)来做集群的内部管理,默认使用 22 端口通过 ssh 登录目标机器 replication.enable-placement-rules:设置这个 PD 参数来确保 TiFlash 正常运行
host:设置为本部署主机的 IP

4、部署TiDB 集群

注:单机测试调整ssh连接数限制

vim /etc/ssh/sshd_config
MaxSessions 20
systemctl restart sshd

1、执行集群部署

# 创建数据存储目录
mkdir  /data

# 创建集群
tiup cluster deploy test-cluster v5.2.1 ./test.yaml --user root -p  # root密码

2、常用命令

# 启动集群
tiup cluster start test-cluster

# 查看集群信息
tiup cluster list

# 查看集群详细状态
tiup cluster display test-cluster

# 访问 TiDB 数据库,密码为空
mysql -h 10.10.8.107 -P 4000 -u root -p # 直接回车

# 访问 grafana
http://grafana_servers_ip/域名:3000
默认账号密码:admin/admin

# 访问 Dashboard
http://pd_servers_ip/域名:2379/dashboard

# 访问 prometheus
http://10.10.8.107:9090/graph

# 重载单节点配置并在必要时重启
tiup cluster reload test-cluster  -N 10.10.8.107:2379

# 重启10.10.8.107:2379 节点
tiup cluster restart test-cluster  -N 10.10.8.107:2379

# 编辑总配置文件
tiup cluster edit-config test-cluster

# 查看配置模版
tiup cluster template topology

# 集群重命名
tiup cluster rename test-cluster test

3、安全优化

# 修改默认用户root的作用域
update mysql.user set host='127.0.0.1' where user='root';
# 修改密码
alter user root@'127.0.0.1' identified by 'tkamc.00';
# 创建dashboard界面访问用户密码
create user root@'本机IP' identified by 'tkamc.00'; # 不要使用%和127.0.0.1
grant all on *.* to  root@'本机IP'; 

# 创建dba用户
create user dba@'10.10.8.%' identified by 'tkamc.00';
# 授权用户
grant select on *.* to dba@'10.10.8.%' ;
# 刷新授权表
flush privileges;

四、扩容缩容

image.png

  • 节点扩容应避开业务繁忙时间段,防止意外事件的发生。

  • 不会编写配置文件直接复制生产的拿过来改改端口路径就可以了

1、扩容TiKV节点

# 编写配置文件 //放哪都行
vim tikv_20163.yaml
tikv_servers:
- host: 10.10.8.107
  ssh_port: 22
  port: 20163
  status_port: 20183
  deploy_dir: /data/tidb-deploy/tikv-20163
  data_dir: /data/tidb-data/tikv-20163
  log_dir: /data/tidb-deploy/tikv-20163/log
  config:
    server.labels:
      host: logic-host-4
  arch: amd64
  os: linux

# 执行扩容命令
tiup cluster scale-out test-cluster tikv_20163.yaml -uroot -p

# 查看集群状态
tiup  cluster  display test-cluster

2、扩容pd节点

# 编写配置文件 //放哪都行
vim pd_3379.yaml
pd_servers:
- host: 10.10.8.107
  ssh_port: 22
  name: pd-10.10.8.107-3379
  client_port: 3379
  peer_port: 3380
  deploy_dir: /data/tidb-deploy/pd-3379
  data_dir: /data/tidb-data/pd-3379
  log_dir: /data/tidb-deploy/pd-3379/log
  arch: amd64
  os: linux

# 执行扩容
tiup cluster scale-out test-cluster pd_2380.yaml -uroot -p

# 查看集群状态
tiup  cluster  display test-cluster

3、扩容tidb节点

# 编写配置文件 //放哪都行
vim tidb_4001.yaml
tidb_servers:
- host: 10.10.8.107
  ssh_port: 22
  port: 4001
  status_port: 10081
  deploy_dir: /data/tidb-deploy/tidb-4001
  log_dir: /data/tidb-deploy/tidb-4001/log
  arch: amd64
  os: linux

# 执行扩容
tiup cluster scale-out test-cluster tidb_4001.yaml -uroot -p

# 查看集群状态
tiup  cluster  display test-cluster

4、扩容tiflash节点

# 检查版本是否支持tiflash //V5.0以上版本支持 tiflash
# 老版本需要先开启 PD 的支持功能详解见官网 enable-placement-rules true 
tiup ctl:v5.2.1  pd -u http://10.10.8.107:2379  config set enable-placement-rules true  # 修改为自己的版本和pd节点信息,任意节点长执行即可

# 编写配置文件 //放哪都行
vim tiflash_9001.yaml
tiflash_servers:
- host: 10.10.8.107
  ssh_port: 22
  tcp_port: 9001
  http_port: 8124
  flash_service_port: 3931
  flash_proxy_port: 20171
  flash_proxy_status_port: 20293
  metrics_port: 8235
  deploy_dir: /data/tidb-deploy/tiflash-9001
  data_dir: /data/tidb-data/tiflash-9001
  log_dir: /data/tidb-deploy/tiflash-9001/log
  arch: amd64
  os: linux

# 执行扩容
tiup cluster scale-out test-cluster tiflash_9001.yaml -uroot -p

# 查看集群状态
tiup  cluster  display test-cluster

5、缩容tiflash节点

# tiflash注意!!!不同

# 在缩容tiflash节点前,必须保证使用tiflash的表的副本数不能大于删除N个节点后的副本数,否则需要修改表的副本数。 //例如有3个tiflash节点,要缩容两个节点,就需要将表的副本集修改为0。 //如果要缩容1个节点,就需要将表的副本集修改为1;因为副本集默认从 0 开始计数 0 1 2 3 表示四个副本数

# 查看使用tiflush的表的信息
MySQL [test]> select * from INFORMATION_SCHEMA.tiflash_replica; // 这里1表示有两个节点 0 1 
+--------------+------------+----------+---------------+-----------------+-----------+----------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_ID | REPLICA_COUNT | LOCATION_LABELS | AVAILABLE | PROGRESS |
+--------------+------------+----------+---------------+-----------------+-----------+----------+
| test         | t1         |       53 |             1 |                 |         1 |        1 |
+--------------+------------+----------+---------------+-----------------+-----------+----------+
1 row in set (0.00 sec)
# 根据tiflash缩容数,调整使用tiflash的表的副本数 //这里演示为有两个tiflash节点,缩容为一个节点
alter table test.t1 set tiflash replica 0;  //如果有多个表需要全部修改为可用的副本数

# 修改完成后查询确认是否修改成功
MySQL [test]>  select * from INFORMATION_SCHEMA.tiflash_replica;
Empty set (0.01 sec)

# 查看要缩容节点的IP端口
tiup  cluster  display test-cluster 

# 执行缩容命令 //谨慎操作
tiup cluster scale-in test-cluster -N 10.10.8.107:9000

# 缩容后整理集群 //谨慎操作
tiup cluster prune test-cluster

# 查看集群状态信息
tiup  cluster  display test-cluster

6、缩容其他节点(通用操作)

# 查看要缩容节点的IP端口
tiup  cluster  display test-cluster 

# 执行缩容命令 //谨慎操作
tiup cluster scale-in test-cluster -N 10.10.8.107:20160

# 缩容后整理集群 //谨慎操作
tiup cluster prune test-cluster

# 查看集群状态信息
tiup  cluster  display test-cluster

7、数据清理

# 删除日志 //谨慎执行
tiup cluster clean test  --ignore-node 10.10.8.107:4001  --ignore-node 10.10.8.107:9001  --log   // --ignore-node 表示需要保留的节点信息,不加默认删除所有

# 删除数据 //禁止执行
tiup cluster clean test  --ignore-node 10.10.8.107:4001  --ignore-node 10.10.8.107:9001  --data   // --ignore-node 表示需要保留的节点信息,不加默认删除所有

# 删除日志和数据 //禁止执行
tiup cluster clean test  --ignore-node 10.10.8.107:4001  --ignore-node 10.10.8.107:9001  --all   // --ignore-node 表示需要保留的节点信息,不加默认删除所有

# 删除整个集群 //集群全部消失,机票已订好!
tiup cluster destroy test

五、时区设置

# 时区设置为 UTC 或者 +08:00' 为国内标准时间
 system_time_zone  Asia/Shanghai  # 时区
 time_zone         +08:00         # 时间

# 以下两种任选
set global time_zone='UTC'
set global time_zone='+08:00'
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
11月前
|
运维 NoSQL 安全
【最佳实践】高可用mongodb集群(1分片+3副本):规划及部署
结合我们的生产需求,本次详细整理了最新版本 MonogoDB 7.0 集群的规划及部署过程,具有较大的参考价值,基本可照搬使用。 适应数据规模为T级的场景,由于设计了分片支撑,后续如有大数据量需求,可分片横向扩展。
1010 1
|
JSON 数据格式 索引
记一次ES节点扩容、数据迁移实践
ElasticSearch、ES数据迁移、ES扩容
13538 0
|
21天前
|
存储 Kubernetes 监控
在K8S中,集群可以做哪些优化?
在K8S中,集群可以做哪些优化?
|
26天前
|
Prometheus Kubernetes 监控
在K8S中,当Pod业务量比较大时候,如何实现水平伸缩和扩容?
在K8S中,当Pod业务量比较大时候,如何实现水平伸缩和扩容?
|
4月前
|
消息中间件 分布式计算 Hadoop
kafaka单节点部署多节点部署
kafaka单节点部署多节点部署
190 2
|
10月前
|
NoSQL Redis
轻松掌握组件启动之Redis集群扩展秘籍:轻松扩容与缩容,释放高性能潜能
在这篇文章中,我们将揭示Redis集群的扩容和缩容操作,让您的Redis集群发挥最佳性能和可伸缩性。通过增加主节点和从节点,并将它们无缝添加到集群中,您将能够轻松扩展您的Redis集群以满足不断增长的需求。同时,我们还将探讨如何进行缩容操作,即删除节点,以优化集群资源的利用。无论您是初学者还是经验丰富的Redis用户,本文将为您提供一系列有用的技巧和最佳实践,帮助您更好地管理和优化Redis集群,实现更高的性能和可扩展性。
214 0
|
存储 NoSQL 算法
接近3w详解Docker搭建Redis集群(主从容错、主从扩容、主从缩容)
接近3w详解Docker搭建Redis集群(主从容错、主从扩容、主从缩容)
123 0
|
存储 Java 数据库
clickhouse集群,双实例多副本集群部署
clickhouse集群,双实例多副本集群部署
|
12月前
|
运维 负载均衡 OceanBase
第四章:OceanBase集群技术架构(动态扩容和缩容)
第四章:OceanBase集群技术架构(动态扩容和缩容)
482 0
|
NoSQL Redis 数据中心
Redis 集群偶数节点跨地域部署之高可用测试
你搭建过偶数节点的 Redis 集群吗?有没有想过它是否具备高可用的能力?会不会脑裂呢?实践出真知!现在 docker 太方便了,搭一个集群模拟一下……
153 4