DEV Community

Cover image for NGINX Monitoring with Prometheus
terngr
terngr

Posted on • Edited on

5

NGINX Monitoring with Prometheus

NGINX เป็นทั้ง Web Server, Load Balancer, Reverse Proxy, Ingress Controller, Caching, Service Mesh, etc. แต่ทุก Use cases มีสิ่งที่เหมือนกันคือ Traffic จะวิ่งผ่าน NGINX ฉะนั้นความสามารถในการ Monitoring ทั้งตัว NGINX และ Traffic ที่วิ่งผ่าน NGINX จะช่วยประเมินสุขภาพของระบบได้ชัดเจน ช่วยดู Utilization รวมถึง Monitor หากเกิดข้อผิดพลาดได้แบบ Real Time

NGINX สามารถที่จะ Expose metrics ของตัวเองออกมาได้ โดยผ่านทาง

/stub_status สำหรับ NGINX Open Source ได้แก่จำนวน Active Connections และ จำนวน Connections ทั้งหมด

/api สำหรับ NGINX Plus จะดู Metrics ได้ละเอียดขึ้นเช่น HTTP Response code, zone status, backend status, etc. see NGINX Plus REST API

Prerequisites: ติดตั้ง NGINX Plus / NGINX OSS พร้อมใช้งาน

ขั้นตอนแรก

เราจะ Configure NGINX ให้ Expose Metrics ออกมา

#NGINX Plus
location /api {
  api;
}
Enter fullscreen mode Exit fullscreen mode
#NGINX OSS
location /basic_status {
  stub_status;
}
Enter fullscreen mode Exit fullscreen mode

ทดลองเรียกดู NGINX Plus Metrics ที่ Path /api
Image description

ขั้นตอนที่สอง

ติดตั้ง Prometheus Exporter เพื่อดึง Metric จาก NGINX ออกไปเป็น Prometheus

Image description

ในขั้นตอนนี้เรารัน Prometheus Exporter บน Container โดยใช้ Image nginx/nginx-prometheus-exporter:0.10.0 และแทนที่ nginx-plus :8080/api ด้วย Address และ Port ที่ Expose metrics จาก NGINX

docker run -p 9113:9113 nginx/nginx-prometheus-exporter:0.10.0 -nginx.plus -nginx.scrape-uri=http://<nginx-plus>:8080/api
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่สาม

ติดตั้ง Prometheus ให้อ่าน Metric จาก Prometheus Exporterในขั้นตอนนี้เรารัน Prometheus บน Container โดยใช้ Image prom/prometheus

Image description

เตรียมไฟล์ prometheus.yml โดยแทนที่ Prometheus Exporter ด้วย Address และ port ที่ได้จาก Prometheus Exporter ในขั้นตอนที่สอง

#prometheus.yml
global:
  scrape_interval: 15s 

  external_labels:
    monitor: 'codelab-monitor'

scrape_configs:  
  - job_name: 'prometheus'

    scrape_interval: 5s

    static_configs:
      - targets: ['Prometheus Exporter:9113']
Enter fullscreen mode Exit fullscreen mode

รัน Prometheus บน Container โดย Mount prometheus.yml และให้เรียกใช้ Prometheus ได้ที่ port 9090

sudo docker run -d -p 9090:9090 -v ~/path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
Enter fullscreen mode Exit fullscreen mode

ทดสอบเรียก Prometheus ในตัวอย่างจะเป็นจำนวนของ connections_accepted

Image description

ตอนต่อไป เรามาดูการนำ Prometheus ไปเป็น Data source ให้กับ Grafana Dashboard เพื่อสร้าง Custom Dashboard ครับ

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay