متقدم
البحث فيالمعامل
طريقة البحث
  • ابحث عن مهمة أو أداة (lvm، selinux، ‎@podman)؛ وصفِّ حسب المستوى والكتاب أو الدورة.
  • مع مربع بحث فارغ، الفلتر وحده يعرض المعامل — مثلًا كل المعامل الصعبة لدورة واحدة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

معمل 7.1أكثر الخدمات استخدامًا, الفصل 7

Monitoring stack in one hour: Prometheus, node exporter, Alertmanager and Grafana

المستوى
متوسط
60 دقيقة
الأجهزة
a Linux VM with Docker and Docker Compose, 2 GB RAM
الحالة
لم يبدأ

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

الهدف

Build the monitoring stack that most teams start with. Prometheus scrapes node exporter, an alert rule fires when a disk is almost full or a target is down, Alertmanager groups and routes the alert, and Grafana shows a dashboard. You break things on purpose to see the alerts arrive.

قبل أن تبدأ

  • Docker with Compose. Make the folder ~/mon-lab and work in it.
  • Ports 9090 (Prometheus), 9093 (Alertmanager), 3000 (Grafana) free on the VM. Open them only to your own IP (or use an SSH tunnel).

المهام

  1. Write compose.yaml with prometheus, node-exporter, alertmanager and grafana (pinned image versions, named volumes for data).
  2. Write prometheus.yml: scrape every 15 s, jobs prometheus and node, rule file alerts.yml, Alertmanager target.
  3. Write alerts.yml with three rules: TargetDown (up == 0 for 1 minute), DiskAlmostFull (less than 15 % free for 5 minutes), HighLoad (load per CPU above 2 for 10 minutes). Give each a severity label and a summary.
  4. Write alertmanager.yml: group by alertname and instance, wait 30 s, repeat every 4 h, send to a webhook receiver (use https://webhook.site or a local nc listener for the lab).
  5. Start the stack and check that all targets are UP and the rules are loaded (use promtool).
  6. Stop node-exporter and watch TargetDown go Pending → Firing → arrive at the webhook. Start it again and see the resolved message.
  7. In Grafana, add Prometheus as a data source and import the dashboard Node Exporter Full (ID 1860).

تحقّق من عملك

docker compose exec prometheus promtool check config /etc/prometheus/prometheus.yml
curl -s localhost:9090/api/v1/targets | python3 -c "import json,sys; [print(t['labels']['job'], t['health']) for t in json.load(sys.stdin)['data']['activeTargets']]"
curl -s localhost:9090/api/v1/rules | python3 -c "import json,sys; print([r['name'] for g in json.load(sys.stdin)['data']['groups'] for r in g['rules']])"
curl -s localhost:9093/api/v2/alerts | python3 -m json.tool | head -20
  • promtool says SUCCESS for the config and the rule file.
  • Both targets are up; the 3 rules are listed.
  • While node-exporter is stopped, Alertmanager lists TargetDown and the webhook got a JSON message with "status": "firing"; later one with "resolved".
  • Grafana shows CPU, memory, disk and network graphs for the VM.

تلميحات

اعرض التلميحات
  • node exporter needs to see the host: mount / read-only at /host and use --path.rootfs=/host, plus pid: host.
  • Free disk in percent: node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes * 100 < 15.
  • Load per CPU: node_load5 / count without(cpu, mode) (node_cpu_seconds_total{mode="idle"}) > 2.
  • docker compose exec prometheus kill -HUP 1 reloads the config after a change (or start Prometheus with --web.enable-lifecycle and curl -X POST localhost:9090/-/reload).

الحل

اعرض الحل
  1. The stack.
    compose.yaml
    services:
      prometheus:
        image: prom/prometheus:v2.55.1
        command: ["--config.file=/etc/prometheus/prometheus.yml", "--storage.tsdb.retention.time=15d", "--web.enable-lifecycle"]
        volumes: ["./prometheus.yml:/etc/prometheus/prometheus.yml:ro", "./alerts.yml:/etc/prometheus/alerts.yml:ro", "prom-data:/prometheus"]
        ports: ["9090:9090"]
      node-exporter:
        image: prom/node-exporter:v1.8.2
        command: ["--path.rootfs=/host"]
        pid: host
        volumes: ["/:/host:ro,rslave"]
      alertmanager:
        image: prom/alertmanager:v0.27.0
        volumes: ["./alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro"]
        ports: ["9093:9093"]
      grafana:
        image: grafana/grafana:11.3.0
        volumes: ["grafana-data:/var/lib/grafana"]
        ports: ["3000:3000"]
    volumes:
      prom-data: {}
      grafana-data: {}
  2. Prometheus, the rules and Alertmanager.
    prometheus.yml
    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    rule_files: [/etc/prometheus/alerts.yml]
    alerting:
      alertmanagers:
        - static_configs: [{targets: ["alertmanager:9093"]}]
    scrape_configs:
      - job_name: prometheus
        static_configs: [{targets: ["localhost:9090"]}]
      - job_name: node
        static_configs: [{targets: ["node-exporter:9100"]}]
    alerts.yml
    groups:
      - name: basics
        rules:
          - alert: TargetDown
            expr: up == 0
            for: 1m
            labels: {severity: critical}
            annotations: {summary: "{{ $labels.job }} on {{ $labels.instance }} is down"}
          - alert: DiskAlmostFull
            expr: node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes * 100 < 15
            for: 5m
            labels: {severity: warning}
            annotations: {summary: "{{ $labels.mountpoint }} has less than 15% free"}
          - alert: HighLoad
            expr: node_load5 / count without(cpu, mode) (node_cpu_seconds_total{mode="idle"}) > 2
            for: 10m
            labels: {severity: warning}
            annotations: {summary: "Load per CPU above 2 on {{ $labels.instance }}"}
    alertmanager.yml
    route:
      receiver: lab-webhook
      group_by: [alertname, instance]
      group_wait: 30s
      group_interval: 5m
      repeat_interval: 4h
    receivers:
      - name: lab-webhook
        webhook_configs:
          - url: https://webhook.site/your-unique-id
            send_resolved: true
  3. Start, check, break.
    docker compose up -d
    docker compose exec prometheus promtool check config /etc/prometheus/prometheus.yml
    docker compose stop node-exporter
    sleep 90; curl -s localhost:9093/api/v2/alerts | grep -o '"alertname":"[A-Za-z]*"'
    "alertname":"TargetDown"
    docker compose start node-exporter
  4. Grafana: open http://VM:3000 (admin / admin, then change the password) → Connections → Data sources → Prometheus, URL http://prometheus:9090 → Dashboards → Import → 1860.

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل