1. Tổng quan

Prometheus là hệ thống giám sát và cảnh báo mã nguồn mở, chuyên thu thập metrics từ ứng dụng và hạ tầng (như Kubernetes, node, service) theo mô hình pull, giúp theo dõi hiệu năng và phát hiện sự cố sớm.

Chức năng chính: 

  • Thu thập và lưu trữ dữ liệu dạng time-series metrics (CPU, RAM, request, latency…)
  • Hỗ trợ truy vấn bằng PromQL để phân tích theo thời gian
  • Tích hợp Alertmanager để cấu hình cảnh báo khi vượt ngưỡng (ví dụ CPU cao, pod restart liên tục)
  • Hỗ trợ nhiều exporter như node-exporter, kube-state-metrics để giám sát hạ tầng Kubernetes 

Grafana là nền tảng trực quan hóa dữ liệu mạnh mẽ, cho phép tạo dashboard từ nhiều nguồn (Prometheus, Loki, Elasticsearch, InfluxDB…), giúp quan sát trạng thái hệ thống realtime và phân tích xu hướng dễ dàng.

Chức năng chính:

  • Hiển thị dữ liệu dưới dạng biểu đồ, bảng, heatmap… giúp người vận hành dễ dàng theo dõi và phân tích hệ thống. 
  • Cho phép tạo dashboard tùy chỉnh, phân quyền người dùng, chia sẻ dashboard, và gửi cảnh báo qua email/Slack/Telegram…

Khi kết hợp với nhau, Prometheus và Grafana trở thành bộ công cụ mạnh mẽ để xây dựng hệ thống monitoring cho hạ tầng và ứng dụng, đặc biệt trong môi trường Microservices và Kubernetes 

2. Hướng dẫn cài đặt Prometheus và Grafana

Bước 1: Đứng từ VM đã kết nối được tới cụm VNPT Kubernetes Service và cài đặt Helm.

curl -L https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

Bước 2: Thêm repo Helm của Prometheus, Grafana:

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
  • Thực hiện cài đặt Prometheus và Grafana bằng câu lệnh sau:
helm install kube-prometheus-stack prometheus-community/kube-prometheus-stack \
  --namespace monitoring \
  --create-namespace \
  --version 80.3.0 \
  --set prometheus.prometheusSpec.retentionSize=5Gi \
  --set prometheus.prometheusSpec.retention=1d \
  --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.resources.requests.storage=10Gi \
  --set alertmanager.alertmanagerSpec.storage.volumeClaimTemplate.spec.resources.requests.storage=2Gi \
  --set grafana.ingress.enabled=true \
  --set grafana.ingress.ingressClassName=nginx \
  --set grafana.ingress.hosts[0]=grafana.my.org

Trong đó:

  • set prometheus.prometheusSpec.retentionSize: Dung lượng lưu trữ metrics
  • set prometheus.prometheusSpec.retention: Thời gian lưu trữ metrics
  • prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.resources.requests.storage: Dung lượng PVC cho Prometheus (lưu trữ time series database – TSDB).
  • alertmanager.alertmanagerSpec.storage.volumeClaimTemplate.spec.resources.requests.storage: Dung lượng PVC cho Alertmanager để lưu trạng thái alert.
  • grafana.ingress.enabled: Bật Ingress để có thể truy cập Grafana.
  • grafana.ingress.ingressClassName: Set ingressclassname cho grafana.
  • grafana.ingress.hosts[0]: Hostname của grafana

* Lưu ý:

  • Có thể chỉ định storageclass cho prometheus và grafana (ví dụ như lvm-thick/thin, nfs-client,....) tùy theo nhu cầu sử dụng. Nếu dùng nfs-client, tham khảo thêm tại đây.
  • Thời gian lưu trữ và dung lượng lưu trữ của metrics tùy theo nhu cầu hệ thống, mặc định theo cài đặt trên là 1 ngày và 5Gi.

Bước 3: Kiểm tra trạng thái

  • Thực hiện kiểm tra pod prometheus, grafana đã running:

  • Chạy câu lệnh sau để lấy password của user admin grafana
kubectl get secret -n monitoring kube-prometheus-stack-grafana -o jsonpath="{.data.admin-password}" | base64 -d; echo
  • Thực hiện kiểm tra trên giao diện