Skip to content

TiDB Prometheus & Grafana 监控配置

Prometheus 和 Grafana 是 TiDB 集群监控的核心组件,用于收集、存储和可视化集群的各种指标。本文将详细介绍 TiDB 集群的 Prometheus & Grafana 监控配置方法。

监控架构

组件构成

  • Prometheus:负责从 TiDB 集群各组件采集指标数据
  • Grafana:负责将 Prometheus 采集的数据可视化展示
  • Node Exporter:采集服务器硬件和系统指标
  • Pushgateway:接收临时任务的指标数据
  • Alertmanager:处理告警通知

数据流向

  1. 各 TiDB 组件(TiDB、TiKV、PD、TiFlash 等)暴露指标端口
  2. Prometheus 定期从这些端口拉取指标数据
  3. Grafana 从 Prometheus 读取数据,通过预定义的 Dashboard 展示
  4. 当指标超过阈值时,Alertmanager 发送告警通知

安装部署

1. 使用 TiUP 部署监控组件

TiUP 提供了一键部署监控组件的功能,适用于使用 TiUP 部署的 TiDB 集群。

bash
# 部署监控组件
tiup cluster deploy <cluster-name> <tidb-version> <topology-file> --with-monitor

# 或者为已部署的集群添加监控
tiup cluster scale-out <cluster-name> <monitor-topology-file>

2. 手动部署监控组件

对于手动部署的 TiDB 集群,可以手动安装 Prometheus 和 Grafana。

安装 Prometheus

bash
# 下载 Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-amd64.tar.gz

tar xvfz prometheus-2.35.0.linux-amd64.tar.gz
cd prometheus-2.35.0.linux-amd64

# 启动 Prometheus
./prometheus --config.file=prometheus.yml

安装 Grafana

bash
# 下载并安装 Grafana
wget https://dl.grafana.com/oss/release/grafana-8.5.0.linux-amd64.tar.gz
tar xvfz grafana-8.5.0.linux-amd64.tar.gz
cd grafana-8.5.0

# 启动 Grafana
./bin/grafana-server

Prometheus 配置

1. 基础配置

yaml
# prometheus.yml 示例

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  - "tidb.rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  # TiDB 指标采集
  - job_name: 'tidb'
    static_configs:
      - targets: ['tidb-1:10080', 'tidb-2:10080']

  # TiKV 指标采集
  - job_name: 'tikv'
    static_configs:
      - targets: ['tikv-1:20180', 'tikv-2:20180', 'tikv-3:20180']

  # PD 指标采集
  - job_name: 'pd'
    static_configs:
      - targets: ['pd-1:2379', 'pd-2:2379', 'pd-3:2379']

  # TiFlash 指标采集
  - job_name: 'tiflash'
    static_configs:
      - targets: ['tiflash-1:8234', 'tiflash-2:8234']

  # Node Exporter 指标采集
  - job_name: 'node'
    static_configs:
      - targets: ['host-1:9100', 'host-2:9100', 'host-3:9100']

2. 高级配置

自动发现配置

对于动态增减节点的集群,可以配置 Prometheus 自动发现目标:

yaml
scrape_configs:
  - job_name: 'tikv'
    dns_sd_configs:
      - names:
          - 'tikv.service.consul'
        type: 'A'
        port: 20180

指标过滤

可以配置指标过滤,只采集需要的指标:

yaml
scrape_configs:
  - job_name: 'tidb'
    static_configs:
      - targets: ['tidb-1:10080']
    metric_relabel_configs:
      - source_labels: [__name__]
        regex: 'tidb_executor_.*'
        action: keep

Grafana 配置

1. 添加数据源

  1. 登录 Grafana 界面(默认地址:http://localhost:3000,默认用户名/密码:admin/admin)
  2. 点击左侧菜单中的 "Configuration" -> "Data Sources"
  3. 点击 "Add data source"
  4. 选择 "Prometheus"
  5. 配置 Prometheus 地址(如:http://prometheus:9090
  6. 点击 "Save & Test" 验证配置

2. 导入 TiDB Dashboard

TiDB 提供了预定义的 Grafana Dashboard,可以直接导入使用:

bash
# 下载 TiDB Dashboard JSON 文件
wget https://raw.githubusercontent.com/pingcap/tidb/master/metrics/dashboard.json

然后在 Grafana 中导入:

  1. 点击左侧菜单中的 "Create" -> "Import"
  2. 点击 "Upload JSON file",选择下载的 dashboard.json
  3. 选择之前添加的 Prometheus 数据源
  4. 点击 "Import" 完成导入

3. 常用 Dashboard

TiDB 提供了多个专用 Dashboard:

  • TiDB:展示 TiDB 服务器的指标
  • TiKV:展示 TiKV 服务器的指标
  • PD:展示 PD 服务器的指标
  • TiFlash:展示 TiFlash 服务器的指标
  • Cluster:展示集群整体指标
  • Node:展示服务器硬件和系统指标

监控指标

1. 核心指标类别

  • 性能指标:查询延迟、QPS、TPS、连接数等
  • 资源指标:CPU 使用率、内存使用率、磁盘 I/O、网络流量等
  • 可靠性指标:副本状态、Raft 选举、调度操作等
  • 业务指标:事务冲突、慢查询、错误率等

2. 关键指标示例

TiDB 关键指标

  • tidb_server_qps:TiDB 每秒处理的查询数
  • tidb_server_tps:TiDB 每秒处理的事务数
  • tidb_session_connect_count:当前连接数
  • tidb_executor_stmt_duration_seconds:SQL 执行延迟

TiKV 关键指标

  • tikv_engine_size_bytes:RocksDB 引擎大小
  • tikv_raft_logs_applied_total:Raft 日志应用总数
  • tikv_storage_thread_cpu_seconds_total:存储线程 CPU 使用率
  • tikv_server_throughput:TiKV 吞吐量

PD 关键指标

  • pd_server_is_leader:是否为 PD Leader
  • pd_cluster_region_count:集群 Region 总数
  • pd_scheduler_operator_total:调度操作总数
  • pd_hot_region:热点 Region 数量

告警配置

1. Prometheus 告警规则

Prometheus 通过告警规则定义告警条件,示例:

yaml
# tidb.rules.yml
groups:
- name: tidb_alerts
  rules:
  - alert: TiDBHighConnectionCount
    expr: tidb_session_connect_count > 1000
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "TiDB 连接数过高"
      description: "TiDB 连接数超过 1000,当前值: {{ $value }}"

  - alert: TiKVDiskAlmostFull
    expr: tikv_store_available_bytes / tikv_store_total_bytes < 0.1
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "TiKV 磁盘空间不足"
      description: "TiKV 磁盘可用空间不足 10%,当前值: {{ $value | printf \"%.2f\" }}%"

2. Alertmanager 配置

Alertmanager 负责处理 Prometheus 发送的告警,并通过各种渠道发送通知:

yaml
# alertmanager.yml
global:
  smtp_smarthost: 'smtp.example.com:587'
  smtp_from: 'alertmanager@example.com'
  smtp_auth_username: 'alertmanager'
  smtp_auth_password: 'password'

route:
  group_by: ['alertname', 'cluster', 'service']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 4h
  receiver: 'email-notifications'

receivers:
- name: 'email-notifications'
  email_configs:
  - to: 'admin@example.com'
    send_resolved: true

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'cluster', 'service']

监控最佳实践

1. 部署建议

  • 独立部署:监控组件应独立部署在专用服务器上,避免影响生产集群
  • 高可用配置:Prometheus 和 Grafana 应配置高可用,避免单点故障
  • 适当的存储策略:根据集群规模和监控需求,配置合适的 Prometheus 数据保留时间

2. 配置优化

  • 合理的采集间隔:根据指标重要性调整采集间隔,核心指标可缩短间隔
  • 指标过滤:只采集必要的指标,减少存储和计算开销
  • 告警规则优化:避免过多的告警,设置合理的阈值和持续时间

3. 日常维护

  • 定期备份:定期备份 Grafana Dashboard 配置和 Prometheus 告警规则
  • 监控监控系统:监控 Prometheus 和 Grafana 自身的运行状态
  • 定期清理:定期清理过期的监控数据,避免磁盘空间不足

4. 告警管理

  • 分级告警:根据问题严重程度设置不同级别的告警
  • 告警聚合:对同类告警进行聚合,避免告警风暴
  • 告警抑制:当严重告警发生时,抑制次要告警
  • 告警演练:定期进行告警演练,确保告警渠道畅通

常见问题(FAQ)

Q1: Prometheus 采集不到 TiDB 组件的指标怎么办?

A1: 可以按照以下步骤排查:

  1. 检查 TiDB 组件是否正常运行:ps aux | grep tidb-server
  2. 检查指标端口是否正常监听:netstat -tuln | grep 10080
  3. 检查防火墙是否开放了指标端口:iptables -L -n | grep 10080
  4. 手动访问指标端点:curl http://tidb-1:10080/metrics
  5. 检查 Prometheus 配置是否正确

Q2: Grafana 中看不到最新的数据怎么办?

A2: 可能的原因和解决方法:

  1. Prometheus 采集延迟:检查 Prometheus 的 scrape_interval 配置
  2. 数据查询时间范围不正确:调整 Grafana 中的时间范围
  3. Prometheus 数据保留时间过短:调整 Prometheus 的 --storage.tsdb.retention.time 参数
  4. 数据源配置错误:重新检查 Grafana 数据源配置

Q3: 如何自定义 TiDB Dashboard?

A3: 可以按照以下步骤自定义:

  1. 进入需要修改的 Dashboard
  2. 点击右上角的 "Settings" 图标
  3. 在 "General" 标签页中,勾选 "Make a copy" 创建副本
  4. 对副本进行修改,包括添加/删除面板、调整查询语句等
  5. 点击 "Save" 保存修改

Q4: 如何设置告警通知到企业微信?

A4: 需要配置 Alertmanager 使用 webhook 发送到企业微信:

  1. 在 Alertmanager 配置中添加企业微信 receiver:
    yaml
    receivers:
    - name: 'wechat-notifications'
      wechat_configs:
      - corp_id: 'your-corp-id'
        api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
        to_party: '1'
        agent_id: '1000001'
        api_secret: 'your-api-secret'
  2. 将 route 配置指向该 receiver

Q5: 如何监控多个 TiDB 集群?

A5: 可以采用以下两种方式:

  1. 单 Prometheus 实例:在一个 Prometheus 实例中配置多个集群的采集任务
  2. 多 Prometheus 实例:为每个集群部署独立的 Prometheus 实例,然后使用 Thanos 或 Cortex 进行统一管理

对于多集群监控,建议使用第二种方式,可以更好地隔离不同集群的监控数据,并提高整体可靠性。