Triển khai Prometheus và Grafana để theo dõi hiệu suất Agent
Bước đầu tiên là cài đặt Prometheus để thu thập metrics từ các Pod AI Agent, sau đó dùng Grafana để trực quan hóa dữ liệu. Chúng ta cần tạo ServiceMonitor để Prometheus tự động phát hiện endpoint metrics của ứng dụng.
Đầu tiên, cài đặt Prometheus Operator và Grafana thông qua Helm chart. Command này sẽ tạo namespace monitoring và deploy các component cần thiết.
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install monitoring prometheus-community/kube-prometheus-stack -n monitoring --create-namespace
Kết quả mong đợi: Kubernetes sẽ tạo các Resource như StatefulSet (Prometheus), Deployment (Grafana) và ServiceMonitor trong namespace monitoring.
Sau đó, cấu hình ServiceMonitor để Prometheus biết cách scrape metrics từ Deployment Agent đã tạo ở Phần 5. File cấu hình này cần được áp dụng vào cluster.
cat > /tmp/agent-servicemonitor.yaml
Kết quả mong đợi: Prometheus sẽ bắt đầu thu thập dữ liệu từ endpoint /metrics của Pod Agent mỗi 15 giây. Bạn có thể verify bằng cách truy cập Grafana dashboard hoặc xem trạng thái targets trong Prometheus UI.
Để verify kết quả, expose dịch vụ Prometheus và Grafana để truy cập từ browser. Sau đó vào Grafana để xem các biểu đồ CPU, Memory và số lượng request của Agent.
kubectl port-forward svc/prometheus-kube-prometheus-prometheus -n monitoring 9090:9090
kubectl port-forward svc/kube-prometheus-grafana -n monitoring 3000:80
Kết quả mong đợi: Mở trình duyệt truy cập http://localhost:9090/targets thấy status "UP" cho ai-agent-monitor. Truy cập http://localhost:3000 login (admin/prometheus) để xem dashboard "Kubernetes / Compute Resources / Nodes".
Cấu hình Log Aggregation với Loki và Grafana
Thay vì dùng ELK stack nặng nề, chúng ta dùng Loki để tập trung nhật ký (log aggregation) vì nó nhẹ và tích hợp tốt với Kubernetes. Loki sẽ đọc logs từ container và lưu trữ để truy vấn sau này.
Deploy Loki, Promtail và Grafana thông qua Helm chart. Command này tạo stack logging trong namespace logging.
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
helm install loki grafana/loki-stack -n logging --create-namespace --set grafana.adminPassword=admin --set promtail.enabled=true --set loki.persistence.enabled=false --set loki.storage.type=memory
Kết quả mong đợi: Các Pod của Loki và Promtail được tạo ra. Promtail sẽ bắt đầu đọc logs từ các container trong cluster.
Cấu hình Promtail để đọc logs từ các container của AI Agent. Chúng ta tạo ConfigMap chứa file config.yaml để Promtail biết cần lọc logs nào.
cat > /tmp/promtail-config.yaml
Kết quả mong đợi: ConfigMap được tạo. Promtail sẽ tự động reload config và bắt đầu gửi logs của các Pod có label app=ai-agent lên Loki.
Để verify, truy cập Grafana đã deploy ở bước trước, vào Explore tab, chọn Loki data source. Gõ query {app="ai-agent"} để xem logs thời gian thực.
kubectl port-forward svc/loki-grafana -n logging 3000:80
Kết quả mong đợi: Trong tab Explore của Grafana, bạn thấy dòng logs từ AI Agent hiện ra theo thời gian thực, bao gồm cả các log debug, info, error của quá trình xử lý Agent.
Sử dụng Distributed Tracing với Jaeger để theo dõi luồng xử lý
Để debug các vấn đề phức tạp như Agent treo hoặc mất kết nối giữa các bước, chúng ta cần Distributed Tracing. Jaeger sẽ theo dõi một request đi qua nhiều microservices hoặc nhiều bước trong một Agent.
Deploy Jaeger vào cluster. Command này tạo Jaeger với storage type memory (phù hợp cho dev/staging) hoặc có thể đổi sang elasticsearch cho production.
helm repo add jaegertracing https://jaegertracing.github.io/helm-charts
helm repo update
helm install jaeger jaegertracing/jaeger -n tracing --create-namespace --set allInOne.storage.type=memory --set allInOne.replicas=1 --set agent.enabled=true --set collector.enabled=true --set query.enabled=true --set ingress.enabled=false
Kết quả mong đợi: Các component của Jaeger (Collector, Agent, Query) được triển khai. Agent của Jaeger sẽ lắng nghe traffic từ các Pod khác.
Tích hợp OpenTelemetry vào ứng dụng AI Agent để gửi trace data về Jaeger. Bạn cần thêm thư viện opentelemetry-exporter-jaeger vào code Python và cấu hình endpoint.
cat >> /app/requirements.txt /app/telemetry_init.py
Kết quả mong đợi: Khi Agent chạy, nó sẽ tự động tạo các Span (đoạn trace) cho mỗi request HTTP hoặc mỗi bước xử lý quan trọng và gửi về Jaeger Collector.
Verify bằng cách expose dịch vụ Jaeger Query UI và tìm kiếm các trace của Agent.
kubectl port-forward svc/jaeger-query -n tracing 16686:16686
Kết quả mong đợi: Truy cập http://localhost:16686. Chọn service "ai-agent", bạn sẽ thấy các trace hiển thị dưới dạng đồ thị (waterfall) cho thấy thời gian xử lý của từng bước, giúp xác định điểm nghẽn (bottleneck).
Phân tích các lỗi thường gặp khi Agent treo hoặc mất kết nối
Khi Agent bị treo (hang) hoặc mất kết nối, nguyên nhân thường do OOM (Out of Memory), timeout của LLM, hoặc deadlock trong luồng xử lý. Chúng ta sẽ dùng kết hợp logs và metrics để debug.
Kiểm tra lỗi OOM Kill. Khi container bị OOM, Kubernetes sẽ giết nó. Log của kubelet sẽ ghi nhận điều này, và metrics của container sẽ đột ngột về 0.
kubectl get pod -n default | grep ai-agent
kubectl logs -n default --previous
kubectl describe pod -n default | grep -A 5 "Last State"
Kết quả mong đợi: Nếu thấy trạng thái "OOMKilled" trong phần "Last State" của describe pod, nghĩa là Agent đã dùng hết RAM giới hạn. Cần tăng request/limit memory trong Deployment.
Phân tích timeout khi gọi LLM. LLM thường mất nhiều thời gian, nếu không cấu hình timeout đúng, Agent sẽ treo ở bước gọi API. Kiểm tra logs để tìm các lỗi "timeout" hoặc "connection reset".
kubectl logs -n default -l app=ai-agent --tail=50 | grep -i "timeout\|error\|exception"
Kết quả mong đợi: Nếu thấy lỗi "Read timed out" hoặc "Max retries exceeded", cần kiểm tra cấu hình timeout trong code gọi API LLM (thường là parameter timeout trong hàm client). Đồng thời kiểm tra trace trong Jaeger để xem bước nào bị treo lâu nhất.
Xử lý deadlock hoặc race condition trong luồng xử lý đa Agent. Sử dụng logs chi tiết để xem trạng thái của từng Agent trong một task. Nếu một Agent chờ input mãi không nhận được, đó có thể là deadlock.
# Xem logs với level debug (nếu đã bật)
kubectl logs -n default -l app=ai-agent --tail=100 | grep -E "waiting|blocked|deadlock"
# Kiểm tra số lượng thread đang chạy (nếu có tool monitoring trong container)
kubectl exec -it -n default -- ps aux | grep python
Kết quả mong đợi: Nếu thấy logs lặp lại "waiting for response" mà không có hoạt động nào khác, và trace trong Jaeger cho thấy span bị dừng lại ở một bước cụ thể không tiến triển, cần kiểm tra code xử lý async hoặc queue message.
Debug mất kết nối với Gateway hoặc Database. Kiểm tra network policy và DNS resolution trong cluster.
# Kiểm tra DNS resolution
kubectl exec -it -n default -- nslookup
# Kiểm tra connectivity
kubectl exec -it -n default -- wget -q -O - http://:/health
Kết quả mong đợi: Nếu nslookup trả về "NXDOMAIN" hoặc wget báo "Connection refused", nghĩa là lỗi mạng hoặc service name sai. Cần kiểm tra lại Service và NetworkPolicy trong Kubernetes.
Điều hướng series:
Mục lục: Series: Xây dựng nền tảng AI Agent tự động hóa với LangGraph, CrewAI và Kubernetes
« Phần 8: Xây dựng Gateway API để điều phối Agent từ bên ngoài
Phần 10: Troubleshooting nâng cao và chiến lược sản xuất »