TiDB 集群监控

    • 状态接口:通过 HTTP 接口对外汇报组件的信息。
    • :使用 Prometheus 记录组件中各种操作的详细信息,使用 Grafana 进行可视化展示。

    状态接口用于监控组件的一些基本信息,并且可以作为 keepalive 的监测接口。另外,通过 PD 的状态接口可以看到整个 TiKV 集群的详细信息。

    • TiDB API 地址:http://${host}:${port}
    • 默认端口:10080
    • 各类 api_name 详细信息:参见 TiDB API 文档

    以下示例中,通过访问 http://${host}:${port}/status 获取当前 TiDB Server 的状态,并判断该 TiDB Server 是否存活。结果以 JSON 格式返回:

    1. {
    2. connections: 0, # 当前 TiDB Server 上的客户端连接数
    3. version: "5.7.25-TiDB-v3.0.0-beta-250-g778c3f4a5", # TiDB 版本号
    4. git_hash: "778c3f4a5a716880bcd1d71b257c8165685f0d70" # TiDB 当前代码的 Git Hash
    5. }
    • PD API 地址:http://${host}:${port}/pd/api/v1/${api_name}
    • 默认端口:2379
    • 各类 api_name 详细信息:参见

    通过该接口可以获取当前所有 TiKV 节点的状态以及负载均衡信息。下面以一个单节点的 TiKV 集群为例,说明用户需要了解的信息:

    1. curl http://127.0.0.1:2379/pd/api/v1/stores
    1. {
    2. "count": 1, # TiKV 节点数量
    3. "stores": [ # TiKV 节点的列表
    4. # 集群中单个 TiKV 节点的信息
    5. {
    6. "store": {
    7. "id": 1,
    8. "address": "127.0.0.1:20160",
    9. "version": "3.0.0-beta",
    10. "state_name": "Up"
    11. },
    12. "status": {
    13. "capacity": "20 GiB", # 存储总容量
    14. "available": "16 GiB", # 存储剩余容量
    15. "leader_count": 17,
    16. "leader_weight": 1,
    17. "leader_score": 17,
    18. "leader_size": 17,
    19. "region_count": 17,
    20. "region_weight": 1,
    21. "region_score": 17,
    22. "region_size": 17,
    23. "start_ts": "2019-03-21T14:09:32+08:00", # 启动时间
    24. "last_heartbeat_ts": "2019-03-21T14:14:22.961171958+08:00", # 最后一次心跳的时间
    25. }
    26. ]

    使用 metrics 接口

    Metrics 接口用于监控整个集群的状态和性能。

    • 如果使用 TiDB Ansible 部署 TiDB 集群,监控系统(Prometheus 和 Grafana)会同时部署。
    • 如果使用其他方式部署 TiDB 集群,在使用 metrics 接口前,需先。

    成功部署 Prometheus 和 Grafana 之后,配置 Grafana

    假设 TiDB 的拓扑结构如下:

    第 1 步:下载二进制包

    下载二进制包:

    解压二进制包:

    1. tar -xzf prometheus-2.8.1.linux-amd64.tar.gz
    2. tar -xzf node_exporter-0.17.0.linux-amd64.tar.gz
    3. tar -xzf grafana-6.1.6.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. ...
    2. global:
    3. scrape_interval: 15s
    4. evaluation_interval: 15s
    5. # scrape_timeout 设置为全局默认值 (10s)
    6. external_labels:
    7. cluster: 'test-cluster'
    8. monitor: "prometheus"
    9. scrape_configs:
    10. - job_name: 'overwritten-nodes'
    11. honor_labels: true # 不要覆盖 job 和实例的 label
    12. static_configs:
    13. - targets:
    14. - '192.168.199.113:9100'
    15. - '192.168.199.114:9100'
    16. - '192.168.199.115:9100'
    17. - '192.168.199.116:9100'
    18. - '192.168.199.117:9100'
    19. - '192.168.199.118:9100'
    20. - job_name: 'tidb'
    21. honor_labels: true # 不要覆盖 job 和实例的 label
    22. static_configs:
    23. - targets:
    24. - '192.168.199.113:10080'
    25. - job_name: 'pd'
    26. honor_labels: true # 不要覆盖 job 和实例的 label
    27. static_configs:
    28. - targets:
    29. - '192.168.199.113:2379'
    30. - '192.168.199.114:2379'
    31. - '192.168.199.115:2379'
    32. - job_name: 'tikv'
    33. honor_labels: true # 不要覆盖 job 和实例的 label
    34. static_configs:
    35. - targets:
    36. - '192.168.199.117:20180'
    37. - '192.168.199.118:20180'
    38. ...

    启动 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

    1. cd grafana-6.1.6 &&
    2. vi conf/grafana.ini

    启动 Grafana 服务:

    1. ./bin/grafana-server \
    2. --config="./conf/grafana.ini" &

    本小节介绍如何配置 Grafana。

    第 1 步:添加 Prometheus 数据源

    1. 登录 Grafana 界面。

      • 默认地址:http://localhost:3000
      • 默认账户:admin
      • 默认密码:admin

    2. 点击 Grafana 侧边栏菜单 Configuration 中的 Data Source

    3. 点击 Add data source

    4. 指定数据源的相关信息:

      • Name 处,为数据源指定一个名称。
      • Type 处,选择 Prometheus
      • URL 处,指定 Prometheus 的 IP 地址。
      • 根据需求指定其它字段。
    5. 点击 Add 保存新的数据源。

    第 2 步:导入 Grafana 面板

    执行以下步骤,为 PD Server、TiKV Server 和 TiDB Server 分别导入 Grafana 面板:

    1. 在侧边栏菜单中,依次点击 Dashboards > Import 打开 Import Dashboard 窗口。

    2. 点击 Upload .json File 上传对应的 JSON 文件(下载 )。

      注意:

      TiKV、PD 和 TiDB 面板对应的 JSON 文件分别为 tikv_summary.jsontikv_details.jsontikv_trouble_shooting.jsonpd.jsontidb.jsontidb_summary.json

    3. 点击 Load

    4. 选择一个 Prometheus 数据源。

    5. 点击 Import,Prometheus 面板即导入成功。

    在顶部菜单中,点击 New dashboard,选择要查看的面板。

    可查看以下集群组件信息:

    • TiDB Server:

      • query 处理时间,可以看到延迟和吞吐
      • ddl 过程监控
      • TiKV client 相关的监控
      • PD client 相关的监控
      • 命令执行的总次数
      • 某个命令执行失败的总次数
      • 某个命令执行成功的耗时统计
      • 某个命令执行失败的耗时统计
      • 某个命令执行完成并返回结果的耗时统计
    • TiKV Server:

      • GC 监控
      • 执行 KV 命令的总次数
      • Scheduler 执行命令的耗时统计
      • Raft propose 命令的总次数
      • Raft 执行命令的耗时统计
      • Raft 处理 ready 状态的总次数