Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes

Monitor Cluster with Prometheus

Learn to monitor a Kubernetes cluster using the Prometheus stack. This lesson covers installation, configuration, and key metrics to observe for cluster health.

Focus: monitor cluster with prometheus stack

Sponsored

When your Kubernetes cluster goes down, do you rely on guesswork? Without observability, you're flying blind — unable to see CPU spikes, memory leaks, or failing pods until it's too late. The Prometheus stack (kube-prometheus-stack) is the gold standard for monitoring clusters with Prometheus, collecting metrics from nodes, pods, and services, and alerting you before users feel the pain.

The problem this lesson solves

Kubernetes is dynamic: pods die, nodes scale, and workloads shift. Traditional monitoring tools (like Nagios) expect static IPs and fixed services — they break in a cluster that auto-heals. Manual kubectl top commands give a snapshot, not history. You need a system that:

  • Discovers targets automatically via Kubernetes APIs
  • Stores time-series metrics (CPU, memory, network, disk)
  • Alerts when thresholds cross (e.g., node disk pressure)
  • Visualizes trends over time (via Grafana)

The Prometheus stack solves all of this in one cohesive deployment.

Core concept / mental model

Think of Prometheus as a polling camera, not a log file. Instead of waiting for errors, Prometheus periodically scrapes HTTP endpoints (/metrics) exposed by applications and the cluster itself. Each scrape pulls key-value pairs called metrics (e.g., container_memory_usage_bytes).

Metrics flow like this:

  1. Exporters (on nodes, pods, or third-party services) expose /metrics.
  2. Prometheus server scrapes those endpoints at a configurable interval (default 30s).
  3. The data is stored in a time-series database and queried via PromQL.
  4. Alertmanager fires notifications (email, Slack, PagerDuty) when PromQL rules trigger.
  5. Grafana visualizes metrics with dashboards.

Pro tip: The default kube-prometheus-stack Helm chart bundles Prometheus, Alertmanager, and Grafana into one launch — you get a full observability stack out of the box.

Key terms to understand

  • Scrape interval — how often Prometheus polls targets (adjust for cost vs granularity).
  • PromQL — the query language for slicing metrics (e.g., sum(rate(container_cpu_usage_seconds_total[5m]))).
  • Alert rules — PromQL expressions that evaluate to pending or firing alerts.
  • ServiceMonitor — a Kubernetes CRD that tells Prometheus which services to monitor.

How it works step by step

  1. Install the stack using Helm or raw manifests. Helm is recommended because it manages upgrades, ConfigMaps, and RBAC.

  2. Configure ServiceMonitors to scrape cluster components (kube-controller-manager, etcd, kube-proxy) and your own applications.

  3. Add custom exporters — e.g., Node Exporter for node metrics, kube-state-metrics for pod states.

  4. Create alerting rules — Prometheus supplies default community rules for common issues (e.g., high memory, pod crash looping).

  5. Set up Grafana dashboards — import pre-built dashboards (like Kubernetes Cluster Monitoring via 315) or build custom panels.

  6. Define notification receivers in Alertmanager — Slack webhook, email SMTP, or OpsGenie.

The stack automatically discovers nodes and services via Kubernetes service discovery — no manual target lists.

Hands-on walkthrough

Install kube-prometheus-stack with Helm

First, add the repository and install the chart into a dedicated monitoring namespace:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

kubectl create namespace monitoring

helm install monitor-cluster prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --set grafana.enabled=true \
  --set grafana.service.type=LoadBalancer

Expected output (similar):

NAME: monitor-cluster
LAST DEPLOYED: ...
NAMESPACE: monitoring
STATUS: deployed
...

Verify the components are running

kubectl get pods -n monitoring

You should see pods like: - monitor-cluster-kube-state-metrics-* - monitor-cluster-prometheus-node-exporter-* - monitor-cluster-grafana-* - prometheus-monitor-cluster-kube-prome-prometheus-0

Access Grafana

Get the LoadBalancer IP:

kubectl get svc -n monitoring | grep grafana

Open http://<EXTERNAL-IP>:80 in your browser. Default credentials: - Username: admin - Password: prom-operator

Once logged in, explore the “Kubernetes / Cluster” dashboard. You’ll see real-time CPU, memory, disk I/O, and network traffic across all nodes.

Query a metric in Prometheus

Port-forward Prometheus server:

kubectl port-forward svc/monitor-cluster-kube-prome-prometheus 9090 -n monitoring

Open http://localhost:9090, and run a PromQL query:

sum(rate(container_cpu_usage_seconds_total[5m])) by (namespace)

This returns CPU usage per namespace over the last 5 minutes.

Compare options / when to choose what

Tool Strengths Weaknesses Best for
kube-prometheus-stack Full stack (Alertmanager + Grafana), easy to deploy, community rules Heavy (multiple pods), higher resource overhead Production clusters needing comprehensive monitoring
Prometheus Operator (standalone) Lightweight, full control over configuration No built-in Grafana, more manual setup Teams who already run Grafana or need custom alert pipelines
Managed services (AWS Managed Prometheus, GCP Cloud Monitoring) Zero ops, auto-scaling, integrated with cloud console Vendor lock-in, limited CRD support Teams that want least ops overhead

Pro tip: For small dev clusters, the standalone Prometheus Operator is lighter. For production, kube-prometheus-stack saves you hours of wiring.

Variations to consider

  • Use kube-prometheus (the old deprecated project) vs the newer kube-prometheus-stack — always choose the stack.
  • Deploy without Grafana and point to an existing Grafana instance.
  • Enable persistent storage for Prometheus to survive pod restarts (set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate).

Troubleshooting & edge cases

Scraping fails: endpoints not found

Check that your apps expose a /metrics endpoint returning valid Prometheus format. Test with curl <pod-ip>:<port>/metrics from within the cluster. Also verify the ServiceMonitor label selector matches your service labels.

High memory usage from Prometheus

Prometheus is memory-intensive when scraping thousands of targets. Reduce the scrape interval to 60s and enable sample limit in your ServiceMonitor (.spec.endpoints[0].metricRelabelings).

Alertmanager not sending notifications

Check Alertmanager configuration. Ensure the receivers section is correct and webhook URLs are reachable from inside the cluster. Test with a simple alert like ALERT TestAlert IF 1 == 1 FOR 1m.

Common mistakes

  • Not setting resource limits — the default heap size can cause OOMKill for large clusters.
  • Using default passwords — change Grafana admin password immediately.
  • Ignoring etcd metrics — etcd latency is critical for control plane health but often not scraped by default.
  • Skipping node exporter — without it, you miss node-level disk I/O and network errors.

What you learned & what's next

You can now monitor your cluster with Prometheus stack: install it with Helm, access Grafana dashboards, query metrics with PromQL, and understand the roles of ServiceMonitors and exporters. You’ve seen how to troubleshoot basic scraping issues and compare deployment options.

Next up: Lesson 60 — Set up alerting rules — where you’ll write custom alerting expressions to get notified before failures happen.

Practice recap

Deploy kube-prometheus-stack in a test or dev cluster using the Helm commands from this lesson. Once installed, open Grafana and explore the 'Kubernetes / Cluster' dashboard. Then write a PromQL query to find the top 3 namespaces by CPU usage over the last hour.

Common mistakes

  • Not setting resource limits for Prometheus and Grafana — large clusters can OOMKill the main Prometheus pod.
  • Using default Grafana admin password — an easy vector for unauthorized access; change it immediately after deployment.
  • Forgetting to scrape etcd and controller-manager — these control-plane metrics are critical but often not included in default ServiceMonitors.
  • Skipping Node Exporter — without it you miss node-level disk I/O, network errors, and filesystem usage.

Variations

  1. Use standalone Prometheus Operator without Grafana if you already run Grafana elsewhere.
  2. Deploy kube-prometheus-stack with persistent storage enabled to retain metrics across pod restarts.
  3. For large clusters, consider Thanos as a long-term storage backend for Prometheus data.

Real-world use cases

  • Production cluster health — detect node pressure or pod crash loops before they affect users.
  • Capacity planning — track CPU/memory usage trends over weeks to size nodes or quotas.
  • Cost optimization — monitor idle resources across namespaces and downsize over-provisioned workloads.

Key takeaways

  • kube-prometheus-stack bundles Prometheus, Alertmanager, and Grafana for one-command cluster monitoring.
  • ServiceMonitor CRDs auto-discover targets via Kubernetes labels — no manual IP lists needed.
  • PromQL queries slice metrics by namespace, pod, or container — great for debugging and dashboards.
  • Always set resource limits and enable persistent storage for production deployments.
  • Test alerting quickly with a simple rule before relying on community rules.
  • Change default credentials immediately after the first login.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.