ai

ONNX Runtime กับ Service Mesh Setup — วิธีใช้

onnx runtime service mesh setup
ONNX Runtime กับ Service Mesh Setup — วิธีใช้

ONNX Runtime สำหรับ ML Inference

ONNX Runtime กับ Service Mesh Setup — วิธีใช้

ONNX Runtime เป็น Inference Engine จาก Microsoft ที่ออกแบบมาให้รัน ML Models ได้เร็วที่สุดบนทุก Platform รองรับ ONNX Format ที่เป็นมาตรฐานกลางระหว่าง ML Frameworks ทำให้ Train ด้วย PyTorch แล้วรันด้วย ONNX Runtime ได้

Service Mesh เช่น Istio ช่วยจัดการ ML Inference Services โดยอัตโนมัติ ทำ Traffic Management ระหว่าง Model Versions, Load Balancing, mTLS, Retry/Circuit Breaker และ Observability โดยไม่ต้องเขียน Code เพิ่ม

Istio Service Mesh Configuration

ONNX Runtime กับ Service Mesh Setup — วิธีใช้
# === Istio Service Mesh สำหรับ ML Inference ===

# 1. Kubernetes Deployment
# k8s/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ml-inference-v1
  namespace: ml
  labels:
    app: ml-inference
    version: v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: ml-inference
      version: v1
  template:
    metadata:
      labels:
        app: ml-inference
        version: v1
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      containers:
        - name: inference
          image: myregistry/ml-inference:v1
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: "500m"
              memory: "512Mi"
            limits:
              cpu: "2"
              memory: "2Gi"
              nvidia.com/gpu: "1"
          readinessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 30
---
# 2. Service
apiVersion: v1
kind: Service
metadata:
  name: ml-inference
  namespace: ml
spec:
  ports:
    - port: 80
      targetPort: 8080
      name: http
  selector:
    app: ml-inference
---
# 3. Istio VirtualService — Traffic Management
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: ml-inference
  namespace: ml
spec:
  hosts:
    - ml-inference
  http:
    - match:
        - headers:
            x-model-version:
              exact: "v2"
      route:
        - destination:
            host: ml-inference
            subset: v2
    - route:
        - destination:
            host: ml-inference
            subset: v1
          weight: 90
        - destination:
            host: ml-inference
            subset: v2
          weight: 10
      timeout: 5s
      retries:
        attempts: 3
        perTryTimeout: 2s
        retryOn: 5xx, reset, connect-failure
---
# 4. DestinationRule — Load Balancing + Circuit Breaker
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: ml-inference
  namespace: ml
spec:
  host: ml-inference
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 100
      http:
        h2UpgradePolicy: DEFAULT
        http1MaxPendingRequests: 100
        http2MaxRequests: 1000
    outlierDetection:
      consecutive5xxErrors: 3
      interval: 10s
      baseEjectionTime: 30s
      maxEjectionPercent: 50
    loadBalancer:
      simple: LEAST_REQUEST
  subsets:
    - name: v1
      labels:
        version: v1
    - name: v2
      labels:
        version: v2

Best Practices

  • ONNX Opset: ใช้ Opset Version ล่าสุดที่ ONNX Runtime รองรับ เพื่อ Performance ดีที่สุด
  • Execution Providers: ใช้ CUDA EP สำหรับ GPU, TensorRT EP สำหรับ NVIDIA Optimization
  • Service Mesh mTLS: เปิด mTLS ระหว่าง Services ป้องกัน Data ระหว่างทาง
  • Circuit Breaker: ตั้ง Circuit Breaker ป้องกัน Cascade Failure
  • Canary Deployment: ใช้ Istio Traffic Splitting ทดสอบ Model Version ใหม่
  • Metrics: ติดตาม Latency, Throughput, Error Rate สำหรับทุก Model Version

ONNX Runtime คืออะไร

High-performance Inference Engine จาก Microsoft รัน ML Models รูปแบบ ONNX รองรับ CPU GPU NPU หลาย Platform เร็วกว่า Native Framework 2-3 เท่า

XM Legend · เทรดเดอร์ & ผู้สอน Forex 13 ปี

ผู้ก่อตั้ง SiamCafe ตั้งแต่ปี 1997 · เทรดเดอร์สาย Forex มากกว่า 13 ปี ได้รับการยกย่องเป็น XM Legend · แบ่งปันความรู้ Forex, ไอที, AI และการเทรด จากประสบการณ์จริงในตลาดจริง