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

三、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;

四、扩容缩容

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

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

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

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'
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值