如何在 docker 容器中为 运行 设置 alertmanager.service

How to set up alertmanager.service for running in docker container

我 运行 在 docker 容器中使用 prometheus,我想配置一个 AlertManager 以便在服务关闭时向我发送电子邮件。我创建了 alert_rules.ymlprometheus.yml,并使用以下命令 运行 所有内容,将两个 yml 文件安装到路径 /etc/prometheus 的 docker 容器中:

docker run -d -p 9090:9090 --add-host host.docker.internal:host-gateway -v "$PWD/prometheus.yml":/etc/prometheus/prometheus.yml -v "$PWD/alert_rules.yml":/etc/prometheus/alert_rules.yml prom/prometheus

现在,我也想让prometheus在出现alert的时候给我发邮件,这也是我遇到一些问题的地方。我将 alertmanager.yml 配置如下:

route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: email-me
receivers:
- name: 'gmail'
  email_configs:
  - to: 'my_email@gmail.com'
    from: 'askonlinetraining@gmail.com'
    smarthost: smtp.gmail.com:587
    auth_username: 'my_email@gmail.com'
    auth_identity: 'my_email@gmail.com'
    auth_password: 'the_password'

我实际上不知道 smarthost 参数是否配置正确,因为我找不到任何关于它的文档,我不知道它应该包含哪些值 我还创建了一个 alertmanager.service 文件:

[Unit]
Description=AlertManager Server Service
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=Simple
ExecStart=/usr/local/bin/alertmanager \
    --config.file /etc/alertmanager.yml

[Install]
WantedBy=multi-user.target

我认为这里有些问题:我认为我传递给 ExecStart 的第一个参数是容器中不存在的路径,但我不知道应该如何替换它。 我尝试使用以下命令将最后两个文件安装到 docker 容器中,该容器位于我安装前两个 yml 文件的同一目录中:

docker run -d -p 9090:9090 --add-host host.docker.internal:host-gateway -v "$PWD/prometheus.yml":/etc/prometheus/prometheus.yml -v "$PWD/alert_rules.yml":/etc/prometheus/alert_rules.yml -v "$PWD/alertmanager.yml":/etc/prometheus/alertmanager.yml -v "$PWD/alertmanager.service":/etc/prometheus/alertmanager.service prom/prometheus

但是邮件警报不起作用,我不知道如何修复配置以便顺利地运行将所有这些放入docker容器中。正如我所说,我认为主要问题出在 alertmanager.service 中的 ExecStart 命令,但也许我错了。我在网上找不到任何有用的东西,因此非常感谢您的帮助

容器的最佳实践是运行每个容器一个进程。

在您的容器中,这建议一个容器用于 prom/prometheus,另一个用于 prom/alertmanager

您可以 运行 这些使用 docker 作为:

docker run \
--detach \
--name=prometheus \
--volume=${PWD}:/prometheus.yml:/etc/prometheus/prometheus.yml \
--volume=${PWD}:/rules.yml:/etc/alertmanager/rules.yml \
--publish=9090:9090 \
prom/prometheus:v2.26.0 \
--config.file=/etc/prometheus/promtheus.yml

docker run \
--detach \
--name=alertmanager \
--volume=${PWD}:/rules.yml:/etc/alertmanager/rules.yml \
--publish=9093:9093 \
prom/alertmanager:v0.21.0

当您 运行 多个容器是 Docker Compose 时的好工具,在这种情况下,您的 docker-compose.yml 可能是:

version: "3"

services:
  prometheus:
    restart: always
    image: prom/prometheus:v2.26.0
    container_name: prometheus
    command:
      - --config.file=/etc/prometheus/prometheus.yml
    volumes:
      - ${PWD}/prometheus.yml:/etc/prometheus/prometheus.yml
      - ${PWD}/rules.yml:/etc/alertmanager/rules.yml
    expose:
      - "9090"
    ports:
      - 9090:9090

  alertmanager:
    restart: always
    depends_on:
      - prometheus
    image: prom/alertmanager:v0.21.0
    container_name: alertmanager
    volumes:
      - ${PWD}/alertmanager.yml:/etc/alertmanager/alertmanager.yml
    expose:
      - "9093"
    ports:
      - 9093:9093

你可以:

docker-compose up

无论哪种情况,您都可以浏览:

  • 主机端口 9090 上的 Prometheus 即 localhost:9090
  • 主机端口 9093 上的警报管理器,即 localhost:9093