Grafana入门:数据可视化与监控面板
Grafana入门:数据可视化与监控面板
什么是Grafana
Grafana是一个开源的数据可视化和监控平台,支持多种数据源(Prometheus、Elasticsearch、InfluxDB等),能够创建丰富的交互式仪表盘。它是DevOps团队不可或缺的监控工具。
安装Grafana
Docker安装
# 使用Docker运行Grafana
docker run -d \
--name=grafana \
-p 3000:3000 \
-v grafana-storage:/var/lib/grafana \
-e GF_SECURITY_ADMIN_PASSWORD=admin123 \
grafana/grafana:latest
# 检查运行状态
docker ps | grep grafana
APT安装(Ubuntu)
# 添加Grafana仓库
sudo apt-get install -y apt-transport-https software-properties-common wget
wget -q -O - https://apt.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://apt.grafana.com stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
# 安装Grafana
sudo apt-get update
sudo apt-get install grafana
# 启动服务
sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
配置数据源
添加Prometheus数据源
# 通过API添加数据源
curl -X POST http://admin:admin@localhost:3000/api/datasources \
-H "Content-Type: application/json" \
-d '{
"name": "Prometheus",
"type": "prometheus",
"url": "http://prometheus:9090",
"access": "proxy",
"isDefault": true
}'
添加Elasticsearch数据源
curl -X POST http://admin:admin@localhost:3000/api/datasources \
-H "Content-Type: application/json" \
-d '{
"name": "Elasticsearch",
"type": "elasticsearch",
"url": "http://elasticsearch:9200",
"access": "proxy",
"database": "logstash-*",
"jsonData": {
"timeField": "@timestamp"
}
}'
创建仪表盘
导入仪表盘
# 导入Prometheus Node Exporter仪表盘(ID: 1860)
curl -X POST http://admin:admin@localhost:3000/api/dashboards/import \
-H "Content-Type: application/json" \
-d '{
"dashboard": {
"id": null,
"title": "Node Exporter",
"panels": []
},
"overwrite": true,
"inputs": [{
"name": "DS_PROMETHEUS",
"type": "datasource",
"pluginId": "prometheus",
"value": "Prometheus"
}]
}'
使用JSON创建面板
{
"title": "CPU使用率",
"type": "graph",
"targets": [{
"expr": "100 - (avg(rate(node_cpu_seconds_total{mode=\"idle\"}[5m])) * 100)",
"legendFormat": "CPU Usage"
}],
"thresholds": [
{"value": 80, "color": "yellow"},
{"value": 95, "color": "red"}
]
}
高级功能
告警配置
# 告警规则配置
apiVersion: 1
groups:
- name: cpu-alert
rules:
- alert: HighCPUUsage
expr: 100 - (avg(rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 5m
labels:
severity: warning
annotations:
summary: "CPU使用率过高"
description: "CPU使用率超过80%,当前值 {{ $value }}%"
变量配置
# 创建模板变量
# 在仪表盘设置中添加变量
# Name: instance
# Type: Query
# Query: label_values(node_uname_info, nodename)
# 这样可以在仪表盘顶部选择不同的实例
快捷键
k + s: 保存仪表盘
k + r: 刷新仪表盘
k + t: 切换主题(亮/暗)
k + o: 打开仪表盘列表
Grafana CLI管理
# 插件管理
grafana-cli plugins install grafana-piechart-panel
grafana-cli plugins list-remote
grafana-cli plugins update-all
# 数据库迁移
grafana-cli admin reset-admin-password newpassword
# 导出仪表盘
curl -X GET http://admin:admin@localhost:3000/api/dashboards/db/my-dashboard \
-o dashboard.json
最佳实践
1. 组织结构
团队A/
├── 生产环境/
│ ├── Web服务
│ └── 数据库
└── 测试环境/
团队B/
├── API服务
└── 微服务
2. 面板布局原则
- 重要的指标放在左上角
- 使用行分组相关面板
- 合理设置刷新频率
- 添加适当的注释和描述
3. 性能优化
# 限制查询时间范围
# 在面板设置中配置 Max data points
# 使用录制规则预计算复杂查询
# 避免在单个面板中查询过多数据
常见问题
# Grafana无法连接数据源
# 检查网络连通性
curl http://prometheus:9090/-/healthy
# 仪表盘加载缓慢
# 减少面板数量
# 增加查询间隔
# 使用录制规则
# 权限问题
# 检查用户角色和组织权限
# 使用API检查权限
curl http://localhost:3000/api/user