Grafana and Prometheus Setup in Kubernetes Using Helm
This README provides step-by-step instructions for setting up Grafana and Prometheus for monitoring in a Kubernetes cluster using Helm.
Prerequisites
- Kubernetes Cluster: A running Kubernetes cluster. You can use Minikube, a managed cluster, or any other Kubernetes provider.
- Helm: Installed and configured in your environment. Follow Helm’s installation guide (opens in a new tab) if needed.
- kubectl: Installed and configured for your Kubernetes cluster. Refer to the kubectl installation guide (opens in a new tab) for setup instructions.
Step 1: Create a Namespace
First, create a namespace for monitoring resources:
kubectl create namespace monitoringStep 2: Add Helm Repositories
Add the required Helm chart repositories for Prometheus and Grafana:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add grafana https://grafana.github.io/helm-charts
helm repo updateStep 3: Install Prometheus
Install Prometheus using Helm:
helm install prometheus prometheus-community/prometheus -n monitoringThe Prometheus server can be accessed via port 80 using the following DNS name from within your cluster:
http://prometheus-server.monitoring.svc.cluster.localStep 4: Install Grafana
Install Grafana using Helm:
helm install grafana grafana/grafana -n monitoringAccessing Grafana
-
Get your 'admin' user password by running the following command:
kubectl get secret --namespace monitoring grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echoThe default password will be displayed. For example:
PASS : aIwX6UK5LLkHQWKmGGQvCtME0BA93YKaH4T91tZt -
You can access Grafana at:
grafana.monitoring.svc.cluster.localStep 5: Expose Services to Localhost
To access Prometheus and Grafana from your local machine, use port-forwarding: (or you can use LB or ingress )
kubectl port-forward service/prometheus-server --namespace monitoring 8090:80
kubectl port-forward service/grafana --namespace monitoring 3000:80
kubectl patch svc grafana -n monitoring -p '{"spec": {"type": "NodePort"}}'Now you can access:
- Prometheus:
http://localhost:8090 - Grafana:
http://localhost:3000
Step 6: Configure Prometheus as a Data Source in Grafana
- Open Grafana (
http://localhost:3000) and log in. - Go to Configuration > Data Sources.
- Click Add Data Source and select Prometheus.
- Set the URL to
http://prometheus-server.monitoring.svc.cluster.local:80. - Click Save & Test to verify the data source is working.
Step 7: Import Dashboards
You can enhance your Grafana setup by importing pre-configured dashboards. Here are some popular Kubernetes dashboards:
Dashboard IDs
Refer to the following dashboards from dotdc/grafana-dashboards-kubernetes (opens in a new tab):
| Dashboard Name | Dashboard ID |
|---|---|
| k8s-addons-prometheus.json | 19105 |
| k8s-addons-trivy-operator.json | 16337 |
| k8s-system-api-server.json | 15761 |
| k8s-system-coredns.json | 15762 |
| k8s-views-global.json | 15757 |
| k8s-views-namespaces.json | 15758 |
| k8s-views-nodes.json | 15759 |
| k8s-views-pods.json | 15760 |
Cleanup (Optional)
To uninstall Prometheus and Grafana, run the following commands:
helm uninstall prometheus -n monitoring
helm uninstall grafana -n monitoring
kubectl delete namespace monitoringTroubleshooting
- Grafana or Prometheus Not Accessible: Ensure port-forwarding is set up correctly or check the service type for external access.
- Data Not Showing in Grafana: Confirm that the Prometheus data source URL is correct in Grafana.
Additional Resources
- Helm Charts for Prometheus (opens in a new tab)
- Helm Charts for Grafana (opens in a new tab)
- Prometheus Documentation (opens in a new tab)
- Grafana Documentation (opens in a new tab)
This README provides a foundational setup for Grafana and Prometheus, which you can further customize based on your monitoring needs.