Skip to content
EgyKode
10 · ObservabilityLab 50 / 59
Guided labprometheus

Custom Prometheus Alert Rules & Grafana Dashboards

Write alerts that fire on conditions worth waking someone for, and a dashboard that shows why they fired.

Time
23 min
Level
Advanced
Objectives
4 objectives
Cost
Low cost

Where this fits in the platform

This lab adds

  • Alerts a human can act on, and dashboards kept in git

Before you start

CostLow cost

Depends on an existing cluster. Alert rules and dashboards are configuration and cost nothing; the Prometheus stack under them holds EBS volumes.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

Prometheus is collecting metrics nobody looks at. There are no alerts, so problems are found by users; the one dashboard shows CPU, which has never once explained an outage.

Collecting metrics and being able to answer a question with them are different things.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on your own machine

Run this lab on your own machine. One command starts the environment, with everything the lab needs already installed:

You will need:

  • docker
  • kubectl
  • kind
  • helm
git clone https://github.com/EgyKode/EgyKode-lab.git
cd EgyKode-lab
./egykode start k8s
./egykode shell

You need Docker and Git installed. Everything else runs inside the environment. The first start downloads it and takes a few minutes; later starts are seconds.

Not sure what you already have? Run: npm run doctor — it checks and changes nothing.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

A PrometheusRule

Step 1 of 3

What you are building#

Alerts on symptoms users feel, not on causes you guessed:

text
  RED, for a request-driven service
    Rate       requests per second
    Errors     the proportion that failed
    Duration   how long they took (p95, p99)

High CPU is not an incident. Users cannot feel CPU. They feel errors and latency — so alert on those, and use CPU to explain them once you are already looking.


Build it#

Verify it worked#

Terminal
# The Operator loaded your rules
kubectl get prometheusrule -n monitoring
curl -s localhost:9090/api/v1/rules | jq -r '.data.groups[].name'
 
# The expression returns something. An alert on a metric that does not exist
# is silent forever and looks identical to an alert that is not firing.
curl -sG localhost:9090/api/v1/query \
  --data-urlencode 'query=sum(rate(http_requests_total{namespace="platform"}[5m]))' \
  | jq '.data.result | length'      # must be > 0

Now make one fire — this is the lab.

Terminal
# Cause real restarts
kubectl set image deploy/api api=nginx:does-not-exist -n platform
 
# Watch it move through the states
watch -n5 'curl -s localhost:9090/api/v1/alerts | jq -r ".data.alerts[] | \"\(.labels.alertname) \(.state)\""'
# inactive -> pending (during `for:`) -> firing
 
# It reached Alertmanager
kubectl port-forward -n monitoring svc/monitoring-kube-prometheus-alertmanager 9093:9093
curl -s localhost:9093/api/v2/alerts | jq -r '.[].labels.alertname'
 
# Restore, and confirm it resolves
kubectl rollout undo deploy/api -n platform

An alert that has never fired is a hypothesis. Watching it go pending → firing and then resolve is the only way to know the expression, the for:, the labels and the routing all work together.


Clean up#

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
kubectl delete prometheusrule platform-alerts -n monitoring
kubectl delete configmap -l grafana_dashboard=1 -n monitoring

Cost of this lab: Free on top of the monitoring stack. Rules and dashboards are configuration; the Prometheus and Grafana volumes underneath them are what bills.

Success criteria

0 of 4

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 50 of 59 on the project path

Centralised Logging with Loki and GrafanaShip every Pod's logs somewhere they survive the Pod, then answer a real question with them.50 minIntermediate

Previous: Deploying Kube-Prometheus-Stack on AWS EKS