← Về danh sách bài học
Bài 23/25
📊 Bài 23: Monitoring với Prometheus
🎯 Sau bài học này, bạn sẽ:
- Cài đặt Prometheus Stack
- Query metrics với PromQL
- Tạo Grafana dashboards
1. Cài Kube-Prometheus-Stack
# Cài bằng Helm
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack \
--namespace monitoring --create-namespace
# Xem pods
kubectl get pods -n monitoring
2. Truy Cập UI
# Prometheus UI
kubectl port-forward svc/prometheus-kube-prometheus-prometheus \
9090:9090 -n monitoring
# Grafana (user: admin, pass: prom-operator)
kubectl port-forward svc/prometheus-grafana \
3000:80 -n monitoring
# AlertManager
kubectl port-forward svc/prometheus-kube-prometheus-alertmanager \
9093:9093 -n monitoring
3. PromQL Basics
# CPU usage của pods
sum(rate(container_cpu_usage_seconds_total[5m])) by (pod)
# Memory usage
container_memory_usage_bytes{namespace="default"}
# HTTP requests
sum(rate(http_requests_total[5m])) by (status_code)
4. ServiceMonitor
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: my-app-monitor
labels:
release: prometheus
spec:
selector:
matchLabels:
app: my-app
endpoints:
- port: metrics
path: /metrics
interval: 30s
💡 App cần expose metrics:
Endpoint
Endpoint
/metrics theo Prometheus format
📝 Tóm Tắt
- Prometheus: Thu thập và lưu metrics
- Grafana: Visualization dashboards
- AlertManager: Quản lý alerts
- ServiceMonitor: Auto-discover targets