Kubernetes Monitoring Using Prometheus and Grafana
Kubernetes Monitoring Using Prometheus and Grafana
Key Features:
✅ Multi-dimensional data model (time-series data)
✅ Powerful query language (PromQL)
✅ Pull-based data collection
✅ Built-in Alertmanager for notifications
What is Grafana?
Grafana is an open-source visualization tool used to create dashboards and graphs based
on data from Prometheus, Elasticsearch, InfluxDB, and other sources.
Key Features:
✅ Rich visualizations (graphs, tables, heatmaps)
✅ Custom dashboards
✅ Data source integration
✅ User authentication & role-based access
1. Create a Namespace for Monitoring
Instead of applying each YAML file individually, we apply all configurations at once.
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-datasources
namespace: monitoring
data:
prometheus.yaml: |-
{
"apiVersion": 1,
"datasources": [
{
"access":"proxy",
"editable": true,
"name": "prometheus",
"orgId": 1,
"type": "prometheus",
"url": "http://prometheus-service.monitoring.svc:8080",
"version": 1
}
]
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: grafana
template:
metadata:
name: grafana
labels:
app: grafana
spec:
containers:
- name: grafana
image: grafana/grafana:latest
ports:
- name: grafana
containerPort: 3000
resources:
limits:
memory: "2Gi"
cpu: "1000m"
requests:
memory: "1Gi"
cpu: "500m"
volumeMounts:
- mountPath: /var/lib/grafana
name: grafana-storage
- mountPath: /etc/grafana/provisioning/datasources
name: grafana-datasources
readOnly: false
volumes:
- name: grafana-storage
emptyDir: {}
- name: grafana-datasources
configMap:
defaultMode: 420
name: grafana-datasources
---
apiVersion: v1
kind: Service
metadata:
name: grafana
namespace: monitoring
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '3000'
spec:
selector:
app: grafana
type: NodePort
ports:
- port: 3000
targetPort: 3000
nodePort: 3200
• Username: admin
• Password: admin (or the one you set)
9. Add Prometheus as a Data Source:
• Go to Settings → Data Sources → Add Prometheus
• Set the URL:
http://<NODE_IP>:<PROMETHEUS_NODEPORT>
The Grafana dashboard is set up, and the cluster resources can now be
monitored effectively.