متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 17Prometheus · Monitoring

Install and set up Prometheus and Node Exporter

Prometheus is the standard for metrics in DevOps: it collects numbers (CPU, memory, disk, requests, errors) from your servers and apps every few seconds, keeps them as time series, and fires alerts when a rule is true. This guide installs Prometheus and Node Exporter from the official release files with systemd on RHEL and Ubuntu, writes the first config and alert rules, and shows the PromQL queries you use every day.

  • 9 الدروس
  • 13 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

17.1

Know it before you install

Prometheus pulls (scrapes) metrics over HTTP from targets — a /metrics page on each server or app. Exporters make that page for things that do not have one (Node Exporter for Linux servers). You ask questions with PromQL.

ItemPrometheusNode Exporter
Files/usr/local/bin/prometheus, promtool/usr/local/bin/node_exporter
Config/etc/prometheus/prometheus.ymlflags in the service file
Data/var/lib/prometheus/—
Port9090/tcp (web UI and API)9100/tcp (/metrics)
Serviceprometheus.service (you write it)node_exporter.service
Userprometheus (system user)node_exporter (system user)

ملاحظة

Ubuntu has packages (apt install prometheus prometheus-node-exporter) — quick, but often old. The official release files below give the current version on every distribution. In Kubernetes you install the whole stack with the Helm chart kube-prometheus-stack instead.

17.2

Install Prometheus (RHEL and Ubuntu)

Choose the version from github.com/prometheus/prometheus/releases (here 3.5.0, a long-term support version). Make a system user and the folders:

useradd --system --no-create-home --shell /sbin/nologin prometheus
mkdir -p /etc/prometheus /var/lib/prometheus
VER=3.5.0
curl -fsSLO https://github.com/prometheus/prometheus/releases/download/v$VER/prometheus-$VER.linux-amd64.tar.gz
tar -xzf prometheus-$VER.linux-amd64.tar.gz
install -m 0755 prometheus-$VER.linux-amd64/prometheus prometheus-$VER.linux-amd64/promtool /usr/local/bin/
chown -R prometheus: /etc/prometheus /var/lib/prometheus
prometheus --version

Write the service file:

/etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
 
[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus \
  --storage.tsdb.retention.time=30d \
  --web.listen-address=127.0.0.1:9090
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

17.3

Install Node Exporter on every server

useradd --system --no-create-home --shell /sbin/nologin node_exporter
NVER=1.9.1
curl -fsSLO https://github.com/prometheus/node_exporter/releases/download/v$NVER/node_exporter-$NVER.linux-amd64.tar.gz
tar -xzf node_exporter-$NVER.linux-amd64.tar.gz
install -m 0755 node_exporter-$NVER.linux-amd64/node_exporter /usr/local/bin/
/etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network-online.target
 
[Service]
User=node_exporter
ExecStart=/usr/local/bin/node_exporter --collector.systemd
Restart=on-failure
 
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now node_exporter
curl -s http://localhost:9100/metrics | grep ^node_load1
node_load1 0.12

Open port 9100 only for the Prometheus server:

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.5/32" port port="9100" protocol="tcp" accept'
firewall-cmd --reload
ufw allow from 10.0.0.5 to any port 9100 proto tcp

17.4

First configuration: targets and alert rules

/etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s
 
rule_files:
  - /etc/prometheus/rules/*.yml
 
alerting:
  alertmanagers:
    - static_configs:
        - targets: ["127.0.0.1:9093"]
 
scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["127.0.0.1:9090"]
 
  - job_name: node
    static_configs:
      - targets: ["web1:9100", "web2:9100", "db1:9100"]
        labels:
          env: prod

The first alert rules every team needs — server down, disk almost full, high CPU:

/etc/prometheus/rules/node.yml
groups:
  - name: node
    rules:
      - alert: InstanceDown
        expr: up == 0
        for: 2m
        labels: { severity: critical }
        annotations:
          summary: "{{ $labels.instance }} is down"
      - alert: DiskAlmostFull
        expr: (node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes) < 0.10
        for: 10m
        labels: { severity: warning }
        annotations:
          summary: "Less than 10% disk free on {{ $labels.instance }} {{ $labels.mountpoint }}"
      - alert: HighCPU
        expr: 100 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100 > 90
        for: 15m
        labels: { severity: warning }

Check, start and open the UI:

mkdir -p /etc/prometheus/rules && chown -R prometheus: /etc/prometheus
promtool check config /etc/prometheus/prometheus.yml
SUCCESS: /etc/prometheus/prometheus.yml is valid prometheus config file syntax
promtool check rules /etc/prometheus/rules/node.yml
systemctl daemon-reload
systemctl enable --now prometheus
systemctl reload prometheus
curl -s http://127.0.0.1:9090/-/ready

Prometheus listens only on 127.0.0.1:9090. Look at it through an SSH tunnel (ssh -L 9090:127.0.0.1:9090 mon1) or put Nginx with a login in front. Status → Targets must show every target UP.

17.5

Daily use: PromQL and the real uses

Queries you use every day (Graph page or Grafana):

PromQL
up
up == 0
100 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
node_filesystem_avail_bytes{mountpoint="/"} / 1e9
rate(node_network_receive_bytes_total{device="eth0"}[5m])
predict_linear(node_filesystem_avail_bytes{mountpoint="/"}[6h], 24*3600) < 0
sum by (status) (rate(http_requests_total[5m]))

Real uses at work:

  • Dashboards in Grafana; alerts to Slack, Teams, email or PagerDuty through Alertmanager.
  • Capacity planning: predict_linear says when a disk will be full.
  • App metrics (requests, errors, latency) from client libraries — the base of SLOs.

17.6

Make it safe

Security checklist:

  • Prometheus and exporters have no login by default: listen on localhost or a private network, firewall the ports.
  • For access from outside, use Nginx with TLS and a login, or --web.config.file with TLS and basic auth.
  • Do not put secrets in labels or metric names.
  • Keep --web.enable-admin-api and --web.enable-lifecycle off unless you need them.
  • Watch Prometheus itself: disk usage of /var/lib/prometheus, the up of all jobs.

17.7

Update and remove

Download the new release, stop the service, replace the binary, start it. Check the config first with the new promtool:

systemctl stop prometheus
install -m 0755 prometheus-3.5.1.linux-amd64/prometheus prometheus-3.5.1.linux-amd64/promtool /usr/local/bin/
systemctl start prometheus
systemctl disable --now prometheus node_exporter
rm -rf /usr/local/bin/{prometheus,promtool,node_exporter} /etc/prometheus /var/lib/prometheus /etc/systemd/system/{prometheus,node_exporter}.service

17.8

Fix common problems

ProblemCauseFix
Target DOWN: connection refusedExporter not running or wrong port.systemctl status node_exporter; curl HOST:9100/metrics.
Target DOWN: context deadline exceededFirewall drops the scrape.Open 9100 for the Prometheus IP.
Service does not start: permission denied on /var/lib/prometheusWrong owner.chown -R prometheus: /var/lib/prometheus /etc/prometheus.
error loading configYAML indentation or a typo.promtool check config; fix the line.
Alert never firesWrong expression, or for: time not reached.Test the expr on the Graph page; look at Alerts → Pending.
Disk fills upRetention too long or too many series.Lower retention.time; add --storage.tsdb.retention.size=50GB; remove high-cardinality labels.
$labels text shows empty in alertsWrong quoting in YAML.Put the summary in double quotes as above.

17.9

Next steps

1Does Prometheus push metrics to a server or pull them from targets?

It pulls (scrapes) them over HTTP from each target's /metrics page.

2Which query shows all targets that are down?

up == 0.

كل الأوامر في هذا الفصل