Мы установили Prometheus из официальных бинарных файлов — это самый надёжный способ для production.
Вот пошаговая установка, которую мы выполнили:
# Создаём пользователя
useradd -r -s /usr/sbin/nologin -M prometheus
# Создаём директории
mkdir -p /etc/prometheus /var/lib/prometheus
chown prometheus:prometheus /var/lib/prometheus
# Скачиваем последнюю версию
PROM_VER="2.52.0"
wget https://github.com/prometheus/prometheus/releases/download/v${PROM_VER}/prometheus-${PROM_VER}.linux-amd64.tar.gz
tar xzf prometheus-${PROM_VER}.linux-amd64.tar.gz
cd prometheus-${PROM_VER}.linux-amd64
# Копируем бинарники и конфиги
cp prometheus promtool /usr/local/bin/
cp -r consoles console_libraries /etc/prometheus/
chown -R prometheus:prometheus /etc/prometheus
# Создаём systemd unit
cat > /etc/systemd/system/prometheus.service << 'EOF'
[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus \
--storage.tsdb.retention.time=30d \
--storage.tsdb.retention.size=50GB \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.enable-lifecycle \
--web.enable-admin-api
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now prometheus
systemctl status prometheus
Для 200+ серверов мы использовали file-based service discovery — это позволяет добавлять серверы без перезапуска Prometheus:
# /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_timeout: 10s
# Подключение файлов с правилами алертов
rule_files:
- "/etc/prometheus/rules/*.yml"
# Подключение Alertmanager
alerting:
alertmanagers:
- static_configs:
- targets:
- "localhost:9093"
# Targets для сбора метрик
scrape_configs:
# Метрики самого Prometheus
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# Метрики серверов через node_exporter
- job_name: "nodes"
static_configs:
- targets:
- "10.0.0.10:9100"
- "10.0.0.11:9100"
- "10.0.0.12:9100"
labels:
env: "production"
- targets:
- "10.0.0.20:9100"
labels:
env: "staging"
# Метрики Nginx
- job_name: "nginx"
static_configs:
- targets: ["10.0.0.10:9113"]
# Метрики PostgreSQL
- job_name: "postgresql"
static_configs:
- targets: ["10.0.0.11:9187"]
# File-based service discovery
- job_name: "file_sd"
file_sd_configs:
- files:
- "/etc/prometheus/targets/*.json"
refresh_interval: 30s
Для динамического добавления серверов мы создали JSON-файлы:
# /etc/prometheus/targets/webservers.json
[
{
"targets": ["10.0.0.10:9100", "10.0.0.11:9100"],
"labels": {
"job": "webservers",
"env": "production",
"team": "platform"
}
}
]
Теперь для добавления нового сервера в мониторинг достаточно обновить JSON-файл — Prometheus подхватит изменения автоматически через 30 секунд.