Hacking your way to Observability — Part 2 : Alerts

Taking advantage of metrics by sending notifications via Slack

Alert rules

To create an alert rule using the Prometheus Operator, we will use the PrometheusRule custom resource. PrometheusRule requires you to specify the following:

Prometheus MysqlDown Alert
kubectl scale deployment/mysql --replicas=0 -n applications
Prometheus MysqlDown Alert firing

Preparing Slack

Since we will be sending all our alerts to Slack, we need to do some preparation first. Let’s start by creating a Slack channel.

Create channel prompt in Slack
Enabling Incoming Webhooks on Slack

Configuring AlertManager

To configure Alert Manager, we need to create a custom resource called AlertmanagerConfig. It requires us to configure at least one receiver, which is the platform we will send the message to, and a route to any of the receivers.

  • The groupBy parameter list the labels that Alert Manager will use to group the alerts in a single notification.
  • groupWait indicates how long to wait before sending the initial notification.
  • groupInterval indicates how long to wait before sending a notification update.
  • repeatInterval indicates how long to wait before sending the last notification again.
  • Slack configuration requires the webhook URL to be presented as a secret; that secret will be referenced in the apiURL parameter.
  • The channel parameter specifies the slack channel that will be used to receive notifications.
  • SendResolved is a parameter that indicates Alert Manager to send a notification when the condition for the alert is not met anymore.
  • Title and Text are parameters to modify the format of the slack message. Both can include a reference to an existing template.
AlertmanagerConfig sample
Routing Tree Editor

Templates

Prometheus supports defining templates for them to be used in the notifications. Templates allow us to standardize a notification message for all our alerts.

Prometheus Template example
helm upgrade --reuse-values prometheus prometheus-community/kube-prometheus-stack -n observability -f values.yml

Testing Notifications

To receive the Slack notifications we should trigger the alerts. Let’s start by scaling down MySQL to receive a single alert notification:

Prometheus Notifications on Slack
kubectl scale deployment/format-service-depl --replicas=1 -n applications
kubectl scale deployment/hello-service-depl --replicas=1 -n applications
kubectl scale deployment/people-service-depl --replicas=1 -n applications
kubectl scale deployment/format-service-depl --replicas=2 -n applications
kubectl scale deployment/hello-service-depl --replicas=2 -n applications
kubectl scale deployment/people-service-depl --replicas=2 -n applications
Prometheus grouped alerts on Slack
Alerts with the same alertname

Conclusion

Notifications are very powerful to communicate to your team what’s is happening with your systems; take advantage of the templates to show more accurate messages; providing precise information helps solve issues faster. Don’t forget to group your alerts to avoid a notification hell!

--

--

Integration Consultant | Technology Enthusiast | Problem Solver | Always willing to learn something new

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jonathan Burmester

Integration Consultant | Technology Enthusiast | Problem Solver | Always willing to learn something new