• TiDB 集群监控
    • 组件状态接口
      • TiDB Server
      • PD Server
    • Metrics 监控
      • TiDB Server
      • PD Server
      • TiKV Server
    • 使用 Prometheus+Grafana
      • 部署架构
      • 搭建监控系统
      • 配置
        • TiDB/PD/TiKV 配置
        • PushServer 配置
        • Prometheus 配置
        • Grafana 配置

    TiDB 集群监控

    TiDB 集群状态监控目前有两种接口,第一种是通过 HTTP 接口对外汇报组件的信息,我们称之为组件的状态接口;第二种是使用 prometheus 记录组件中各种操作的详细信息,我们称之为 metrics 接口。

    组件状态接口

    这类接口可以获取组件的一些基本信息,并且可以作为 keepalive 监测接口。另外 PD 的接口可以看到整个 TiKV 集群的详细信息。

    TiDB Server

    TiDB 对外暴露的 HTTP 接口是 http://host:port/status ,默认的端口号是 10080 (可以通过 —status 参数设置),可以通过访问这个接口获取当前 TiDB Server 的状态,以及判断是否存活。返回结果是 Json 格式:

    1. curl http://127.0.0.1:10080/status
    2. {
    3. connections: 0,
    4. version: "5.5.31-TiDB-1.0",
    5. git_hash: "b99521846ff6f71f06e2d49a3f98fa1c1d93d91b"
    6. }
    • connection: 当前 TiDB Server 上的客户端连接数
    • version: TiDB 版本号
    • git_hash: TiDB 当前代码的 Git Hash

    PD Server

    PD API 地址: http://${host}:${port}/pd/api/v1/${api_name}

    其中 port 默认为 2379,各类 api_name 详细信息参见 PD API Doc。

    通过这个接口可以获取当前所有 TiKV 的状态以及负载均衡信息。其中最重要也是最常用的接口获取 TiKV 集群所有节点状态的接口,下面以一个单个 TiKV 构成的集群为例,说明一些用户需要了解的信息:

    1. curl http://127.0.0.1:2379/pd/api/v1/stores
    2. {
    3. "count": 1, TiKV 节点数量
    4. "stores": [ // TiKV 节点的列表
    5. // 下面列出的是这个集群中单个 TiKV 节点的信息
    6. {
    7. "store": {
    8. "id": 1,
    9. "address": "127.0.0.1:22161",
    10. "state": 0
    11. },
    12. "status": {
    13. "store_id": 1, // 节点的 ID
    14. "capacity": 1968874332160, // 存储总容量
    15. "available": 1264847716352, // 存储剩余容量
    16. "region_count": 1, // 该节点上存放的 Region 数量
    17. "sending_snap_count": 0,
    18. "receiving_snap_count": 0,
    19. "start_ts": "2016-10-24T19:54:00.110728339+08:00", // 启动时间
    20. "last_heartbeat_ts": "2016-10-25T10:52:54.973669928+08:00", // 最后一次心跳时间
    21. "total_region_count": 1, // Region 数量
    22. "leader_region_count": 1, // Leader Region 数量
    23. "uptime": "14h58m54.862941589s"
    24. },
    25. "scores": [
    26. 100,
    27. 35
    28. ]
    29. }
    30. ]
    31. }

    Metrics 监控

    这部分主要对整个集群的状态、性能做监控,通过 Prometheus+Grafana 展现 metrics 数据,在下面一节会介绍如何搭建监控系统。

    TiDB Server

    • query 处理时间,可以看到延迟和吞吐
    • ddl 过程监控
    • TiKV client 相关的监控
    • PD client 相关的监控

    PD Server

    • 命令执行的总次数
    • 某个命令执行失败的总次数
    • 某个命令执行成功的耗时统计
    • 某个命令执行失败的耗时统计
    • 某个命令执行完成并返回结果的耗时统计

    TiKV Server

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

    使用 Prometheus+Grafana

    部署架构

    整个架构如下图所示,在 TiDB/PD/TiKV 三个组件的启动参数中添加 Prometheus Pushgateway 地址:

    Deployment Architecture

    搭建监控系统

    Prometheus Push Gateway
    参考: https://github.com/prometheus/pushgateway

    Prometheus Server
    参考: https://github.com/prometheus/prometheus#install

    Grafana
    参考: http://docs.grafana.org

    配置

    TiDB/PD/TiKV 配置

    • TiDB

      设置 --metrics-addr 和 --metrics-interval 两个参数,其中 metrics-addr 设为 Push Gateway 的地址,metrics-interval 为 push 的频率,单位为秒,默认值为 15

    • PD

      修改 toml 配置文件,填写 Push Gateway 的地址和推送频率

      1. [metric]
      2. # prometheus client push interval, set "0s" to disable prometheus.
      3. interval = "15s"
      4. # prometheus pushgateway address, leaves it empty will disable prometheus.
      5. address = "host:port"
    • TiKV

      修改 toml 配置文件,填写 Push Gateway 的地址和推送频率,job 字段一般设为“tikv”。

      1. [metric]
      2. # the Prometheus client push interval. Setting the value to 0s stops Prometheus client from pushing.
      3. interval = "15s"
      4. # the Prometheus pushgateway address. Leaving it empty stops Prometheus client from pushing.
      5. address = "host:port"
      6. # the Prometheus client push job name. Note: A node id will automatically append, e.g., "tikv_1".
      7. job = "tikv"

    PushServer 配置

    一般无需特殊配置,使用默认端口 9091 即可

    Prometheus 配置

    在 yaml 配置文件中添加 Push Gateway 地址:

    1. scrape_configs:
    2. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
    3. - job_name: 'TiDB'
    4. # Override the global default and scrape targets from this job every 5 seconds.
    5. scrape_interval: 5s
    6. honor_labels: true
    7. static_configs:
    8. - targets: ['host:port'] # use the Push Gateway address
    9. labels:
    10. group: 'production'

    Grafana 配置

    • 进入 Grafana Web 界面(默认地址: http://localhost:3000 ,默认账号: admin 密码: admin)

      点击 Grafana Logo -> 点击 Data Sources -> 点击 Add data source -> 填写 data source 信息 ( 注: Type 选 Prometheus,Url 为 Prometheus 地址,其他根据实际情况填写 )

    • 导入 dashboard 配置文件

      点击 Grafana Logo -> 点击 Dashboards -> 点击 Import -> 选择需要的 Dashboard 配置文件上传 -> 选择对应的 data source