هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
الهدف
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-laband 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).
المهام
ضع علامة على المهمة عندما تنتهي منها. متصفحك فقط يتذكر ذلك.
- Write
compose.yamlwithprometheus,node-exporter,alertmanagerandgrafana(pinned image versions, named volumes for data). - Write
prometheus.yml: scrape every 15 s, jobsprometheusandnode, rule filealerts.yml, Alertmanager target. - Write
alerts.ymlwith three rules:TargetDown(up == 0for 1 minute),DiskAlmostFull(less than 15 % free for 5 minutes),HighLoad(load per CPU above 2 for 10 minutes). Give each aseveritylabel and a summary. - Write
alertmanager.yml: group byalertnameandinstance, wait 30 s, repeat every 4 h, send to a webhook receiver (use https://webhook.site or a localnclistener for the lab). - Start the stack and check that all targets are UP and the rules are loaded (use
promtool). - Stop
node-exporterand watchTargetDowngo Pending → Firing → arrive at the webhook. Start it again and see the resolved message. - 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 -20promtoolsays SUCCESS for the config and the rule file.- Both targets are
up; the 3 rules are listed. - While node-exporter is stopped, Alertmanager lists
TargetDownand 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/hostand use--path.rootfs=/host, pluspid: 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 1reloads the config after a change (or start Prometheus with--web.enable-lifecycleandcurl -X POST localhost:9090/-/reload).
الحل
اعرض الحل
- 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: {} - 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.ymlgroups: - 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.ymlroute: 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 - Start, check, break.
- Grafana: open
http://VM:3000(admin / admin, then change the password) → Connections → Data sources → Prometheus, URLhttp://prometheus:9090→ Dashboards → Import →1860.
ملاحظاتي
لا توجد ملاحظات بعد.