Inference Gateway 및 Dynamic Routing
📅 작성일: 2025-02-05 | 수정일: 2026-02-14 | ⏱️ 읽는 시간: 약 6분
개요
대규모 AI 모델 서빙 환경에서는 다양한 모델에 대한 추론 요청을 효율적으로 라우팅하고 관리하는 것이 핵심입니다. 이 문서에서는 Kubernetes Gateway API와 Kgateway를 활용하여 AI 모델 추론 요청의 동적 라우팅, 로드 밸런싱, 그리고 장애 대응 전략을 구성하는 방법을 다룹니다.
주요 목표
- 지능형 라우팅: 요청 특성에 따른 최적의 모델 백엔드 선택
- 트래픽 분배: 가중치 기반 로드 밸런싱으로 안정적인 서비스 제공
- 점진적 배포: 카나리 및 A/B 테스트를 통한 안전한 모델 업데이트
- 고가용성: 폴백 및 재시도 정책을 통한 서비스 연속성 보장
Inference Gateway 아키텍처
전체 아키텍처 다이어그램
컴포넌트 구조
| 컴포넌트 | 역할 | 설명 |
|---|---|---|
| GatewayClass | 게이트웨이 구현체 정의 | Kgateway 컨트롤러 지정 |
| Gateway | 진입점 정의 | 리스너, TLS, 주소 설정 |
| HTTPRoute | 라우팅 규칙 | 경로, 헤더 기반 라우팅 |
| Backend | 모델 서비스 | vLLM, TGI 등 추론 서버 |
트래픽 플로우
Kgateway는 Kubernetes Gateway API 표준을 구현하여 벤더 중립적인 설정이 가능합니다. 이를 통해 다른 Gateway 구현체로의 마이그레이션이 용이합니다.
Topology-Aware Routing (Kubernetes 1.33+)
Kubernetes 1.33+의 topology-aware routing을 활용하면 동일 AZ 내 Pod 간 통신을 우선시하여 크로스 AZ 데이터 전송 비용을 절감하고 지연 시간을 개선할 수 있습니다.
apiVersion: v1
kind: Service
metadata:
name: vllm-inference
namespace: ai-inference
annotations:
# Kubernetes 1.33+ Topology-Aware Routing
service.kubernetes.io/topology-mode: "Auto"
spec:
selector:
app: vllm
ports:
- port: 8000
targetPort: 8000
# 토폴로지 인식 라우팅 활성화
trafficDistribution: PreferClose
Topology-Aware Routing 효과
| 지표 | 기존 방식 | Topology-Aware | 개선 효과 |
|---|---|---|---|
| 크로스 AZ 트래픽 | 높음 | 최소화 | 데이터 전송 비용 50% 절감 |
| 지연 시간 | 높음 (크로스 AZ) | 낮음 (동일 AZ) | P99 지연 시간 30-40% 개선 |
| 네트워크 대역폭 | 제한적 | 최적화 | 처리량 20-30% 향상 |
- 멀티 AZ 배포: 여러 AZ에 분산된 GPU 노드 간 통신 최적화
- 대규모 추론: 높은 처리량이 필요한 추론 워크로드
- 비용 최적화: 크로스 AZ 데이터 전송 비용 절감이 중요한 경우
Kgateway 설치 및 구성
사전 요구사항
- Kubernetes 1.33 이상 (topology-aware routing 지원)
- Helm 3.x
- Gateway API CRD 설치
Kubernetes 1.33+를 사용하면 topology-aware routing을 활용하여 크로스 AZ 트래픽 비용을 절감하고 지연 시간을 개선할 수 있습니다. Kubernetes 1.34+에서는 projected service account tokens로 보안이 더욱 강화됩니다.
Gateway API CRD 설치
# Gateway API 표준 CRD 설치 (v1.2.0+)
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.2.0/standard-install.yaml
# 실험적 기능 포함 설치 (HTTPRoute 필터 등)
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.2.0/experimental-install.yaml
Gateway API v1.2.0은 다음과 같은 향상된 기능을 제공합니다:
- HTTPRoute 개선: 더 유연한 라우팅 규칙
- GRPCRoute 안정화: gRPC 서비스 라우팅 지원
- BackendTLSPolicy: 백엔드 TLS 설정 표준화
- Kubernetes 1.33+ 통합: Topology-aware routing 지원
Kgateway Helm 차트 설치
# Helm 저장소 추가
helm repo add kgateway oci://cr.kgateway.dev/kgateway-dev/charts
helm repo update
# 네임스페이스 생성
kubectl create namespace kgateway-system
# Kgateway 설치 (v2.0+)
helm install kgateway kgateway/kgateway \
--namespace kgateway-system \
--version 2.0.5 \
--set controller.replicaCount=2 \
--set controller.resources.requests.cpu=500m \
--set controller.resources.requests.memory=512Mi \
--set controller.resources.limits.cpu=1000m \
--set controller.resources.limits.memory=1Gi \
--set metrics.enabled=true \
--set metrics.serviceMonitor.enabled=true
Kgateway v2.0+는 다음과 같은 향상된 기능을 제공합니다:
- Gateway API v1.2.0+ 지원: 최신 Gateway API 표준 완벽 지원
- 향상된 성능: 더 빠른 라우팅 및 낮은 지연 시간
- Kubernetes 1.33+ 최적화: Topology-aware routing 통합
Helm Values 상세 설정
# values.yaml
controller:
replicaCount: 2
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1000m
memory: 1Gi
# 고가용성 설정
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: kgateway
topologyKey: kubernetes.io/hostname
# 메트릭 설정
metrics:
enabled: true
port: 9090
serviceMonitor:
enabled: true
interval: 15s
labels:
release: prometheus
# 로깅 설정
logging:
level: info
format: json
# TLS 설정
tls:
enabled: true
certManager:
enabled: true
issuerRef:
name: letsencrypt-prod
kind: ClusterIssuer
GatewayClass 및 Gateway 설정
GatewayClass 정의
GatewayClass는 Gateway 구현체를 정의합니다.
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: kgateway
spec:
controllerName: kgateway.dev/kgateway-controller
description: "Kgateway for AI inference routing"
parametersRef:
group: kgateway.dev
kind: GatewayClassConfig
name: kgateway-config
---
apiVersion: kgateway.dev/v1alpha1
kind: GatewayClassConfig
metadata:
name: kgateway-config
spec:
# 프록시 설정
proxy:
replicas: 3
resources:
requests:
cpu: "1"
memory: "2Gi"
limits:
cpu: "2"
memory: "4Gi"
# 연결 설정
connectionSettings:
maxConnections: 10000
connectTimeout: 10s
idleTimeout: 60s
Gateway 리소스 정의
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: ai-inference-gateway
namespace: ai-gateway
annotations:
# AWS ALB 통합
service.beta.kubernetes.io/aws-load-balancer-type: "external"
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: "ip"
service.beta.kubernetes.io/aws-load-balancer-scheme: "internet-facing"
spec:
gatewayClassName: kgateway
listeners:
# HTTPS 리스너
- name: https
protocol: HTTPS
port: 443
hostname: "inference.example.com"
tls:
mode: Terminate
certificateRefs:
- name: inference-tls-cert
kind: Secret
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
gateway-access: "true"
# HTTP 리스너 (HTTPS 리다이렉트용)
- name: http
protocol: HTTP
port: 80
hostname: "inference.example.com"
allowedRoutes:
namespaces:
from: Same
# 내부 gRPC 리스너
- name: grpc
protocol: HTTPS
port: 8443
hostname: "inference-grpc.example.com"
tls:
mode: Terminate
certificateRefs:
- name: inference-grpc-tls-cert
allowedRoutes:
kinds:
- kind: GRPCRoute
프로덕션 환경에서는 cert-manager를 사용하여 TLS 인증서를 자동으로 관리하세요. 수동 인증서 관리는 만료로 인한 서비스 중단 위험이 있습니다.
Dynamic Routing 설정
헤더 기반 라우팅
요청 헤더의 x-model-id 값에 따라 적절한 모델 백엔드로 라우팅합니다.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: model-header-routing
namespace: ai-gateway
spec:
parentRefs:
- name: ai-inference-gateway
namespace: ai-gateway
sectionName: https
hostnames:
- "inference.example.com"
rules:
# GPT-4 모델 라우팅
- matches:
- path:
type: PathPrefix
value: /v1/chat/completions
headers:
- name: x-model-id
value: "gpt-4"
backendRefs:
- name: vllm-gpt4-service
namespace: ai-inference
port: 8000
weight: 100
# Claude-3 모델 라우팅
- matches:
- path:
type: PathPrefix
value: /v1/chat/completions
headers:
- name: x-model-id
value: "claude-3"
backendRefs:
- name: vllm-claude3-service
namespace: ai-inference
port: 8000
weight: 100
# Mixtral MoE 모델 라우팅
- matches:
- path:
type: PathPrefix
value: /v1/chat/completions
headers:
- name: x-model-id
value: "mixtral-8x7b"
backendRefs:
- name: tgi-mixtral-service
namespace: ai-inference
port: 8080
weight: 100
경로 기반 라우팅
API 경로에 따라 다른 서비스로 라우팅합니다.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: path-based-routing
namespace: ai-gateway
spec:
parentRefs:
- name: ai-inference-gateway
namespace: ai-gateway
hostnames:
- "inference.example.com"
rules:
# Chat Completions API
- matches:
- path:
type: PathPrefix
value: /v1/chat/completions
backendRefs:
- name: chat-completion-service
port: 8000
# Embeddings API
- matches:
- path:
type: PathPrefix
value: /v1/embeddings
backendRefs:
- name: embedding-service
port: 8000
# Completions API (Legacy)
- matches:
- path:
type: PathPrefix
value: /v1/completions
backendRefs:
- name: completion-service
port: 8000
# Health Check
- matches:
- path:
type: Exact
value: /health
backendRefs:
- name: health-check-service
port: 8080