Grafana
Overview
Grafana provides visualization and dashboarding for metrics from Prometheus and other data sources.
Access
Data Sources
| Name |
Type |
URL |
| Prometheus |
prometheus |
http://prometheus:9090 |
| InfluxDB |
influxdb |
http://influxdb.databases:8086 |
| Elasticsearch |
elasticsearch |
http://elasticsearch.monitoring:9200 |
Adding Data Source
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-datasources
namespace: monitoring
data:
datasources.yaml: |
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://prometheus:9090
access: proxy
isDefault: true
Dashboards
Infrastructure Dashboards
| Dashboard |
Description |
| Kubernetes Cluster |
Overall cluster health |
| Node Exporter |
Per-node system metrics |
| Pods |
Container resource usage |
| Longhorn |
Storage metrics |
Application Dashboards
| Dashboard |
Description |
| Hub API |
API request metrics |
| Nginx |
Web server metrics |
| Wiki |
Wiki site metrics |
Database Dashboards
| Dashboard |
Description |
| InfluxDB |
Time series metrics |
| MongoDB |
Document database |
| Elasticsearch |
Search and logs |
Dashboard Provisioning
ConfigMap for Dashboards
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-dashboards
namespace: monitoring
labels:
grafana_dashboard: "1"
data:
dashboard.json: |
{
"title": "My Dashboard",
"panels": [...]
}
Dashboard Provider
apiVersion: 1
providers:
- name: 'default'
folder: ''
type: file
options:
path: /var/lib/grafana/dashboards
Alerting
Grafana Alerts
Grafana can send alerts based on dashboard panels:
- Create alert rule in panel
- Set threshold conditions
- Configure notification channel
| Type |
Description |
| Email |
SMTP notifications |
| Slack |
Webhook integration |
| PagerDuty |
Incident management |
Variables
Template Variables
label_values(kube_pod_info, namespace)
label_values(kube_pod_info{namespace="$namespace"}, pod)
Useful Variables
| Variable |
Query |
$namespace |
label_values(namespace) |
$pod |
label_values(kube_pod_info{namespace="$namespace"}, pod) |
$node |
label_values(node_uname_info, nodename) |
Panels
Common Panel Types
| Type |
Use Case |
| Time series |
Metrics over time |
| Stat |
Single values |
| Gauge |
Threshold indicators |
| Table |
Tabular data |
| Logs |
Log lines (with Loki/ES) |
Example Time Series Panel
{
"type": "timeseries",
"title": "CPU Usage",
"targets": [
{
"expr": "rate(container_cpu_usage_seconds_total[5m])",
"legendFormat": "{{pod}}"
}
]
}
Best Practices
- Use variables for dynamic filtering
- Set refresh intervals appropriately
- Add annotations for deployments
- Use folders to organize dashboards
- Version control dashboard JSON
API
Dashboard API
# Export dashboard
curl -H "Authorization: Bearer $TOKEN" \
https://grafana.ajandrews.pro/api/dashboards/uid/xyz
# Import dashboard
curl -X POST -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d @dashboard.json \
https://grafana.ajandrews.pro/api/dashboards/db