0
0
Kubernetesdevops~5 mins

Alerting with Prometheus Alertmanager in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When your applications or servers have problems, you want to know quickly so you can fix them. Prometheus Alertmanager helps by sending alerts when something goes wrong based on rules you set.
When you want to get notified if your server CPU is too high for a long time.
When your application is not responding and you want to receive an alert.
When disk space is running low and you need to clean up before it causes issues.
When a Kubernetes pod crashes repeatedly and you want to investigate.
When you want to send alerts to email or chat apps automatically.
Config File - alertmanager.yml
alertmanager.yml
global:
  resolve_timeout: 5m

route:
  receiver: 'team-email'
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h

receivers:
- name: 'team-email'
  email_configs:
  - to: 'team@example.com'
    from: 'alertmanager@example.com'
    smarthost: 'smtp.example.com:587'
    auth_username: 'alertmanager@example.com'
    auth_password: 'securepassword'
    require_tls: true

inhibit_rules:
- source_match:
    severity: 'critical'
  target_match:
    severity: 'warning'
  equal: ['alertname', 'dev', 'instance']

global: Sets general settings like how long to wait before marking alerts as resolved.

route: Defines how alerts are grouped and sent to receivers.

receivers: Lists where alerts go, here an email address with SMTP settings.

inhibit_rules: Prevents sending less important alerts if a critical alert is already firing for the same issue.

Commands
This command applies the Alertmanager configuration to the monitoring namespace in Kubernetes so Alertmanager knows how to send alerts.
Terminal
kubectl apply -f alertmanager.yml -n monitoring
Expected OutputExpected
configmap/alertmanager configured
-f - Specifies the configuration file to apply
-n - Specifies the Kubernetes namespace
Check that the Alertmanager pod is running properly after applying the configuration.
Terminal
kubectl get pods -n monitoring
Expected OutputExpected
NAME READY STATUS RESTARTS AGE alertmanager-main-0 2/2 Running 0 3m
-n - Shows pods in the monitoring namespace
View the logs of the Alertmanager pod to verify it started correctly and is using the new config.
Terminal
kubectl logs alertmanager-main-0 -n monitoring
Expected OutputExpected
level=info ts=2024-06-01T12:00:00.000Z caller=main.go:123 msg="Starting Alertmanager" version="0.25.0" level=info ts=2024-06-01T12:00:01.000Z caller=main.go:456 msg="Loading configuration file" file=alertmanager.yml level=info ts=2024-06-01T12:00:01.500Z caller=main.go:789 msg="Listening on :9093"
Key Concept

If you remember nothing else from this pattern, remember: Alertmanager sends notifications based on rules you configure to help you fix problems fast.

Common Mistakes
Not applying the Alertmanager config in the correct Kubernetes namespace.
Alertmanager runs in a specific namespace, so applying config elsewhere means it won't pick up your settings.
Always use the correct namespace with -n monitoring when applying or checking Alertmanager resources.
Forgetting to configure receivers like email or chat in alertmanager.yml.
Without receivers, Alertmanager has nowhere to send alerts, so you won't get notified.
Add at least one receiver with proper settings like email_configs or webhook_configs.
Not checking Alertmanager pod logs after applying config.
Errors in the config file can prevent Alertmanager from starting or sending alerts.
Always check logs with kubectl logs to confirm Alertmanager loaded the config without errors.
Summary
Apply the Alertmanager configuration file to set alert rules and receivers.
Verify the Alertmanager pod is running in the monitoring namespace.
Check Alertmanager logs to ensure the configuration loaded correctly and it is ready to send alerts.