我在 docker 容器中運行 prometheus,我想配置一個 AlertManager,讓它在服務關閉時向我發送電子郵件。我創建了 alert_rules.yml和prometheus.yml,并使用以下命令運行了所有內容,將兩個 yml 檔案安裝到路徑上的 docker 容器上/etc/prometheus:
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 在出現警報時向我發送電子郵件,這就是我遇到一些問題的地方。我的配置alertmanager.yml如下:
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: email-me
receivers:
- name: 'gmail'
email_configs:
- to: '[email protected]'
from: '[email protected]'
smarthost: smtp.gmail.com:587
auth_username: '[email protected]'
auth_identity: '[email protected]'
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 的第一個引數是容器中不存在的路徑,但我不知道應該如何替換它。我嘗試使用以下命令將最后兩個檔案掛載到與掛載前兩個 yml 檔案相同的目錄中的 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 -v "$PWD/alertmanager.yml":/etc/prometheus/alertmanager.yml -v "$PWD/alertmanager.service":/etc/prometheus/alertmanager.service prom/prometheus
但是郵件警報不起作用,我不知道如何修復配置以將所有這些順利運行到 docker 容器中。正如我所說,我認為主要問題在于 中ExecStart存在的命令alertmanager.service,但也許我錯了。我在網上找不到任何有用的東西,因此我非常感謝您的幫助
uj5u.com熱心網友回復:
容器的最佳實踐是針對每個容器運行一個行程。
在您的容器中,這建議一個容器用于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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383904.html
