FSDS SOTA 2026 DSAI + EDAI Textbook
T2-P3-U03 • PRODUCTION LAB GUIDE ~3 phút đọc

Lab 13 (k8s_ai_platform_lab) — Nền tảng Xử lý Job AI Bất đồng bộ Trên Kubernetes (Helmfile, HashiCorp Vault + ESO, KEDA, KServe, MIG/DRA & FastAPI)

Hướng dẫn thực hành, cài đặt môi trường .venv/uv và tiêu chuẩn kiểm định pytest.

Unit ID: T2-P3-U03 | Chương Song hành: ch-15-kubernetes-and-cloud-native-ai-platforms.md
Truy xuất Giáo trình Gốc (R44): Implement an async job processing system; Deploy your API applications locally on k8s; k8s, Helm, KServe, HashiCorp Vault.
Kiến trúc Hai Tầng (100% Offline Verifiable): Tầng 1 cung cấp trọn bộ manifest sản xuất Helmfile + Helm Chart + Vault ESO + KEDA (ScaledObject + TriggerAuthentication) + KServe + NetworkPolicy + PDB (0 bí mật hardcoded); Tầng 2 (k8s_ai_platform_lab) chạy kiểm toán manifest, mô phỏng kube-scheduler/OOMKill/MIG vs DRA/KEDA vs CPU HPA/Canary/Vault Lease và dịch vụ FastAPI Async Job Queue + BoundedInMemorySpanExporter, đạt 21/21 pytest pass (0 skip), 0 lỗi ruff/mypy, 0 # type: ignore, uv.lock xác thực sạch.


1. Mục tiêu Bài thực hành & 6 Bất biến Kỹ thuật (seed = 42)#

  1. Kiểm toán Tầng 1 Helmfile + Helm + Vault ESO (Tests 02–06, 20–21): Xác thực DAG phụ thuộc keda & vault-eso \(\to\) kserve \(\to\) ml-async-platform cho cả dev (gpuMode=dra) và prod (gpuMode=mig) kèm DisruptionsAllowed >= 1 (replicas > minAvailable), quét 12 tệp YAML/Template đảm bảo 0 mật khẩu/token tĩnh (Vault KV v2 qua ServiceAccount JWT, automountServiceAccountToken: false), API/KServe đạt chuẩn bảo mật PSS Restricted, KEDA TriggerAuthentication, NetworkPolicy default-deny + doc-ocr-reranker/S3 443/Prometheus 8080/9090 và PDB.
  2. kube-scheduler 2 Pha & Thứ tự Thu hồi QoS / OOMKill (Tests 07–09): Mô phỏng Filter -> Score (NodeResourcesFit, TaintToleration, ImageLocality, TopologySpread) và áp lực bộ nhớ 39,130 MiB / 32,768 MiB trên Node: toàn bộ Pod BestEffort (oom_score_adj = 1000) bị tiêu diệt 100.0% trước tiên, theo sau là Burstable vượt ngưỡng (75.0% sống), trong khi các Pod API/KServe Guaranteed (oom_score_adj = -997) sống sót 100.0% (3/3).
  3. Lập lịch GPU Cụm 4 × 80 GB: Whole vs Time-Slicing vs MIG vs DRA (Tests 10–11): Với 12 workload 16 GB trên 320.0 GB VRAM: Whole GPU chỉ nhận 4/12 (33.33%, 20.0% VRAM); Time-Slicing nhận 12/12 nhưng thiếu cách ly khiến một sự cố tăng VRAM 52 GB gây chết chéo 2 workload hàng xóm (CUDA OOM); NVIDIA MIG (2g.20gb) nhận 12/12 (3.0x mật độ) với 0 lỗi lan truyền; và K8s DRA (ResourceClaim 16GB) mở rộng tối đa tới 20 claim (5.0x, 100.0% đóng gói VRAM, 0 lỗi lan truyền).
  4. KEDA Queue-Depth vs CPU-Only HPA Trên Tải Burst 8x (Test 12): Trên 60 tick (600s, 744 job đến với burst 32 job/tick), worker chờ GPU/I/O giữ CPU tối đa 61.8% (< 70% target) làm CPU-Only HPA kẹt ở 2 Pod, nghẽn đỉnh 432 job, p95 chờ 525.25s (18.33% đạt SLA <=30s). KEDA ScaledObject co giãn theo độ sâu hàng đợi lên 8 Pod, hạ đỉnh nghẽn xuống 32 job, đưa p95 về 10.0s (nhanh hơn 52.52x, 100.0% đạt SLA).
  5. Cổng An toàn Canary / Blue-Green & Xoay vòng Lease Vault (Tests 13–14): Trước bản lỗi 8.5% error rate dù /health vẫn 200 OK, RollingUpdate làm lộ 100.0% lưu lượng; KServe Canary SLO Gate phát hiện và rollback ngay tại 10.0%; Blue-Green chặn từ smoke test (0.0% bán kính nổ). Đồng thời Vault + ESO xoay vòng 4 lease động (TTL 300s, refresh 60s) trong 900s với 0 lỗi xác thực và 0 lần khởi động lại Pod.
  6. Dịch vụ FastAPI Async Job Queue + Bounded OpenTelemetry (Tests 01, 15–19): Cung cấp /health, /v1/jobs (202 Accepted + lọc trùng idempotency_key), /v1/jobs/drain (tối đa 3 lần thử = 1 gốc + 2 retry + chuyển DEAD_LETTERED vào DLQ khi gặp poison_payload), /v1/jobs/{job_id} (404/422), thu đủ 14 OpenTelemetry spans trong BoundedInMemorySpanExporter(maxlen=256) với 0 bí mật rò rỉ.

2. Cấu trúc Thư mục Lab 13#

text28 dòng
lab-13-k8s-helm-vault-async-job-platform/
├── helmfile.yaml.gotmpl                        # Điều phối đa release dev/prod (vault-eso, keda, kserve, ml-async-platform)
├── charts/ml-async-platform/
│   ├── Chart.yaml
│   ├── values.yaml                             # Tham số QoS Guaranteed, MIG/DRA, KEDA, KServe Canary & Vault KV v2 paths
│   └── templates/
│       ├── serviceaccount.yaml                 # ServiceAccount, ClusterIP Service & Gateway API HTTPRoute
│       ├── deployment.yaml                     # API Guaranteed QoS, PSS Restricted, 3 Probes, topologySpread, Vault envFrom
│       ├── worker-deployment.yaml              # Worker InitContainer precheck, MIG/DRA switch, terminationGracePeriodSeconds
│       ├── keda-scaledobject.yaml              # KEDA ScaledObject theo Redis queue lag + stabilization windows
│       ├── keda-triggerauthentication.yaml     # KEDA TriggerAuthentication kết nối Vault Kubernetes Auth
│       ├── kserve-inferenceservice.yaml        # KServe InferenceService (Standard mode, PSS Restricted, probes, GPU)
│       ├── external-secret-vault.yaml          # SecretStore + ExternalSecret (Vault KV v2 qua SA JWT — 0 plaintext secret)
│       ├── networkpolicy.yaml                  # Default-deny all + API/Worker/KServe/Broker/Vault/S3-443/Metrics
│       └── pdb.yaml                            # PodDisruptionBudget cho API & Worker
├── src/k8s_ai_platform_lab/
│   ├── __init__.py
│   ├── manifest_and_vault_audit.py             # Trình diễn dịch Helm template & kiểm toán DAG, bí mật, QoS, PSS, MIG/DRA
│   └── scheduler_keda_and_async_app.py         # Scheduler, QoS/OOMKill, MIG/DRA, KEDA vs HPA, Canary/Vault & FastAPI Queue
├── scripts/
│   └── run_lab_demo.py                         # CLI sinh artifacts/t2_ch15_lab13_results.json (6 KPI, 4 Bar, 4 Table)
└── tests/
    └── test_k8s_ai_platform_lab.py             # 21 hàm pytest (0 skip, Hypothesis invariants, AST/type-ignore & C3/H6 tests)
├── artifacts/
│   └── t2_ch15_lab13_results.json              # Kết quả đo đạc xác định (seed=42)
├── pyproject.toml
├── uv.lock
└── README.md

3. Hướng dẫn Chạy & Kiểm thử Tự động#

bash10 dòng
cd 03-track-2-data-and-ai-engineering/part-03-distributed-k8s-cloud-and-gateway/labs/lab-13-k8s-helm-vault-async-job-platform

# 1. Sinh báo cáo kết quả chuẩn hóa
PYTHONPATH=src python scripts/run_lab_demo.py --seed 42 --output artifacts/t2_ch15_lab13_results.json

# 2. Kiểm định lint, kiểu tĩnh mypy strict, uv.lock và toàn bộ 21 test cases
ruff check src tests scripts
mypy src tests
pytest -p no:cacheprovider -v
uv lock --check