Grafana
核心功能
Grafana 是开源的可视化与可观测性平台,支持多数据源统一展示。
| 功能 | 说明 |
|---|---|
| 多数据源 | Prometheus、Loki、Elasticsearch、MySQL、InfluxDB 等 |
| 仪表盘 | 丰富的面板类型(图表、表格、热力图、日志等) |
| 告警 | Grafana Alerting 统一告警管理 |
| 变量 | 仪表盘变量实现动态切换(Namespace / Instance) |
| 注解 | 标记部署、事件等时间节点 |
Dashboard 设计
常用面板类型
| 面板 | 用途 | 适用指标 |
|---|---|---|
| Time Series | 时间序列折线图 | CPU 使用率、QPS |
| Stat | 单值展示 | 总数、当前值 |
| Gauge | 仪表盘 | 使用率百分比 |
| Table | 表格 | Top N、详情列表 |
| Heatmap | 热力图 | 延迟分布 |
| Logs | 日志面板 | Loki 日志 |
| Alert List | 告警列表 | 当前活跃告警 |
Dashboard 变量
# 定义变量 $namespace(从 Prometheus 获取所有 Namespace)
label_values(kube_pod_info, namespace)
# 定义变量 $pod(根据已选 namespace 过滤)
label_values(kube_pod_info{namespace="$namespace"}, pod)
# 在查询中使用变量
rate(container_cpu_usage_seconds_total{namespace="$namespace", pod="$pod"}[5m])
推荐 Dashboard
| Dashboard ID | 名称 | 数据源 | 用途 |
|---|---|---|---|
| 1860 | Node Exporter Full | Prometheus | 主机监控 |
| 3662 | Prometheus 2.0 Overview | Prometheus | Prometheus 自身 |
| 7362 | MySQL Overview | Prometheus | MySQL 监控 |
| 763 | Redis Dashboard | Prometheus | Redis 监控 |
| 15757 | K8s Views / Pods | Prometheus | K8s Pod 监控 |
导入方式:Grafana → Dashboards → Import → 输入 Dashboard ID。
Grafana Alerting
Grafana 统一告警(Grafana 9+)支持多数据源告警。
告警规则示例
# 在 Grafana UI 或 Provisioning 中配置
# 条件:CPU > 85% 持续 5 分钟
# 表达式:
# A = avg by(instance)(rate(node_cpu_seconds_total{mode="idle"}[5m]))
# B = 100 - A * 100
# C = B > 85
通知渠道
| 渠道 | 配置 |
|---|---|
| 邮件 | SMTP 配置 |
| Slack | Webhook URL |
| 钉钉 | 机器人 Webhook |
| 飞书 | 机器人 Webhook |
| PagerDuty | Integration Key |
| Webhook | 自定义 HTTP 回调 |
告警模板
通知模板
{{ define "custom.title" }}
[{{ .Status | toUpper }}] {{ .CommonLabels.alertname }}
{{ end }}
{{ define "custom.message" }}
严重级别: {{ .CommonLabels.severity }}
实例: {{ .CommonLabels.instance }}
当前值: {{ .CommonAnnotations.value }}
描述: {{ .CommonAnnotations.summary }}
{{ end }}
Grafana as Code
Provisioning(自动化配置)
provisioning/datasources/prometheus.yml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://prometheus:9090
access: proxy
isDefault: true
- name: Loki
type: loki
url: http://loki:3100
access: proxy
provisioning/dashboards/default.yml
apiVersion: 1
providers:
- name: Default
folder: ''
type: file
options:
path: /var/lib/grafana/dashboards
Terraform 管理
Terraform Grafana Provider
resource "grafana_dashboard" "node" {
config_json = file("dashboards/node-exporter.json")
folder = grafana_folder.monitoring.id
}
resource "grafana_data_source" "prometheus" {
type = "prometheus"
name = "Prometheus"
url = "http://prometheus:9090"
}
常见面试问题
Q1: Grafana 告警和 Alertmanager 告警哪个更好?
答案:
| 维度 | Grafana Alerting | Alertmanager |
|---|---|---|
| 数据源 | 多数据源(Prometheus、Loki、MySQL 等) | 仅 Prometheus |
| 可视化 | 与 Dashboard 集成 | 无可视化 |
| 路由 | 简单 | 复杂路由、分组、抑制 |
| 生态 | Grafana 生态 | Prometheus 生态 |
推荐:小团队用 Grafana Alerting 更简单;大型平台用 Alertmanager 更灵活。
Q2: 如何设计运维监控 Dashboard?
答案:
按层级组织:
- 总览 Dashboard:全局健康状态(绿/黄/红),关键 SLI
- 基础设施:Node CPU / 内存 / 磁盘 / 网络
- K8s:Pod 状态、资源使用、Deployment
- 中间件:MySQL QPS / 慢查询、Redis 命中率 / 内存
- 应用:QPS / 错误率 / P99 延迟
每个 Dashboard 用变量(Namespace / Instance)做动态切换。