Giám sát hiệu năng hệ thống với Prometheus và Grafana

Prometheus là một công cụ giám sát và cảnh báo mã nguồn mở, ban đầu được phát triển bởi SoundCloud. Kể từ năm 2012, nó đã được nhiều tổ chức áp dụng và hiện là dự án độc lập dưới sự bảo trợ của Cloud Native Computing Foundation (CNCF), cùng với Kubernetes. Điểm nổi bật của Prometheus bao gồm:

  • Mô hình dữ liệu đa chiều với chuỗi thời gian được định danh bằng tên metric và cặp key-value
  • Ngôn ngữ truy vấn PromQL linh hoạt để khai thác dữ liệu
  • Không phụ thuộc vào lưu trữ phân tán — mỗi node tự quản lý dữ liệu
  • Thu thập dữ liệu theo mô hình kéo (pull) qua HTTP
  • Hỗ trợ đẩy dữ liệu thông qua gateway trung gian
  • Khám phá mục tiêu giám sát qua service discovery hoặc cấu hình tĩnh
  • Tích hợp đa dạng dashboard và công cụ trực quan hóa

Cài đặt và cấu hình cơ bản

Yêu cầu môi trường: Ubuntu 22.04, Docker 24.06, Prometheus 2.47.0, Grafana 10.1.2, node-exporter 1.6.1, mysqld-exporter 0.15.0.

1. Tải các image cần thiết

docker pull prom/prometheus:v2.47.0
docker pull grafana/grafana:10.1.2
docker pull prom/node-exporter:1.6.1
docker pull prom/mysqld-exporter:0.15.0

2. Khởi động Prometheus với cấu hình tùy chỉnh

docker run --name prom-server -d -p 9090:9090 prom/prometheus:v2.47.0
docker cp prom-server:/etc/prometheus /opt/prom-config
docker stop prom-server && docker rm prom-server
docker run --name prom-server -d -p 9090:9090 \
  -v /opt/prom-config:/etc/prometheus \
  prom/prometheus:v2.47.0

Truy cập http://[IP]:9090 để kiểm tra dịch vụ.

3. Triển khai Grafana

docker run -d --name=grafana-ui -p 3000:3000 grafana/grafana:10.1.2

Đăng nhập tại http://[IP]:3000 với tài khoản mặc định admin/admin.

Thiết lập giám sát

Quy trình thiết lập gồm các bước chính:

  1. Kết nối Grafana với Prometheus làm nguồn dữ liệu
  2. Triển khai các exporter để thu thập chỉ số
  3. Cấu hình job trong prometheus.yml để Prometheus thu thập dữ liệu từ exporter
  4. Nhập dashboard tương ứng vào Grafana để hiển thị dữ liệu

1. Cấu hình nguồn dữ liệu trong Grafana

Vào Connections → Data sources → Add data source → Prometheus, nhập URL Prometheus (vd: http://localhost:9090), đặt tên là Prom-Docker, sau đó lưu lại.

2. Giám sát hệ thống Linux với node-exporter

docker run --name sys-metrics -p 9100:9100 -d prom/node-exporter:1.6.1

Sửa file /opt/prom-config/prometheus.yml:

- job_name: "linux-host"
  static_configs:
    - targets: ["[IP_MÁY_CHỦ]:9100"]

Khởi động lại Prometheus:

docker restart prom-server

Tải dashboard "Node Exporter Full" (ID 1860) từ Grafana Dashboards, nhập vào Grafana và chọn nguồn dữ liệu Prom-Docker.

3. Giám sát MySQL với mysqld-exporter

Tạo file /opt/mysql-export/.my.cnf:

[client]
host=10.533.200.99
port=3306
user=root
password=123456

Khởi động exporter:

docker run -d -p 9104:9104 --name mysql-metrics \
  -e DATA_SOURCE_NAME="root:123456@(10.533.200.99:3306)/" \
  -v /opt/mysql-export/.my.cnf:/.my.cnf \
  prom/mysqld-exporter:0.15.0

Thêm job vào Prometheus:

- job_name: "mysql-db"
  static_configs:
    - targets: ["[IP_EXPORTER]:9104"]

Tải dashboard MySQL Overview (ID 7362) và nhập vào Grafana.

4. Giám sát ứng dụng Spring Boot

Thêm dependency vào pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Cấu hình trong application.yml:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    metrics:
      enabled: true
    prometheus:
      enabled: true
  metrics:
    tags:
      application: ${spring.application.name}

Thêm job Prometheus:

- job_name: "app-spring"
  metrics_path: /actuator/prometheus
  static_configs:
    - targets: ["[IP_APP]:[PORT]"]

Tải dashboard JVM (Micrometer) (ID 4701) để theo dõi hiệu năng ứng dụng Java.

Thẻ: prometheus Grafana SpringBoot node-exporter mysqld-exporter

Đăng vào ngày 10 tháng 6 lúc 19:11