TiDB 集群监控部署
本文档适用于希望手动部署 TiDB 监控报警系统的用户。TiUP 部署方式,会同时自动部署监控报警系统,无需手动部署。
部署 Prometheus 和 Grafana
假设 TiDB 的拓扑结构如下:
| 节点 | 主机 IP | 服务 |
|---|---|---|
| Node1 | 192.168.199.113 | PD1, TiDB, node_export, Prometheus, Grafana |
| Node2 | 192.168.199.114 | PD2, node_export |
| Node3 | 192.168.199.115 | PD3, node_export |
| Node4 | 192.168.199.116 | TiKV1, node_export |
| Node5 | 192.168.199.117 | TiKV2, node_export |
| Node6 | 192.168.199.118 | TiKV3, node_export |
第 1 步:下载二进制包
下载二进制包:
1. wget https://download.pingcap.org/prometheus-2.27.1.linux-amd64.tar.gz
2. wget https://download.pingcap.org/node_exporter-0.17.0.linux-amd64.tar.gz
3. wget https://download.pingcap.org/grafana-7.5.11.linux-amd64.tar.gz
解压二进制包:
1. tar -xzf prometheus-2.27.1.linux-amd64.tar.gz
2. tar -xzf node_exporter-0.17.0.linux-amd64.tar.gz
3. tar -xzf grafana-7.5.11.linux-amd64.tar.gz
第 2 步:在 Node1,Node2,Node3,Node4 上启动 node_exporter
1. cd node_exporter-0.17.0.linux-amd64
启动 node_exporter 服务:
1. ./node_exporter --web.listen-address=":9100" \
2. --log.level="info" &
第 3 步:在 Node1 上启动 Prometheus
编辑 Prometheus 的配置文件:
1. cd prometheus-2.27.1.linux-amd64 &&
2. vi prometheus.yml
1. ...
3. global:
4. scrape_interval: 15s
5. evaluation_interval: 15s
6. # scrape_timeout 设置为全局默认值 (10s)
7. external_labels:
8. cluster: 'test-cluster'
9. monitor: "prometheus"
11. scrape_configs:
12. - job_name: 'overwritten-nodes'
13. honor_labels: true # 不要覆盖 job 和实例的 label
14. static_configs:
15. - targets:
16. - '192.168.199.113:9100'
17. - '192.168.199.114:9100'
18. - '192.168.199.115:9100'
19. - '192.168.199.116:9100'
20. - '192.168.199.117:9100'
21. - '192.168.199.118:9100'
23. - job_name: 'tidb'
24. honor_labels: true # 不要覆盖 job 和实例的 label
25. static_configs:
26. - targets:
27. - '192.168.199.113:10080'
29. - job_name: 'pd'
30. honor_labels: true # 不要覆盖 job 和实例的 label
31. static_configs:
32. - targets:
33. - '192.168.199.113:2379'
34. - '192.168.199.114:2379'
35. - '192.168.199.115:2379'
37. - job_name: 'tikv'
38. honor_labels: true # 不要覆盖 job 和实例的 label
39. static_configs:
40. - targets:
41. - '192.168.199.116:20180'
42. - '192.168.199.117:20180'
43. - '192.168.199.118:20180'
45. ...
启动 Prometheus 服务:
1. ./prometheus \
2. --config.file="./prometheus.yml" \
3. --web.listen-address=":9090" \
4. --web.external-url="http://192.168.199.113:9090/" \
5. --web.enable-admin-api \
6. --log.level="info" \
7. --storage.tsdb.path="./data.metrics" \
8. --storage.tsdb.retention="15d" &
第 4 步:在 Node1 上启动 Grafana
编辑 Grafana 的配置文件:
1. cd grafana-7.5.11 &&
2. vi conf/grafana.ini
1. ...
3. [paths]
4. data = ./data
5. logs = ./data/log
6. plugins = ./data/plugins
7. [server]
8. http_port = 3000
9. domain = 192.168.199.113
10. [database]
11. [session]
12. [analytics]
13. check_for_updates = true
14. [security]
15. admin_user = admin
16. admin_password = admin
17. [snapshots]
18. [users]
19. [auth.anonymous]
20. [auth.basic]
21. [auth.ldap]
22. [smtp]
23. [emails]
24. [log]
25. mode = file
26. [log.console]
27. [log.file]
28. level = info
29. format = text
30. [log.syslog]
31. [event_publisher]
32. [dashboards.json]
33. enabled = false
34. path = ./data/dashboards
35. [metrics]
36. [grafana_net]
37. url = https://grafana.net
39. ...
启动 Grafana 服务:
1. ./bin/grafana-server \
2. --config="./conf/grafana.ini" &
配置 Grafana
本小节介绍如何配置 Grafana。
第 1 步:添加 Prometheus 数据源
-
登录 Grafana 界面。
-
默认地址:
http://localhost:3000 - 默认账户:admin
-
默认密码:admin
注意:
Change Password 步骤可以选择 Skip。 2. 点击 Grafana 侧边栏菜单 Configuration 中的 Data Source。 3. 点击 Add data source。 4. 指定数据源的相关信息:
-
在 Name 处,为数据源指定一个名称。
- 在 Type 处,选择 Prometheus。
- 在 URL 处,指定 Prometheus 的 IP 地址。
- 根据需求指定其它字段。
- 点击 Add 保存新的数据源。
第 2 步:导入 Grafana 面板
执行以下步骤,为 PD Server、TiKV Server 和 TiDB Server 分别导入 Grafana 面板:
- 点击侧边栏的 Grafana 图标。
- 在侧边栏菜单中,依次点击 Dashboards > Import 打开 Import Dashboard 窗口。
- 点击 Upload .json File 上传对应的 JSON 文件(下载 TiDB Grafana 配置文件。
注意:
TiKV、PD 和 TiDB 面板对应的 JSON 文件分别为
tikv_summary.json,tikv_details.json,tikv_trouble_shooting.json,pd.json,tidb.json,tidb_summary.json。 4. 点击 Load。 5. 选择一个 Prometheus 数据源。 6. 点击 Import,Prometheus 面板即导入成功。
查看组件 metrics
在顶部菜单中,点击 New dashboard,选择要查看的面板。

可查看以下集群组件信息:
-
TiDB Server:
-
query 处理时间,可以看到延迟和吞吐
- ddl 过程监控
- TiKV client 相关的监控
- PD client 相关的监控
-
PD Server:
-
命令执行的总次数
- 某个命令执行失败的总次数
- 某个命令执行成功的耗时统计
- 某个命令执行失败的耗时统计
- 某个命令执行完成并返回结果的耗时统计
-
TiKV Server:
-
GC 监控
- 执行 KV 命令的总次数
- Scheduler 执行命令的耗时统计
- Raft propose 命令的总次数
- Raft 执行命令的耗时统计
- Raft 执行命令失败的总次数
- Raft 处理 ready 状态的总次数
