🗂️ VPS › ติดตั้งระบบ Monitor VPS  |  อัพเดต: 9 พ.ย. 2569

ติดตั้งระบบ Monitor VPS ด้วย Netdata หรือ Prometheus

เมื่อ VPS รันงานจริง คุณต้องรู้ว่า CPU ใช้เท่าไหร่ RAM เหลือเท่าไหร่ Disk กำลังจะเต็มไหม — ก่อนที่ปัญหาจะเกิดขึ้น บทความนี้จะพาคุณติดตั้ง Netdata (ง่าย เร็ว) และ Prometheus + Grafana (ยืดหยุ่น ทรงพลัง) บน VPS Ubuntu ทีละขั้นตอน

ติดตั้งระบบ Monitor VPS ด้วย Netdata หรือ Prometheus

📋 สารบัญ

  1. ทำไมต้อง Monitor VPS
  2. เปรียบเทียบ Netdata vs Prometheus
  3. ติดตั้ง Netdata บน Ubuntu
  4. ตั้งค่า Netdata (Alert + Secure)
  5. ติดตั้ง Prometheus + Node Exporter
  6. ติดตั้ง Grafana และเชื่อมกับ Prometheus
  7. Metric สำคัญที่ควรดู
  8. ตั้งค่า Alert แจ้งเตือน
  9. ป้องกัน Dashboard ไม่ให้เปิดสาธารณะ
  10. คำถามที่พบบ่อย

ทำไมต้อง Monitor VPS

VPS ที่ไม่มี monitoring เปรียบเหมือนรถที่ไม่มีแผงหน้าปัด คุณไม่รู้ว่าน้ำมันกำลังจะหมด หรือเครื่องกำลังจะพัง จนกว่าจะสายเกินไป

ปัญหาที่มักเกิดกับ VPS ที่ไม่มี monitoring:

💡 ประโยชน์ของ Monitoring ตรวจจับปัญหาล่วงหน้าก่อนเว็บล่ม, วางแผน Scale Up ได้ถูกเวลา, Debug performance issue ได้แม่นยำ

เปรียบเทียบ Netdata vs Prometheus

คุณสมบัติNetdataPrometheus + Grafana
ความง่ายในการติดตั้ง⭐⭐⭐⭐⭐ ง่ายมาก (1 คำสั่ง)⭐⭐⭐ ต้องตั้งค่าหลายขั้นตอน
Real-time Dashboard✅ มี UI พร้อมใช้ทันที❌ ต้อง import dashboard
ความยืดหยุ่นปานกลาง⭐⭐⭐⭐⭐ สูงมาก
Long-term Storageจำกัด (default 24h)✅ เก็บได้ตามต้องการ
Resource ที่ใช้RAM ~50-100 MBRAM ~300-700 MB
Multi-serverNetdata Cloud (ฟรี)✅ รองรับ native
เหมาะกับVPS เดี่ยว, มือใหม่Cluster, Production ขนาดใหญ่
✅ คำแนะนำ มี VPS เดี่ยวหรือไม่กี่เครื่อง → ใช้ Netdata. มีหลายเครื่องและต้องการ custom dashboard + alert ซับซ้อน → ใช้ Prometheus + Grafana

ติดตั้ง Netdata บน Ubuntu

ฟรี Ubuntu 20.04/22.04/24.04

Netdata มี one-line installer ที่ตรวจสอบ OS และติดตั้งอัตโนมัติ:

# อัพเดต package list ก่อน
sudo apt update

# ติดตั้ง Netdata ด้วย one-line installer
wget -O /tmp/netdata-kickstart.sh https://get.netdata.cloud/kickstart.sh
sh /tmp/netdata-kickstart.sh --stable-channel --no-updates

หลังติดตั้งเสร็จ เช็คว่า service รันอยู่:

sudo systemctl status netdata
# ต้องเห็น: active (running)

เปิด Dashboard ที่ http://YOUR_VPS_IP:19999 จะเห็น real-time graphs ทันที

⚠️ ห้ามเปิด Port 19999 สาธารณะ ค่า default เปิด port 19999 ให้ทุก IP เข้าได้ — ต้องปิดหรือ restrict ก่อน (ดูหัวข้อ Security ด้านล่าง)

ติดตั้งด้วย Package Manager (ทางเลือก)

# เพิ่ม Netdata repo
curl -s https://packagecloud.io/install/repositories/netdata/netdata/script.deb.sh | sudo bash

# ติดตั้ง
sudo apt install netdata -y

# เปิดใช้งาน
sudo systemctl enable netdata
sudo systemctl start netdata

ตั้งค่า Netdata (Alert + Secure)

จำกัดการเข้าถึง Dashboard

แก้ไขไฟล์ /etc/netdata/netdata.conf:

[web]
    # อนุญาตเฉพาะ localhost (แล้วใช้ SSH Tunnel เข้าถึง)
    bind to = 127.0.0.1
    # หรือระบุ IP ที่อนุญาต
    allow connections from = localhost 192.168.1.*
sudo systemctl restart netdata

เข้าถึง Dashboard ผ่าน SSH Tunnel

# รันบน local machine
ssh -L 19999:127.0.0.1:19999 user@YOUR_VPS_IP

# แล้วเปิด browser ที่
# http://localhost:19999

ตั้งค่า Alert Email

sudo nano /etc/netdata/health_alarm_notify.conf
# Email
SEND_EMAIL="YES"
DEFAULT_RECIPIENT_EMAIL="[email protected]"
SENDMAIL="$(which sendmail)"

ติดตั้ง Prometheus + Node Exporter

Prometheus ต้องติดตั้ง 2 ส่วน: ตัว Prometheus (เก็บ metric) และ Node Exporter (ดึง metric จาก OS)

ขั้นที่ 1: ติดตั้ง Node Exporter

# ดาวน์โหลด Node Exporter (เช็ค version ล่าสุดที่ github.com/prometheus/node_exporter)
cd /tmp
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar xvf node_exporter-1.8.2.linux-amd64.tar.gz
sudo cp node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/

# สร้าง service user
sudo useradd -rs /bin/false node_exporter

สร้าง systemd service:

sudo nano /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
# เช็ค: curl http://localhost:9100/metrics | head -20

ขั้นที่ 2: ติดตั้ง Prometheus

cd /tmp
wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
tar xvf prometheus-2.54.1.linux-amd64.tar.gz

sudo useradd -rs /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus
sudo cp prometheus-2.54.1.linux-amd64/prometheus /usr/local/bin/
sudo cp prometheus-2.54.1.linux-amd64/promtool /usr/local/bin/
sudo cp -r prometheus-2.54.1.linux-amd64/consoles /etc/prometheus
sudo cp -r prometheus-2.54.1.linux-amd64/console_libraries /etc/prometheus

ตั้งค่า Prometheus:

sudo nano /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

สร้าง systemd service:

sudo nano /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target

[Service]
User=prometheus
Group=prometheus
Type=simple
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

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus
sudo systemctl status prometheus

ติดตั้ง Grafana และเชื่อมกับ Prometheus

ติดตั้ง Grafana

sudo apt install -y apt-transport-https software-properties-common wget

sudo wget -q -O /usr/share/keyrings/grafana.key https://apt.grafana.com/gpg.key
echo "deb [signed-by=/usr/share/keyrings/grafana.key] https://apt.grafana.com stable main" | \
  sudo tee /etc/apt/sources.list.d/grafana.list

sudo apt update
sudo apt install grafana -y

sudo systemctl enable grafana-server
sudo systemctl start grafana-server

เปิด Grafana UI

เข้าถึงผ่าน SSH Tunnel: ssh -L 3000:127.0.0.1:3000 user@VPS_IP แล้วเปิด http://localhost:3000

เพิ่ม Prometheus เป็น Data Source

  1. ไปที่ Connections → Data sources → Add data source
  2. เลือก Prometheus
  3. URL: http://localhost:9090
  4. คลิก Save & test

Import Dashboard สำเร็จรูป

  1. ไปที่ Dashboards → Import
  2. ใส่ ID: 1860 (Node Exporter Full — dashboard ยอดนิยม)
  3. เลือก Prometheus data source ที่สร้างไว้
  4. คลิก Import

คุณจะได้ dashboard สวยงามแสดง CPU, RAM, Disk, Network ทันที

Metric สำคัญที่ควรดู

Metricเกณฑ์ปกติต้องระวัง
CPU Usage< 70%> 90% ต่อเนื่อง 5 นาที
RAM Usage< 80%> 90% (เสี่ยง OOM)
Disk Usage< 70%> 85% (วางแผน clean up)
Disk I/Oขึ้นอยู่กับ workloadWait > 20% ต่อเนื่อง
Network In/Outตามปกติใช้งานspike ผิดปกติ
Load Average< จำนวน CPU> 2× CPU cores
Swap Usage0-20%> 50% (RAM ไม่พอ)

ตั้งค่า Alert แจ้งเตือน

Netdata Alert

Netdata มี alert พร้อมใช้จำนวนมากใน /etc/netdata/health.d/ แก้ threshold ได้ เช่น:

# /etc/netdata/health.d/cpu.conf
alarm: cpu_usage
on: system.cpu
lookup: average -1m percentage of user,system
units: %
warn: $this > 80
crit: $this > 95
info: CPU usage is high

Grafana Alert

  1. เปิด Panel ที่ต้องการ → Edit → Alert
  2. ตั้งเงื่อนไข เช่น CPU > 90% นาน 5 นาที
  3. เพิ่ม Contact Point (Email, Slack, LINE, Telegram)
  4. คลิก Save
✅ Grafana Alert ที่แนะนำ CPU > 90% / RAM > 90% / Disk > 85% / Service Down / High Network Traffic

ป้องกัน Dashboard ไม่ให้เปิดสาธารณะ

Dashboard ไม่ควรเปิดสาธารณะ เพราะมีข้อมูล internal ของเซิร์ฟเวอร์ วิธีป้องกัน:

วิธีที่ 1: ใช้ SSH Tunnel (แนะนำสำหรับ VPS เดี่ยว)

# เข้าถึง Netdata
ssh -L 19999:127.0.0.1:19999 user@VPS_IP &
# เข้าถึง Grafana
ssh -L 3000:127.0.0.1:3000 user@VPS_IP &

วิธีที่ 2: ใช้ Nginx Reverse Proxy + Basic Auth

sudo apt install -y nginx apache2-utils
# สร้าง password file
sudo htpasswd -c /etc/nginx/.htpasswd admin
# /etc/nginx/sites-available/netdata
server {
    listen 80;
    server_name monitor.yourdomain.com;

    auth_basic "Monitoring";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {
        proxy_pass http://127.0.0.1:19999;
        proxy_set_header Host $host;
    }
}

วิธีที่ 3: UFW Firewall Block Port

# บล็อก port monitoring จาก internet
sudo ufw deny 19999
sudo ufw deny 9090
sudo ufw deny 3000
sudo ufw allow from YOUR_IP_ADDRESS to any port 19999

🎯 สรุป: เลือก Monitor ที่ใช่

คำถามที่พบบ่อย

Netdata กับ Prometheus ต่างกันอย่างไร?
Netdata เน้น Real-time monitoring ติดตั้งง่าย มี UI พร้อมใช้ทันที ส่วน Prometheus เน้นเก็บ metric ระยะยาว ยืดหยุ่นสูง แต่ต้องติดตั้ง Grafana แยกสำหรับ visualization
Netdata กินทรัพยากร VPS มากไหม?
Netdata ใช้ RAM ประมาณ 50-100 MB และ CPU น้อยมาก (1-3%) เหมาะกับ VPS ทุกขนาด ตั้งแต่ RAM 1 GB ขึ้นไป
ตั้งค่า Alert แจ้งเตือนบน Netdata ได้ไหม?
ได้ Netdata มี Alert built-in รองรับ Email, Slack, Telegram, PagerDuty และอื่นๆ ตั้งค่าใน /etc/netdata/health.d/ หรือใช้ Netdata Cloud ฟรี
Prometheus + Grafana ต้องการ RAM เท่าไหร่?
Prometheus ใช้ RAM 200-500 MB ขึ้นอยู่กับจำนวน metric ส่วน Grafana ใช้ประมาณ 100-200 MB แนะนำ VPS ที่มี RAM ≥ 2 GB สำหรับ stack นี้
เปิด Port ใดบ้างสำหรับ Netdata และ Prometheus?
Netdata ใช้ Port 19999, Prometheus ใช้ Port 9090, Node Exporter ใช้ Port 9100, Grafana ใช้ Port 3000 ควร restrict ด้วย firewall ไม่เปิดสาธารณะ
ใช้ทั้ง Netdata และ Prometheus พร้อมกันได้ไหม?
ได้ Netdata ส่ง metric ไปยัง Prometheus ได้ผ่าน Exporter built-in ทำให้ใช้ Netdata สำหรับ real-time และ Prometheus สำหรับ long-term storage พร้อมกันได้

บทความที่เกี่ยวข้อง: ตั้งค่า UFW Firewall บน VPS • สร้าง systemd Service • Grafana + Prometheus Monitor VPS