Triển khai FastAPI làm Gateway trung gian
Chúng ta cần một lớp Gateway để đóng vai trò là điểm đến duy nhất cho tất cả các yêu cầu từ bên ngoài, sau đó Gateway này sẽ chuyển tiếp yêu cầu đến đúng Pod Agent dựa trên logic điều phối.
Tạo thư mục dự án Gateway và file cấu hình chính:
mkdir -p /opt/ai-platform/gateway
cd /opt/ai-platform/gateway
touch main.py requirements.txt
File requirements.txt chứa các thư viện cần thiết cho FastAPI, quản lý phiên làm việc và client HTTP:
fastapi==0.104.1
uvicorn==0.24.0
httpx==0.25.2
python-multipart==0.0.6
pydantic==2.5.2
slowapi==0.1.9
Tạo file main.py với logic xử lý request, xác thực cơ bản và định tuyến:
from fastapi import FastAPI, HTTPException, Depends, Header, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel
import httpx
import os
from slowapi import Limiter
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
app = FastAPI(title="AI Agent Gateway")
limiter = Limiter(key_func=get_remote_address, app=app, default_limits=["100 per minute"])
# Cấu hình từ biến môi trường
AGENT_SERVICE_URL = os.getenv("AGENT_SERVICE_URL", "http://agent-service:8000")
class TaskRequest(BaseModel):
task: str
agent_type: str
priority: int = 1
class AuthHeader(BaseModel):
api_key: str
@app.exception_handler(RateLimitExceeded)
async def rate_limit_handler(request: Request, exc: RateLimitExceeded):
return JSONResponse(
status_code=429,
content={"detail": str(exc.detail)}
)
async def verify_api_key(api_key: str = Header(...)):
valid_key = os.getenv("GATEWAY_API_KEY", "secret-key-12345")
if api_key != valid_key:
raise HTTPException(status_code=401, detail="Invalid API Key")
return api_key
@app.post("/execute")
@limiter.limit("10 per minute")
async def execute_task(request: TaskRequest, api_key: str = Depends(verify_api_key)):
try:
async with httpx.AsyncClient() as client:
response = await client.post(
f"{AGENT_SERVICE_URL}/process",
json={
"task": request.task,
"agent_type": request.agent_type,
"priority": request.priority
},
timeout=30.0
)
return response.json()
except httpx.RequestError as e:
raise HTTPException(status_code=503, detail=f"Agent service unavailable: {str(e)}")
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": "gateway"}
Kết quả mong đợi: File main.py được tạo với endpoint /execute yêu cầu API Key và endpoint /health để kiểm tra trạng thái.
Thiết kế Endpoint phân phối và xác thực
Chúng ta cần đóng gói Gateway thành Docker Image và cấu hình cơ chế xác thực thông qua biến môi trường để dễ dàng quản lý trong Kubernetes.
Tạo file Dockerfile để đóng gói ứng dụng:
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY main.py .
ENV GATEWAY_API_KEY=super-secret-key-change-me
ENV AGENT_SERVICE_URL=http://agent-service:8000
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Xây dựng Docker Image và chạy thử nghiệm cục bộ để kiểm tra logic:
cd /opt/ai-platform/gateway
docker build -t ai-gateway:latest .
docker run -d --name gateway-test -p 8000:8000 -e GATEWAY_API_KEY=my-secret-key ai-gateway:latest
Verify kết quả bằng cách gọi API không có key (phải bị reject) và có key (phải thành công):
curl -X POST http://localhost:8000/execute \
-H "Content-Type: application/json" \
-d '{"task": "Summarize this text", "agent_type": "writer", "priority": 1}'
curl -X POST http://localhost:8000/execute \
-H "Content-Type: application/json" \
-H "api-key: my-secret-key" \
-d '{"task": "Summarize this text", "agent_type": "writer", "priority": 1}'
Kết quả mong đợi: Lệnh đầu tiên trả về 401 Unauthorized, lệnh thứ hai trả về 200 OK (hoặc 503 nếu service Agent chưa chạy, nhưng lỗi xác thực đã vượt qua).
Cấu hình Ingress Controller để truy cập qua Internet
Sau khi có Image Gateway, chúng ta cần triển khai nó lên Kubernetes và cấu hình Ingress để ánh xạ URL công cộng vào Service Gateway.
Tạo file Kubernetes Manifest gateway-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: ai-gateway
namespace: ai-platform
spec:
replicas: 2
selector:
matchLabels:
app: ai-gateway
template:
metadata:
labels:
app: ai-gateway
spec:
containers:
- name: gateway
image: ai-gateway:latest
ports:
- containerPort: 8000
env:
- name: GATEWAY_API_KEY
valueFrom:
secretKeyRef:
name: gateway-secrets
key: api-key
- name: AGENT_SERVICE_URL
value: "http://agent-service:8000"
resources:
limits:
cpu: "500m"
memory: "256Mi"
requests:
cpu: "200m"
memory: "128Mi"
Tạo file gateway-service.yaml để expose Gateway:
apiVersion: v1
kind: Service
metadata:
name: gateway-service
namespace: ai-platform
spec:
selector:
app: ai-gateway
ports:
- port: 80
targetPort: 8000
protocol: TCP
type: ClusterIP
Tạo file gateway-ingress.yaml để cấu hình Ingress (giả sử đã cài đặt Nginx Ingress Controller):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gateway-ingress
namespace: ai-platform
annotations:
nginx.ingress.kubernetes.io/rate-limit: "100"
nginx.ingress.kubernetes.io/rate-limit-window: "1m"
spec:
ingressClassName: nginx
rules:
- host: api.ai-platform.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: gateway-service
port:
number: 80
Triển khai Secret chứa API Key trước khi áp dụng Deployment:
kubectl create secret generic gateway-secrets \
--from-literal=api-key=production-secret-key-xyz \
--namespace ai-platform
Áp dụng toàn bộ cấu hình:
kubectl apply -f gateway-deployment.yaml
kubectl apply -f gateway-service.yaml
kubectl apply -f gateway-ingress.yaml
Verify kết quả bằng cách kiểm tra Pod và Ingress:
kubectl get pods -n ai-platform -l app=ai-gateway
kubectl get ingress -n ai-platform
Kết quả mong đợi: 2 Pod Gateway ở trạng thái Running và Ingress có địa chỉ IP được cấp (hoặc <pending> nếu dùng cloud provider chưa cấp IP).
Test luồng điều phối hoàn chỉnh
Thực hiện kiểm tra cuối cùng để đảm bảo Gateway nhận request, xác thực đúng, và chuyển tiếp đến Agent.
Giả sử Ingress đã cấp IP 192.168.1.100 (thay thế bằng IP thực tế của bạn), thực hiện lệnh test:
curl -X POST http://192.168.1.100/execute \
-H "Host: api.ai-platform.local" \
-H "Content-Type: application/json" \
-H "api-key: production-secret-key-xyz" \
-d '{"task": "Analyze sales data Q4", "agent_type": "analyst", "priority": 2}'
Kiểm tra log của Gateway để xác nhận request đã đi vào:
kubectl logs -n ai-platform -l app=ai-gateway --tail=20
Kiểm tra log của Agent Service để xác nhận request đã được chuyển tiếp thành công:
kubectl logs -n ai-platform -l app=agent-service --tail=20
Kết quả mong đợi: Log Gateway hiển thị request 200, log Agent hiển thị nhận được task "Analyze sales data Q4" và trả về kết quả xử lý.
Đ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 7: Tích hợp Memory và Lưu trữ trạng thái bền vững
Phần 9: Giám sát, Logging và Debugging trong môi trường Kubernetes »