Grafana 仪表盘
学习目标
- 理解 Grafana 的作用和架构
- 掌握创建和配置仪表盘
- 学会导入社区仪表盘和配置告警
核心概念
Grafana 架构
┌─────────────────────────────────────────────────────────┐
│ Grafana 架构 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ Grafana Server │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ 仪表盘 │ │ 告警 │ │ 用户管理 │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
│ └───────────────────────┬──────────────────────────┘ │
│ │ │
│ ┌────────────────┼────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ Prometheus │ │ Loki │ │ InfluxDB │ │
│ │ (指标) │ │ (日志) │ │ (指标) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ 数据源(Data Sources) │
└─────────────────────────────────────────────────────────┘
核心概念
| 概念 | 说明 |
|---|---|
| Dashboard | 仪表盘,包含多个面板 |
| Panel | 面板,显示单个可视化 |
| Data Source | 数据源,如 Prometheus |
| Variable | 变量,实现动态过滤 |
| Alert | 告警规则 |
面板类型
1. Time Series - 时序折线图(最常用)
2. Stat - 单一数值展示
3. Gauge - 仪表盘
4. Bar Chart - 柱状图
5. Table - 表格
6. Heatmap - 热力图
7. Logs - 日志流
实践练习
练习 1:访问 Grafana
# 如果使用 kube-prometheus-stack
kubectl port-forward svc/prometheus-grafana 3000:80 -n monitoring
# 默认凭据
# 用户名: admin
# 密码: prom-operator(kube-prometheus-stack 默认)
# 访问 http://localhost:3000
练习 2:导入社区仪表盘
1. 访问 https://grafana.com/grafana/dashboards/
2. 搜索需要的仪表盘(如 Node Exporter、Kubernetes Cluster)
3. 复制仪表盘 ID
4. 在 Grafana 中:+ → Import → 粘贴 ID → Load
5. 选择 Prometheus 数据源 → Import
常用仪表盘 ID:
| ID | 名称 | 用途 |
|---|---|---|
| 315 | Kubernetes Cluster Monitoring | 集群概览 |
| 1860 | Node Exporter Full | Node 详情 |
| 6417 | Kubernetes Pods | Pod 详情 |
| 13770 | Kubernetes Persistent Volumes | 存储监控 |
练习 3:创建自定义仪表盘
步骤:
1. 点击 + → New Dashboard
2. 添加面板 → Add Panel
3. 选择数据源(Prometheus)
4. 编写 PromQL 查询
5. 配置可视化选项
6. 保存仪表盘
示例 PromQL 查询:
- 节点 CPU 使用率:100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
- Pod 内存使用:container_memory_usage_bytes{namespace="default"}
- HTTP 请求速率:sum(rate(http_requests_total[5m]))
练习 4:配置变量
1. 仪表盘设置 → Variables → Add variable
2. 配置示例:
- Name: namespace
- Type: Query
- Query: label_values(kube_pod_info, namespace)
3. 在面板查询中使用:$namespace
container_memory_usage_bytes{namespace="$namespace"}
练习 5:配置告警
# Grafana 告警配置
apiVersion: 1
contactPoints:
- name: slack
receivers:
- uid: slack_channel
type: slack
settings:
url: https://hooks.slack.com/services/xxx/yyy/zzz
recipient: #alerts
alertRules:
- name: High CPU Usage
condition: A
data:
- refId: A
datasourceUid: prometheus
model:
expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
> 80
for: 5m
labels:
severity: warning
练习 6:Grafana Loki(日志)
# 添加 Loki 数据源
# Configuration → Data Sources → Add data source
# Type: Loki
# URL: http://loki:3100
# 查询日志
# Explore → Loki
# 查询示例:{namespace="default"} |= "error"
最佳实践
仪表盘设计原则
1. 层次清晰
- 顶部:关键指标(Stat 面板)
- 中部:趋势图(Time Series)
- 底部:详细数据(Table)
2. 命名规范
- 使用统一的命名格式
- 包含环境标识
3. 性能优化
- 避免过多面板
- 合理设置刷新间隔
- 使用变量减少重复
小结
| 要点 | 说明 |
|---|---|
| Grafana | 可视化和监控平台 |
| 核心概念 | Dashboard、Panel、Data Source |
| 社区资源 | grafana.com/dashboards |
| 数据源 | Prometheus、Loki、InfluxDB |
| 告警 | 统一告警管理 |
练习编辑器
bash
Loading...