Deploying Prometheus and Grafana on
Kubernetes for Monitoring
This guide provides step-by-step instructions for setting up Prometheus and Grafana on
Kubernetes to monitor cluster performance.
Introduction to Prometheus and Grafana
What is Prometheus?
Prometheus is an open-source monitoring system designed for time-series data collection.
It is widely used for:
• Monitoring Kubernetes clusters
• Scraping metrics from applications and services
• Triggering alerts using the Alertmanager component
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
This creates a separate namespace to deploy Prometheus and Grafana.
2. Clone the Prometheus Kubernetes Deployment Repository
We will use a ready-made configuration from GitHub.
This downloads pre-configured Kubernetes manifests.
Lists files in the current directory to verify the repository is cloned.
Navigates into the cloned directory and lists available files.
3. Deploy Prometheus and Related Services
Instead of applying each YAML file individually, we apply all configurations at once.
Deploys all Kubernetes resources for Prometheus and Alertmanager.
4. Verify the Running Pods and Services.
Lists all running pods in the monitoring namespace.
Displays all services, including Prometheus and Alertmanager.
5. Access the Prometheus Web Interface.
Prometheus is exposed via NodePort, allowing external access.
Open your browser and access: http://<NODE_IP>:<PROMETHEUS_NODEPORT>
The prometheus server is now accessible:
6. Deploy Grafana
Grafana provides visual dashboards for monitoring data.
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
Opens the grafana.yaml file for modifications (if needed).
Deploys Grafana into Kubernetes.
7. Verify the Deployment
Ensures the Grafana pod is running.
Checks if the Grafana deployment is successful.
8. Access the Grafana Dashboard.
Grafana is also exposed via a NodePort service. You can access the Grafana web
interface using the NodePort.
Identify the NodePort assigned to the Grafana service using:
Open a browser and navigate to: http://<Node-IP>:<NodePort>
Log in using default credentials:
• 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.