使用 TiUP 运维 DM 集群

本文介绍如何使用 TiUP 的 DM 组件运维 DM 集群。

如果你还未部署 DM 集群,可参考使用 TiUP 部署 DM 集群

注意:

  • 需要确保以下组件间端口可正常连通:

    • 各 DM-master 节点间的 peer_port(默认为 8291)可互相连通。

    • 各 DM-master 节点可连通所有 DM-worker 节点的 port(默认为 8262)。

    • 各 DM-worker 节点可连通所有 DM-master 节点的 port(默认为 8261)。

    • TiUP 节点可连通所有 DM-master 节点的 port(默认为 8261)。

    • TiUP 节点可连通所有 DM-worker 节点的 port(默认为 8262)。

TiUP DM 组件的帮助信息如下:

  1. tiup dm --help
  1. Deploy a DM cluster for production
  2. Usage:
  3. tiup dm [flags]
  4. tiup dm [command]
  5. Available Commands:
  6. deploy Deploy a DM cluster for production
  7. start Start a DM cluster
  8. stop Stop a DM cluster
  9. restart Restart a DM cluster
  10. list List all clusters
  11. destroy Destroy a specified DM cluster
  12. audit Show audit log of cluster operation
  13. exec Run shell command on host in the dm cluster
  14. edit-config Edit DM cluster config
  15. display Display information of a DM cluster
  16. reload Reload a DM cluster's config and restart if needed
  17. upgrade Upgrade a specified DM cluster
  18. patch Replace the remote package with a specified package and restart the service
  19. scale-out Scale out a DM cluster
  20. scale-in Scale in a DM cluster
  21. import Import an exist DM 1.0 cluster from dm-ansible and re-deploy 2.0 version
  22. help Help about any command
  23. Flags:
  24. -h, --help help for tiup-dm
  25. --native-ssh Use the native SSH client installed on local system instead of the build-in one.
  26. --ssh-timeout int Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection. (default 5)
  27. -v, --version version for tiup-dm
  28. --wait-timeout int Timeout in seconds to wait for an operation to complete, ignored for operations that don't fit. (default 60)
  29. -y, --yes Skip all confirmations and assumes 'yes'

查看集群列表

集群部署成功后,可以通过 tiup dm list 命令在集群列表中查看该集群:

  1. tiup dm list
  1. Name User Version Path PrivateKey
  2. ---- ---- ------- ---- ----------
  3. prod-cluster tidb ${version} /root/.tiup/storage/dm/clusters/test /root/.tiup/storage/dm/clusters/test/ssh/id_rsa

启动集群

集群部署成功后,可以执行以下命令启动该集群。如果忘记了部署的集群名字,可以使用 tiup dm list 命令查看。

  1. tiup dm start prod-cluster

检查集群状态

如果想查看集群中每个组件的运行状态,逐一登录到各个机器上查看显然很低效。因此,TiUP 提供了 tiup dm display 命令,用法如下:

  1. tiup dm display prod-cluster
  1. dm Cluster: prod-cluster
  2. dm Version: ${version}
  3. ID Role Host Ports OS/Arch Status Data Dir Deploy Dir
  4. -- ---- ---- ----- ------- ------ -------- ----------
  5. 172.19.0.101:9093 alertmanager 172.19.0.101 9093/9094 linux/x86_64 Up /home/tidb/data/alertmanager-9093 /home/tidb/deploy/alertmanager-9093
  6. 172.19.0.101:8261 dm-master 172.19.0.101 8261/8291 linux/x86_64 Healthy|L /home/tidb/data/dm-master-8261 /home/tidb/deploy/dm-master-8261
  7. 172.19.0.102:8261 dm-master 172.19.0.102 8261/8291 linux/x86_64 Healthy /home/tidb/data/dm-master-8261 /home/tidb/deploy/dm-master-8261
  8. 172.19.0.103:8261 dm-master 172.19.0.103 8261/8291 linux/x86_64 Healthy /home/tidb/data/dm-master-8261 /home/tidb/deploy/dm-master-8261
  9. 172.19.0.101:8262 dm-worker 172.19.0.101 8262 linux/x86_64 Free /home/tidb/data/dm-worker-8262 /home/tidb/deploy/dm-worker-8262
  10. 172.19.0.102:8262 dm-worker 172.19.0.102 8262 linux/x86_64 Free /home/tidb/data/dm-worker-8262 /home/tidb/deploy/dm-worker-8262
  11. 172.19.0.103:8262 dm-worker 172.19.0.103 8262 linux/x86_64 Free /home/tidb/data/dm-worker-8262 /home/tidb/deploy/dm-worker-8262
  12. 172.19.0.101:3000 grafana 172.19.0.101 3000 linux/x86_64 Up - /home/tidb/deploy/grafana-3000
  13. 172.19.0.101:9090 prometheus 172.19.0.101 9090 linux/x86_64 Up /home/tidb/data/prometheus-9090 /home/tidb/deploy/prometheus-9090

Status 列用 Up 或者 Down 表示该服务是否正常。对于 DM-master 组件,同时可能会带有 |L 表示该 DM-master 是 Leader, 对于 DM-worker 组件,Free 表示当前 DM-worker 没有与上游绑定。

缩容节点

缩容即下线服务,最终会将指定的节点从集群中移除,并删除遗留的相关数据文件。

缩容操作进行时,内部对 DM-master、DM-worker 组件的操作流程为:

  1. 停止组件进程
  2. 调用 DM-master 删除 member 的 API
  3. 清除节点的相关数据文件

缩容命令的基本用法:

  1. tiup dm scale-in <cluster-name> -N <node-id>

它需要指定至少两个参数,一个是集群名字,另一个是节点 ID。节点 ID 可以参考上一节使用 tiup dm display 命令获取。

比如想缩容 172.16.5.140 上的 DM-worker 节点(DM-master 的缩容类似),可以执行:

  1. tiup dm scale-in prod-cluster -N 172.16.5.140:8262

扩容节点

扩容的内部逻辑与部署类似,TiUP DM 组件会先保证节点的 SSH 连接,在目标节点上创建必要的目录,然后执行部署并且启动服务。

例如,在集群 prod-cluster 中扩容一个 DM-worker 节点(DM-master 的扩容类似):

  1. 新建 scale.yaml 文件,添加新增的 woker 节点信息:

    注意:

    需要新建一个拓扑文件,文件中只写入扩容节点的描述信息,不要包含已存在的节点。

    其他更多配置项(如:部署目录等)请参考 TiUP 配置参数模版

    1. ---
    2. worker_servers:
    3. - host: 172.16.5.140
  2. 执行扩容操作。TiUP DM 根据 scale.yaml 文件中声明的端口、目录等信息在集群中添加相应的节点:

    1. tiup dm scale-out prod-cluster scale.yaml

    执行完成之后可以通过 tiup dm display prod-cluster 命令检查扩容后的集群状态。

滚动升级

注意:

从 v2.0.5 版本开始,dmctl 支持导出和导入集群的数据源和任务配置

升级前,可使用 config export 命令导出集群的配置文件,升级后如需降级回退到旧版本,可重建旧集群后,使用 config import 导入之前的配置。

对于 v2.0.5 之前版本的集群,可使用 v2.0.5 及之后版本的 dmctl 导出和导入集群配置。

对于 v2.0.2 之后的版本,导入集群配置时暂不支持自动恢复 relay worker 相关配置,可手动执行 start-relay 命令开启 relay log

滚动升级过程中尽量保证对前端业务透明、无感知,其中对不同节点有不同的操作。

升级操作

可使用 tiup dm upgrade 命令来升级集群。例如,以下示例将集群升级到 ${version},执行命令前,将 ${version} 替换为实际需要的版本:

  1. tiup dm upgrade prod-cluster `${version}`

更新配置

如果想要动态更新组件的配置,TiUP DM 组件为每个集群保存了一份当前的配置,如果想要编辑这份配置,则执行 tiup dm edit-config <cluster-name> 命令。例如:

  1. tiup dm edit-config prod-cluster

然后 TiUP DM 组件会使用 vi 打开配置文件供编辑(如果你想要使用其他编辑器,请使用 EDITOR 环境变量自定义编辑器,例如 export EDITOR=nano),编辑完之后保存即可。此时的配置并没有应用到集群,如果想要让它生效,还需要执行:

  1. tiup dm reload prod-cluster

该操作会将配置发送到目标机器,滚动重启集群,使配置生效。

更新组件

常规的升级集群可以使用 upgrade 命令,但是在某些场景下(例如 Debug),可能需要用一个临时的包替换正在运行的组件,此时可以用 patch 命令:

  1. tiup dm patch --help
  1. Replace the remote package with a specified package and restart the service
  2. Usage:
  3. tiup dm patch <cluster-name> <package-path> [flags]
  4. Flags:
  5. -h, --help help for patch
  6. -N, --node strings Specify the nodes
  7. --overwrite Use this package in the future scale-out operations
  8. -R, --role strings Specify the role
  9. --transfer-timeout int Timeout in seconds when transferring dm-master leaders (default 300)
  10. Global Flags:
  11. --native-ssh Use the native SSH client installed on local system instead of the build-in one.
  12. --ssh-timeout int Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection. (default 5)
  13. --wait-timeout int Timeout in seconds to wait for an operation to complete, ignored for operations that don't fit. (default 60)
  14. -y, --yes Skip all confirmations and assumes 'yes'

例如,有一个 DM-master 的 hotfix 包放在 /tmp/dm-master-hotfix.tar.gz,如果此时想要替换集群上的所有 DM-master,则可以执行:

  1. tiup dm patch prod-cluster /tmp/dm-master-hotfix.tar.gz -R dm-master

或者只替换其中一个 DM-master:

  1. tiup dm patch prod-cluster /tmp/dm--hotfix.tar.gz -N 172.16.4.5:8261

导入和升级由 DM-Ansible 部署的 DM 1.0 集群

注意:

  • TiUP 不支持在 DM 1.0 集群中导入 DM Portal 组件。
  • 导入 DM 集群前,终止运行待导入的集群。
  • 如果需要将迁移任务升级至 2.0,不可对该任务执行 stop-run 命令。
  • TiUP 仅支持导入数据到 v2.0.0-rc.2 或以上版本的 DM 集群。
  • import 命令用于将数据从 DM 1.0 集群导入到一个新的 DM 2.0 集群。如果你需要将 DM 迁移任务导入一个现有的 DM 2.0 集群,可以参考 TiDB Data Migration 1.0.x 到 2.0+ 手动升级
  • 导入集群某些组件的部署目录与原始集群的部署目录不同,可以执行 display 命令来查看相关信息。
  • 导入前,执行 tiup update --self && tiup update dm,以确保 DM 组件是最新版本。
  • 导入后,集群中只存在一个 DM-master 节点。请参考 扩容节点 来扩展 DM-master。

引入 TiUP 前,DM-Ansible 用于部署 DM 集群。要使 TiUP 接管由 DM-Ansible 部署的 DM 1.0 集群,需要执行 import 命令:

例如,以下命令可导入使用 DM-Ansible 部署的集群:

  1. tiup dm import --dir=/path/to/dm-ansible --cluster-version ${version}

可以执行 tiup list dm-master 来查看 TiUP 支持的最新集群版本。

import 命令的执行过程如下:

  1. 根据之前使用 DM-Ansible 部署的 DM 集群,TiUP 生成一个拓扑文件 topology.yml
  2. 确认拓扑文件生成后,你可以用这个文件来部署 v2.0 或更高版本的 DM 集群。

部署完成后,执行 tiup dm start 命令来启动集群和开始 DM 内核升级流程。

查看操作日志

操作日志的查看可以借助 audit 命令,其用法如下:

  1. Usage:
  2. tiup dm audit [audit-id] [flags]
  3. Flags:
  4. -h, --help help for audit

在不使用 [audit-id] 参数时,该命令会显示执行的命令列表,如下:

  1. tiup dm audit
  1. ID Time Command
  2. -- ---- -------
  3. 4D5kQY 2020-08-13T05:38:19Z tiup dm display test
  4. 4D5kNv 2020-08-13T05:36:13Z tiup dm list
  5. 4D5kNr 2020-08-13T05:36:10Z tiup dm deploy -p prod-cluster ${version} ./examples/dm/minimal.yaml

第一列为 audit-id,如果想看某个命令的执行日志,则传入这个 audit-id:

  1. tiup dm audit 4D5kQY

在集群节点机器上执行命令

exec 命令可以很方便地到集群的机器上执行命令,使用方式如下:

  1. Usage:
  2. tiup dm exec <cluster-name> [flags]
  3. Flags:
  4. --command string the command run on cluster host (default "ls")
  5. -h, --help help for exec
  6. -N, --node strings Only exec on host with specified nodes
  7. -R, --role strings Only exec on host with specified roles
  8. --sudo use root permissions (default false)

例如,如果要到所有的 DM 节点上执行 ls /tmp,则可以执行:

  1. tiup dm exec prod-cluster --command='ls /tmp'

集群控制工具 (dmctl)

TiUP 集成了 DM 的控制工具 dmctl

运行命令如下:

  1. tiup dmctl [args]

指定 dmctl 版本。执行如下命令前,将 ${version} 修改为实际需要的版本。

  1. tiup dmctl: ${version} [args]

例如,以前添加 source 命令为 dmctl --master-addr master1:8261 operate-source create /tmp/source1.yml,集成到 TiUP 中的命令为:

  1. tiup dmctl --master-addr master1:8261 operate-source create /tmp/source1.yml

使用中控机系统自带的 SSH 客户端连接集群

在以上所有操作中,涉及到对集群机器的操作都是通过 TiUP 内置的 SSH 客户端连接集群执行命令,但是在某些场景下,需要使用系统自带的 SSH 客户端来对集群执行操作,比如:

  • 使用 SSH 插件来做认证
  • 使用定制的 SSH 客户端

此时可以通过命令行参数 --native-ssh 启用系统自带命令行:

  • 部署集群: tiup dm deploy <cluster-name> <version> <topo> --native-ssh
  • 启动集群: tiup dm start <cluster-name> --native-ssh
  • 升级集群: tiup dm upgrade ... --native-ssh

所有涉及集群操作的步骤都可以加上 --native-ssh 来使用系统自带的客户端。

也可以使用环境变量 TIUP_NATIVE_SSH 来指定是否使用本地 SSH 客户端,避免每个命令都需要添加 --native-ssh 参数:

  1. export TIUP_NATIVE_SSH=true
  2. # 或者
  3. export TIUP_NATIVE_SSH=1
  4. # 或者
  5. export TIUP_NATIVE_SSH=enable

若环境变量和 --native-ssh 同时指定,则以 --native-ssh 为准。

注意:

在部署集群的步骤中,若需要使用密码的方式连接 (-p),或者密钥文件设置了 passphrase,则需要保证中控机上安装了 sshpass,否则连接时会报错。