Monitor Docker Container ด้วย cAdvisor + Prometheus + Grafana

การ Monitor Docker Container เป็นสิ่งสำคัญที่ไม่ควรมองข้าม โดยเฉพาะอย่างยิ่งเมื่อคุณใช้งาน Cloud VPS หรือ Cloud Server ที่รันเป็นจำนวนมาก การดูแลสุขภาพของ Container เป็นกุญแจสำคัญในการรักษาประสิทธิภาพและความเสถียรภาพของระบบ บทความนี้จะแนะนำวิธีการ Monitor Docker Container ด้วยการผสมผสาน cAdvisor, Prometheus, และ Grafana ซึ่งเป็นเครื่องมือที่ทรงพลังและเป็นที่นิยมในวงการ DevOps

ทำไมต้อง Monitor Docker Container และ Metrics ที่สำคัญ

การ Monitor Container มีความสำคัญหลายประการ ได้แก่:

  • ป้องกันปัญหา – การติดตามสถานการณ์แบบเรียลไทม์ช่วยให้คุณตรวจพบปัญหาก่อนที่จะส่งผลกระทบต่อผู้ใช้
  • เพิ่มประสิทธิภาพ – ข้อมูล Metrics ช่วยให้คุณระบุจุดคอขวดและปรับปรุงการใช้ทรัพยากร
  • ลดค่าใช้จ่าย – การควบคุมการใช้ทรัพยากรอย่างเหมาะสมช่วยลดค่า Infrastructure
  • การแก้ไขปัญหา – ข้อมูล Logs และ Metrics ช่วยให้ Debugging เร็วขึ้น

Metrics ที่สำคัญที่สุด

  • CPU Usage – เปอร์เซ็นต์การใช้ CPU ของ Container
  • Memory Usage – ปริมาณหน่วยความจำที่ใช้ (Bytes) และเปอร์เซ็นต์จากทั้งหมด
  • Network I/O – ปริมาณข้อมูลที่ส่ง (TX) และรับ (RX)
  • Disk I/O – การอ่านและเขียนลงดิสก์
  • Container State – สถานะการทำงาน (Running, Exited, Paused)

cAdvisor คืออะไร

cAdvisor ย่อมาจาก “Container Advisor” เป็น Open Source Tool ที่พัฒนาโดย Google สำหรับการเก็บรวบรวม ประมวลผล และส่งออก Metrics จาก Container ในเครื่อง cAdvisor ทำงานโดยการเชื่อมต่อกับ Docker Daemon ผ่าน Docker Socket และอ่าน Metrics จาก cgroups (Linux kernel feature) ส่งไปยัง Prometheus เพื่อจัดเก็บต่อไป

ติดตั้ง cAdvisor ด้วย Docker

รัน cAdvisor Container

docker run \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:ro \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --volume=/dev/disk/:/dev/disk:ro \
  --publish=8080:8080 \
  --detach=true \
  --name=cadvisor \
  google/cadvisor:latest
  • --volume=/:/rootfs:ro – Mount root filesystem เป็น read-only
  • --volume=/var/run:/var/run:ro – Mount /var/run สำหรับ Docker Socket
  • --volume=/sys:/sys:ro – Mount /sys สำหรับ cgroups
  • --publish=8080:8080 – เปิดให้ใช้งาน Port 8080

หลังจาก Container เริ่มทำงาน คุณสามารถเข้าถึง cAdvisor ได้ที่ http://localhost:8080

ตั้งค่า Prometheus ให้อ่าน Metrics จาก cAdvisor

global:
  scrape_interval: 15s
  evaluation_interval: 15s

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

  - job_name: 'cadvisor'
    static_configs:
      - targets: ['localhost:8080']
    metrics_path: '/metrics'
    scrape_interval: 5s

PromQL Queries สำหรับ Docker Container Metrics

CPU Usage ของ Container

rate(container_cpu_usage_seconds_total[1m])

Memory Usage (หน่วย MB)

container_memory_usage_bytes{name!=""} / 1024 / 1024

Network Bytes Sent/Received

rate(container_network_transmit_bytes_total[1m])
rate(container_network_receive_bytes_total[1m])

Memory Usage Percentage

(container_memory_usage_bytes{name!=""} / container_spec_memory_limit_bytes{name!=""}) * 100

Import Grafana Dashboard สำหรับ Docker Monitoring

Grafana มี Pre-built Dashboard ที่สามารถ Import ได้ทันที

  • Docker Container Monitoring – Dashboard ID: 11600
  • Docker Container Stats – Dashboard ID: 10619
  • cAdvisor Exporter – Dashboard ID: 14981

วิธี Import: เข้า Grafana → Dashboards → Import → ใส่ Dashboard ID → เลือก Prometheus Data Source → Import

Docker Compose สำหรับ cAdvisor + Prometheus + Grafana

version: '3.8'

services:
  cadvisor:
    image: google/cadvisor:latest
    container_name: cadvisor
    ports:
      - "8080:8080"
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro
    networks:
      - monitoring
    restart: unless-stopped

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    networks:
      - monitoring
    restart: unless-stopped
    depends_on:
      - cadvisor

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana_data:/var/lib/grafana
    networks:
      - monitoring
    restart: unless-stopped
    depends_on:
      - prometheus

networks:
  monitoring:
    driver: bridge

volumes:
  prometheus_data:
  grafana_data:

รันด้วย docker-compose up -d แล้วเข้า Grafana ที่ http://localhost:3000

ตารางแสดง Container Metrics และ PromQL Queries

Metric คำอธิบาย PromQL Query Unit
CPU Usage Rate อัตราการใช้ CPU ในช่วงเวลา 5 นาที rate(container_cpu_usage_seconds_total[5m]) Cores
Memory Usage หน่วยความจำที่ Container ใช้อยู่ container_memory_usage_bytes{name!=""} Bytes
Memory Limit ขีดจำกัดหน่วยความจำของ Container container_spec_memory_limit_bytes{name!=""} Bytes
Network RX Bytes ปริมาณข้อมูลที่รับผ่านเครือข่าย rate(container_network_receive_bytes_total[1m]) Bytes/sec
Network TX Bytes ปริมาณข้อมูลที่ส่งผ่านเครือข่าย rate(container_network_transmit_bytes_total[1m]) Bytes/sec
Memory % เปอร์เซ็นต์การใช้หน่วยความจำ (container_memory_usage_bytes{name!=""} / container_spec_memory_limit_bytes{name!=""}) * 100 %

ตั้งค่า Alert สำหรับ Container Resources

groups:
  - name: docker_alerts
    interval: 30s
    rules:
      - alert: HighCPUUsage
        expr: (rate(container_cpu_usage_seconds_total[5m])) > 0.8
        for: 5m
        annotations:
          summary: "Container {{ $labels.name }} has high CPU usage"
          description: "CPU usage is {{ $value | humanizePercentage }}"
      - alert: HighMemoryUsage
        expr: ((container_memory_usage_bytes{name!=""} / container_spec_memory_limit_bytes{name!=""}) * 100) > 80
        for: 5m
        annotations:
          summary: "Container {{ $labels.name }} has high memory usage"
          description: "Memory usage is {{ $value | humanizePercentage }}"

Best Practices สำหรับ Monitoring Docker

  • ตั้ง Resource Limits – กำหนดขีดจำกัด CPU และ Memory สำหรับแต่ละ Container
  • Keep Metrics Retention – กำหนดระยะเวลาเก็บข้อมูล Metrics ให้เหมาะสม (15-30 วัน)
  • Optimize Scrape Interval – สำหรับ Production ควรใช้ 15-30 วินาที เพื่อลดภาระ Prometheus
  • ตั้ง Alert Rules อย่างเหมาะสม – ตั้ง Threshold ให้มีเหตุผล เพื่อลดการแจ้งเตือนเท็จ
  • Backup Configuration – Backup prometheus.yml และ Grafana Dashboard

ประยุกต์ใช้กับ Dot Enterprise Cloud VPS

หากคุณใช้งาน Dot Enterprise Cloud VPS หรือ Cloud Hosting Services การตั้งค่า Monitoring Stack นี้จะเป็นประโยชน์อย่างมากในการ:

  • ติดตามสถานะของ Docker Container ที่รันบน VPS ของคุณ
  • ควบคุมการใช้ทรัพยากร (CPU, Memory) เพื่อให้ไม่เกินข้อจำกัดของแพลน
  • สร้าง Alert และ Notification เพื่อการตอบสนองอย่างรวดเร็ว
  • วิเคราะห์ Performance ของแอปพลิเคชัน โดยเฉพาะในช่วง Peak Time

สรุป

การ Monitor Docker Container ด้วย cAdvisor + Prometheus + Grafana เป็นการลงทุนที่คุ้มค่าสำหรับทุกๆ สภาพแวดล้อม DevOps สามารถติดตามสถานการณ์เรียลไทม์ ตั้ง Alert อัตโนมัติ และวิเคราะห์ข้อมูลด้วย Dashboard ที่สวยงาม การตั้งค่า Docker Compose ที่เราแนะนำช่วยให้ทุกอย่างง่ายขึ้นและสามารถนำมาใช้ได้ทั่วไป ด้วยการใช้ Dot Enterprise Cloud VPS ร่วมกับ Monitoring Stack นี้ คุณจะได้ความสามารถในการจัดการ Infrastructure ที่มีประสิทธิภาพและเชื่อถือได้