คู่มือฉบับสมบูรณ์สำหรับการติดตั้งและตั้งค่า Monitoring Stack
คู่มือนี้ครอบคลุมการติดตั้งและตั้งค่า Monitoring Stack ที่ประกอบด้วย Prometheus สำหรับเก็บข้อมูล metrics, Grafana สำหรับแสดงผลใน Dashboard ที่สวยงาม, Alertmanager สำหรับระบบแจ้งเตือน และ Node Exporter สำหรับเก็บ metrics ของระบบปฏิบัติการ ทั้งหมดใช้ Docker Compose ในการจัดการ
version: '3.8'
services:
# Prometheus - Metrics Collection
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: unless-stopped
ports:
- "9090:9090"
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus/alert.rules.yml:/etc/prometheus/alert.rules.yml
- prometheus-data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.enable-lifecycle'
# Grafana - Visualization
grafana:
image: grafana/grafana:latest
container_name: grafana
restart: unless-stopped
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
- GF_USERS_ALLOW_SIGN_UP=false
volumes:
- grafana-data:/var/lib/grafana
depends_on:
- prometheus
# Node Exporter - System Metrics
node-exporter:
image: prom/node-exporter:latest
container_name: node-exporter
restart: unless-stopped
ports:
- "9100:9100"
command:
- '--path.procfs=/host/proc'
- '--path.rootfs=/rootfs'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
# Alertmanager - Alert Management
alertmanager:
image: prom/alertmanager:latest
container_name: alertmanager
restart: unless-stopped
ports:
- "9093:9093"
volumes:
- ./alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml
- alertmanager-data:/alertmanager
command:
- '--config.file=/etc/alertmanager/alertmanager.yml'
volumes:
prometheus-data:
grafana-data:
alertmanager-data:
restart: unless-stopped คือ restart policy ที่แนะนำadmin / adminglobal:
scrape_interval: 15s
evaluation_interval: 15s
external_labels:
monitor: 'docker-monitor'
# Alert rules configuration
rule_files:
- '/etc/prometheus/alert.rules.yml'
# Scrape configurations
scrape_configs:
# Prometheus self-monitoring
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# Node Exporter - System Metrics
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
labels:
instance: 'docker-host'
# Alertmanager
- job_name: 'alertmanager'
static_configs:
- targets: ['alertmanager:9093']
groups:
- name: system_alerts
interval: 30s
rules:
# High CPU Usage Alert
- alert: HighCPUUsage
expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 5m
labels:
severity: warning
annotations:
summary: "High CPU Usage Detected"
description: "CPU usage on {{ $labels.instance }} is above 80% (current: {{ $value }}%)"
# High Memory Usage Alert
- alert: HighMemoryUsage
expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "High Memory Usage Detected"
description: "Memory usage on {{ $labels.instance }} is above 80% (current: {{ $value }}%)"
# Disk Space Alert
- alert: DiskSpaceLow
expr: (1 - (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"})) * 100 > 85
for: 5m
labels:
severity: critical
annotations:
summary: "Low Disk Space"
description: "Disk space on {{ $labels.instance }} (mount: {{ $labels.mountpoint }}) is above 85% (current: {{ $value }}%)"
# System Down Alert
- alert: InstanceDown
expr: up == 0
for: 2m
labels:
severity: critical
annotations:
summary: "Instance Down"
description: "{{ $labels.instance }} has been down for more than 2 minutes"
docker compose up -d
docker compose ps
ผลลัพธ์ควรแสดงทั้ง 4 services อยู่ในสถานะ Up
# ดู logs ทั้งหมด docker compose logs -f # ดู logs เฉพาะ service docker compose logs -f prometheus
Settings → Data sourcesAdd data sourcePrometheushttp://prometheus:9090http://localhost:9090Save & Testคลิก + ที่มุมซ้ายบน → Import
กรอก Dashboard ID (เช่น 1860) หรืออัปโหลด JSON file
เลือก Prometheus เป็น Data Source
คลิก Import
global:
resolve_timeout: 5m
# SMTP configuration for email alerts
smtp_smarthost: 'smtp.gmail.com:587'
smtp_from: 'your-email@gmail.com'
smtp_auth_username: 'your-email@gmail.com'
smtp_auth_password: 'your-app-password'
# Route configuration
route:
group_by: ['alertname', 'severity']
group_wait: 10s
group_interval: 10s
repeat_interval: 12h
receiver: 'default-receiver'
routes:
- match:
severity: critical
receiver: 'critical-receiver'
- match:
severity: warning
receiver: 'warning-receiver'
# Receivers
receivers:
- name: 'default-receiver'
email_configs:
- to: 'admin@example.com'
from: 'monitoring@example.com'
- name: 'critical-receiver'
email_configs:
- to: 'admin@example.com,support@example.com'
from: 'alerts@example.com'
headers:
subject: '🚨 CRITICAL: {{ .GroupLabels.alertname }}'
- name: 'warning-receiver'
email_configs:
- to: 'admin@example.com'
from: 'alerts@example.com'
headers:
subject: '⚠️ WARNING: {{ .GroupLabels.alertname }}'
# Inhibition rules
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'instance']
Prometheus จะส่ง alerts ไปยัง Alertmanager โดยอัตโนมัติ เนื่องจากไฟล์ prometheus.yml
มีการตั้งค่า rule_files และ scrape_configs สำหรับ Alertmanager เรียบร้อยแล้ว
localhost:9090
ตรวจสอบ Targets
localhost:3000
ดู Dashboard
localhost:9093
ตรวจสอบ Alerts
UPเพื่อทดสอบว่า Alertmanager ทำงานได้ สามารถทำดังนี้:
ใช้ Docker volumes สำหรับรักษา config และ data ของ Prometheus, Grafana, Alertmanager เพื่อป้องกันการสูญหายเมื่อคอนเทนเนอร์ถูกลบหรือรีสตาร์ท
ใช้ NGINX หรือ Caddy เป็น reverse proxy เพื่อเปิด HTTPS และปกป้อง Grafana และ Dashboard ด้วย Authentication เพิ่มเติม
เพิ่ม blackbox_exporter สำหรับตรวจสอบ HTTP endpoints, ICMP ping, DNS และ TCP ports จากภายนอก
แยก alert routes ตาม severity (critical, warning, info) และกำหนด receivers ที่แตกต่างกันสำหรับแต่ละกลุ่ม
สำหรับระบบขนาดใหญ่ สามารถใช้ Prometheus Federation เพื่อรวมข้อมูลจากหลายๆ instance
# บน Prometheus หลัก (Central)
scrape_configs:
- job_name: 'federate'
honor_labels: true
metrics_path: '/federate'
params:
'match[]':
- '{job="prometheus"}'
- '{__name__=~"job:.*"}'
static_configs:
- targets:
- 'prometheus-instance-1:9090'
- 'prometheus-instance-2:9090'
ส่ง metrics จากหลาย instance ไปยัง Prometheus server กลางผ่าน remote_write
# บน Prometheus instance ย่อย
remote_write:
- url: 'http://central-prometheus:9090/api/v1/write'
basic_auth:
username: 'username'
password: 'password'
คำสั่งเหล่านี้จะลบคอนเทนเนอร์และ data ทั้งหมด ให้แน่ใจว่าได้ backup ข้อมูลสำคัญก่อน
docker compose down
docker compose down --volumes
docker volume rm wiki_prometheus-data wiki_grafana-data wiki_alertmanager-data