AI 서비스를 운영하면서 모델 버전 업데이트, 프로ンプ트 변경, 시스템 개선 등을 이유로 디플로이먼트를 자주 변경해야 합니다. 이 과정에서 사용자에게 끊김 없는 서비스를 제공하면서 안전한 배포를 수행하는 것은 모든 개발자가 고민하는 문제입니다. 이 튜토리얼에서는 블루-그린 디플로이먼트(Blue-Green Deployment)를 AI 서비스에 적용하는 방법을 심층적으로 다룹니다.
저는 HolySheep AI에서 실제 프로덕션 환경에 블루-그린 디플로이먼트를 구현하면서 얻은 경험과 노하우를 공유하겠습니다.
서비스 비교: HolySheep AI vs 공식 API vs 기타 릴레이 서비스
| 비교 항목 | HolySheep AI | 공식 API | 기타 릴레이 서비스 |
|---|---|---|---|
| 결제 방식 | 로컬 결제 지원 (해외 신용카드 불필요) |
해외 신용카드 필수 | 다양함 |
| GPT-4.1 토큰당 비용 | $8/MTok | $8/MTok | $10-15/MTok |
| Claude Sonnet 4 가격 | $15/MTok | $15/MTok | $18-25/MTok |
| Gemini 2.5 Flash | $2.50/MTok | $2.50/MTok | $3-5/MTok |
| DeepSeek V3.2 | $0.42/MTok | 제한적 지원 | $0.50-0.80/MTok |
| 평균 지연 시간 | 120-180ms | 100-150ms | 200-500ms |
| 무료 크레딧 | ✅ 가입 시 제공 | ❌ 없음 | 다양함 |
| 단일 API 키 | ✅ 모든 모델 통합 | ❌ 모델별 별도 | 부분 지원 |
블루-그린 디플로이먼트란?
블루-그린 디플로이먼트는 두 개의 동일한 환경(블루와 그린)을 교대로 사용하는 배포 전략입니다. 새 버전을 그린 환경에 배포하고 검증한 후, 로드밸런서를 통해 트래픽을 전환합니다. 문제가 발생하면 즉시 이전 환경으로 롤백할 수 있습니다.
┌─────────────────────────────────────────────────────────────┐
│ 로드밸런서 / 라우터 │
│ (사용자 요청을 분배하는 핵심 컴포넌트) │
└─────────────────────────────────────────────────────────────┘
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ 블루 환경 │ │ 그린 환경 │
│ (Production)│ │ (Staging) │
│ │ │ │
│ HolySheep API│ │ HolySheep API│
│ (GPT-4.1) │ │ (Claude 3.5) │
│ │ │ │
└─────────────┘ └─────────────┘
│ │
└──────────────────────┘
│
▼
┌─────────────────┐
│ HolySheep AI │
│ API Gateway │
└─────────────────┘
AI 서비스 블루-그린 디플로이먼트 아키텍처
1. 기본 구조 설정
# AI 서비스 블루-그린 디플로이먼트 기본 구조
HolySheep AI를 기반으로 한 구현 예시
import os
from enum import Enum
from typing import Optional
from dataclasses import dataclass
import httpx
import asyncio
class Environment(Enum):
"""디플로이먼트 환경 상태"""
BLUE = "blue" # 현재 운영 환경
GREEN = "green" # 새 버전 배포 환경
@dataclass
class AIEndpoint:
"""AI 서비스 엔드포인트 정보"""
environment: Environment
model: str
api_url: str
weight: int # 트래픽 가중치 (0-100)
is_healthy: bool
last_response_time: float
class BlueGreenRouter:
"""
블루-그린 디플로이먼트 라우터
HolySheep AI API를 활용하여 AI 서비스의 트래픽을 관리합니다.
"""
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://api.holysheep.ai/v1"
self.environments = {
Environment.BLUE: AIEndpoint(
environment=Environment.BLUE,
model="gpt-4.1",
api_url=f"{self.base_url}/chat/completions",
weight=100,
is_healthy=True,
last_response_time=0
),
Environment.GREEN: AIEndpoint(
environment=Environment.GREEN,
model="claude-sonnet-4-20250514",
api_url=f"{self.base_url}/chat/completions",
weight=0,
is_healthy=True,
last_response_time=0
)
}
self.active_environment = Environment.BLUE
self.canary_percentage = 0 # 카나리 배포 비율
async def route_request(self, messages: list, system_prompt: str) -> dict:
"""요청을 현재 활성 환경으로 라우팅"""
active = self.environments[self.active_environment]
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
payload = {
"model": active.model,
"messages": [
{"role": "system", "content": system_prompt},
*messages
],
"temperature": 0.7,
"max_tokens": 2000
}
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
active.api_url,
headers=headers,
json=payload
)
response.raise_for_status()
# 응답 시간 기록
active.last_response_time = response.elapsed.total_seconds() * 1000
return response.json()
def switch_environment(self, target: Environment, canary: int = 0):
"""
환경 전환 실행
canary: 카나리 배포 비율 (0-100)
"""
if canary > 0:
# 카나리 배포 모드
self.canary_percentage = canary
self.environments[target].weight = canary
self.environments[self.active_environment].weight = 100 - canary
print(f"카나리 배포 시작: {canary}% 트래픽을 {target.value} 환경으로 전환")
else:
# 완전한 환경 전환
self.active_environment = target
for env in self.environments.values():
env.weight = 100 if env.environment == target else 0
print(f"환경 전환 완료: {target.value} 환경이 활성화됨")
사용 예시
router = BlueGreenRouter(api_key="YOUR_HOLYSHEEP_API_KEY")
2. 카나리 배포 및 자동 롤백 시스템
# 카나리 배포 및 자동 롤백 시스템 구현
HolySheep AI 모니터링 기반 실시간 트래픽 조정
import time
from typing import Callable, Optional
from dataclasses import dataclass, field
from collections import deque
import statistics
@dataclass
class DeploymentMetrics:
"""배포 메트릭 수집"""
success_count: int = 0
failure_count: int = 0
total_requests: int = 0
response_times: deque = field(default_factory=lambda: deque(maxlen=100))
error_messages: list = field(default_factory=list)
@property
def success_rate(self) -> float:
if self.total_requests == 0:
return 100.0
return (self.success_count / self.total_requests) * 100
@property
def avg_response_time(self) -> float:
if not self.response_times:
return 0.0
return statistics.mean(self.response_times)
@property
def p95_response_time(self) -> float:
if len(self.response_times) < 2:
return 0.0
sorted_times = sorted(self.response_times)
index = int(len(sorted_times) * 0.95)
return sorted_times[index]
class CanaryDeployer:
"""
카나리 배포 및 자동 롤백 관리자
HolySheep AI 환경에서 안전하게 AI 모델 전환을 수행합니다.
"""
def __init__(
self,
router: BlueGreenRouter,
success_threshold: float = 95.0,
max_response_time: float = 5000.0,
rollback_threshold: float = 90.0
):
self.router = router
self.success_threshold = success_threshold
self.max_response_time = max_response_time
self.rollback_threshold = rollback_threshold
self.metrics = {
Environment.BLUE: DeploymentMetrics(),
Environment.GREEN: DeploymentMetrics()
}
self.rollback_callbacks: list[Callable] = []
def register_rollback_callback(self, callback: Callable):
"""롤백 시 실행할 콜백 등록"""
self.rollback_callbacks.append(callback)
async def execute_canary_deployment(
self,
target_env: Environment,
initial_percentage: int = 10,
increment_percentage: int = 10,
evaluation_interval: int = 60
) -> bool:
"""
카나리 배포 실행
Args:
target_env: 배포 대상 환경
initial_percentage: 초기 트래픽 비율
increment_percentage: 증가幅度
evaluation_interval: 평가 간격 (초)
Returns:
bool: 배포 성공 여부
"""
print(f"카나리 배포 시작: {target_env.value} 환경")
print(f"초기 트래픽: {initial_percentage}%")
current_percentage = initial_percentage
base_env = Environment.BLUE if target_env == Environment.GREEN else Environment.GREEN
while current_percentage <= 100:
# 1단계: 트래픽 전환
self.router.switch_environment(target_env, canary=current_percentage)
print(f"\n[{time.strftime('%H:%M:%S')}] {current_percentage}% 트래픽 전환")
# 2단계: 메트릭 수집 대기
await asyncio.sleep(evaluation_interval)
# 3단계: 메트릭 분석
target_metrics = self.metrics[target_env]
base_metrics = self.metrics[base_env]
print(f" 성공률: {target_metrics.success_rate:.2f}%")
print(f" 평균 응답시간: {target_metrics.avg_response_time:.2f}ms")
print(f" P95 응답시간: {target_metrics.p95_response_time:.2f}ms")
# 4단계: 자동 롤백 판단
if self._should_rollback(target_metrics):
print(f"\n⚠️ 롤백 임계값 도달! {base_env.value} 환경으로 복귀")
await self._execute_rollback(target_env, base_env)
return False
# 5단계: 카나리 비율 증가
if current_percentage < 100:
current_percentage = min(current_percentage + increment_percentage, 100)
# 메트릭 초기화
self.metrics[target_env] = DeploymentMetrics()
# 6단계: 완전한 전환
self.router.switch_environment(target_env, canary=0)
print(f"\n✅ 카나리 배포 완료: {target_env.value} 환경이正式 운영 환경이 됨")
return True
def _should_rollback(self, metrics: DeploymentMetrics) -> bool:
"""롤백 필요 여부 판단"""
# 성공률 체크
if metrics.success_rate < self.rollback_threshold:
return True
# 응답 시간 체크
if metrics.p95_response_time > self.max_response_time:
return True
return False
async def _execute_rollback(self, failed_env: Environment, safe_env: Environment):
"""롤백 실행"""
self.router.switch_environment(safe_env, canary=0)
# 롤백 콜백 실행
for callback in self.rollback_callbacks:
try:
await callback(failed_env, safe_env)
except Exception as e:
print(f"롤백 콜백 실행 실패: {e}")
# 실패 환경 메트릭 초기화
self.metrics[failed_env] = DeploymentMetrics()
사용 예시
deployer = CanaryDeployer(
router=router,
success_threshold=95.0,
max_response_time=5000.0,
rollback_threshold=90.0
)
롤백 알림 콜백 등록
async def notify_rollback(failed: Environment, safe: Environment):
print(f"Slack/이메일/Webhook으로 롤백 알림 전송: {failed.value} → {safe.value}")
deployer.register_rollback_callback(notify_rollback)
3. HolySheep AI 다중 모델 블루-그린 전략
# HolySheep AI에서 제공하는 다중 모델을 활용한 블루-그린 디플로이먼트
모델별 비용 최적화와 트래픽 분산 전략
from typing import Dict, List
from dataclasses import dataclass
import hashlib
@dataclass
class ModelConfig:
"""모델별 설정"""
name: str
display_name: str
cost_per_mtok: float # 달러
avg_latency_ms: float
quality_score: float # 0-10
max_tokens: int
class MultiModelBlueGreen:
"""
HolySheep AI 다중 모델 블루-그린 디플로이먼트
비용 효율성과 품질 균형을 자동으로 최적화합니다.
"""
# HolySheep AI에서 사용 가능한 모델 목록 및 비용
AVAILABLE_MODELS = {
"gpt-4.1": ModelConfig(
name="gpt-4.1",
display_name="GPT-4.1",
cost_per_mtok=8.00,
avg_latency_ms=150,
quality_score=9.5,
max_tokens=128000
),
"claude-sonnet-4-20250514": ModelConfig(
name="claude-sonnet-4-20250514",
display_name="Claude Sonnet 4",
cost_per_mtok=15.00,
avg_latency_ms=180,
quality_score=9.5,
max_tokens=200000
),
"gemini-2.5-flash": ModelConfig(
name="gemini-2.5-flash",
display_name="Gemini 2.5 Flash",
cost_per_mtok=2.50,
avg_latency_ms=120,
quality_score=8.5,
max_tokens=1000000
),
"deepseek-v3.2": ModelConfig(
name="deepseek-v3.2",
display_name="DeepSeek V3.2",
cost_per_mtok=0.42,
avg_latency_ms=140,
quality_score=8.0,
max_tokens=64000
)
}
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://api.holysheep.ai/v1"
self.model_weights: Dict[str, int] = {
"gpt-4.1": 50,
"gemini-2.5-flash": 50
}
def select_model_by_request(
self,
prompt_length: int,
required_quality: float,
budget_constraint: float
) -> str:
"""
요청 특성에 따라 최적의 모델 선택
Args:
prompt_length: 프롬프트 토큰 수
required_quality: 필요한 품질 수준 (0-10)
budget_constraint: 예산 제약 (달러)
Returns:
선택된 모델 이름
"""
candidates = []
for model_name, config in self.AVAILABLE_MODELS.items():
# 품질 체크
if config.quality_score < required_quality:
continue
# 비용 체크
estimated_cost = (prompt_length + config.max_tokens) * config.cost_per_mtok / 1_000_000
if estimated_cost > budget_constraint:
continue
# 점수 계산 (품질/비용 비율)
efficiency = config.quality_score / config.cost_per_mtok
candidates.append((model_name, efficiency, config))
if not candidates:
# 가장 저렴한 모델 폴백
return "deepseek-v3.2"
# 효율성 기반 정렬
candidates.sort(key=lambda x: x[1], reverse=True)
# 로깅
selected = candidates[0]
print(f"모델 선택: {selected[0]} (효율성: {selected[1]:.2f}, "
f"품질: {selected[2].quality_score}, "
f"비용: ${selected[2].cost_per_mtok}/MTok)")
return selected[0]
async def weighted_model_request(
self,
messages: list,
task_type: str = "general"
) -> dict:
"""
가중치 기반 다중 모델 요청
요청의 해시를 기반으로 일관된 모델 선택 보장
"""
# 요청 해시 생성 (같은 요청은 같은 모델로)
request_hash = hashlib.md5(
str(messages).encode()
).hexdigest()
# 해시 기반 모델 선택
hash_value = int(request_hash[:8], 16)
model_names = list(self.model_weights.keys())
cumulative_weight = sum(self.model_weights.values())
selected_index = hash_value % cumulative_weight
cumulative = 0
for i, model_name in enumerate(model_names):
cumulative += self.model_weights[model_name]
if selected_index < cumulative:
selected_model = model_name
break
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
payload = {
"model": selected_model,
"messages": messages,
"temperature": 0.7,
"max_tokens": 2000
}
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=payload
)
result = response.json()
result["selected_model"] = selected_model
return result
def update_model_weights(self, performance_data: Dict[str, dict]):
"""
성능 데이터 기반 모델 가중치 자동 조정
HolySheep AI 대시보드에서 수집한 데이터 활용
"""
for model, metrics in performance_data.items():
success_rate = metrics.get("success_rate", 95)
avg_latency = metrics.get("avg_latency_ms", 150)
# 성능 점수 계산
latency_score = max(0, 100 - (avg_latency / 3))
success_score = success_rate
performance_score = (latency_score * 0.4) + (success_score * 0.6)
# 가중치 업데이트 (단순화된 로직)
current_weight = self.model_weights.get(model, 0)
new_weight = int(current_weight * 0.7 + performance_score * 0.3)
self.model_weights[model] = max(1, min(100, new_weight))
# 정규화
total = sum(self.model_weights.values())
if total != 100:
factor = 100 / total
for model in self.model_weights:
self.model_weights[model] = int(self.model_weights[model] * factor)
print(f"모델 가중치 업데이트: {self.model_weights}")
HolySheep AI 다중 모델 활용 예시
multi_model = MultiModelBlueGreen(api_key="YOUR_HOLYSHEEP_API_KEY")
모델 선택 예시
selected = multi_model.select_model_by_request(
prompt_length=500,
required_quality=8.0,
budget_constraint=0.05
)
print(f"선택된 모델: {selected}")
실전 모니터링 및 알림 시스템
# HolySheep AI 블루-그린 디플로이먼트 모니터링 시스템
실시간 메트릭 추적 및 이상 탐지
import asyncio
from typing import Dict, List
from dataclasses import dataclass
from datetime import datetime
import json
@dataclass
class AlertRule:
"""알림 규칙"""
name: str
condition: str # "gt", "lt", "eq"
threshold: float
severity: str # "info", "warning", "critical"
cooldown_seconds: int = 300
class DeploymentMonitor:
"""
블루-그린 디플로이먼트 실시간 모니터
HolySheep AI API 응답을 추적하고 문제를 즉시 감지합니다.
"""
def __init__(self, deployer: CanaryDeployer):
self.deployer = deployer
self.alert_rules: List[AlertRule] = []
self.alert_history: List[dict] = []
self.monitoring_active = False
def add_alert_rule(
self,
name: str,
condition: str,
threshold: float,
severity: str = "warning",
cooldown: int = 300
):
"""알림 규칙 추가"""
rule = AlertRule(
name=name,
condition=condition,
threshold=threshold,
severity=severity,
cooldown_seconds=cooldown
)
self.alert_rules.append(rule)
print(f"알림 규칙 추가: {name} (조건: {condition} {threshold})")
async def start_monitoring(self, interval: int = 10):
"""
모니터링 루프 시작
Args:
interval: 모니터링 간격 (초)
"""
self.monitoring_active = True
print(f"모니터링 시작: {interval}초 간격")
while self.monitoring_active:
try:
await self._check_metrics()
await self._evaluate_alerts()
except Exception as e:
print(f"모니터링 오류: {e}")
await asyncio.sleep(interval)
def stop_monitoring(self):
"""모니터링 중지"""
self.monitoring_active = False
print("모니터링 중지")
async def _check_metrics(self):
"""메트릭 상태 확인"""
for env, metrics in self.deployer.metrics.items():
status = "✅" if metrics.success_rate >= 95 else "⚠️"
print(f"{status} [{env.value}] "
f"성공률: {metrics.success_rate:.1f}% | "
f"평균응답: {metrics.avg_response_time:.0f}ms | "
f"P95: {metrics.p95_response_time:.0f}ms")
async def _evaluate_alerts(self):
"""알림 조건 평가"""
for env, metrics in self.deployer.metrics.items():
for rule in self.alert_rules:
# 쿨다운 체크
if self._is_in_cooldown(rule.name, env):
continue
# 조건 평가
value = self._get_metric_value(metrics, rule.name)
triggered = self._evaluate_condition(value, rule.condition, rule.threshold)
if triggered:
await self._send_alert(env, rule, value)
def _get_metric_value(self, metrics: DeploymentMetrics, metric_name: str) -> float:
"""메트릭 값 추출"""
mapping = {
"success_rate": metrics.success_rate,
"avg_response_time": metrics.avg_response_time,
"p95_response_time": metrics.p95_response_time,
"failure_count": metrics.failure_count
}
return mapping.get(metric_name, 0.0)
def _evaluate_condition(self, value: float, condition: str, threshold: float) -> bool:
"""조건 평가"""
if condition == "gt":
return value > threshold
elif condition == "lt":
return value < threshold
elif condition == "eq":
return abs(value - threshold) < 0.01
return False
def _is_in_cooldown(self, rule_name: str, env: Environment) -> bool:
"""쿨다운 상태 확인"""
now = datetime.now()
for alert in self.alert_history:
if alert["rule_name"] == rule_name and alert["environment"] == env.value:
elapsed = (now - datetime.fromisoformat(alert["timestamp"])).total_seconds()
if elapsed < alert["cooldown"]:
return True
return False
async def _send_alert(self, env: Environment, rule: AlertRule, value: float):
"""알림 전송"""
alert = {
"timestamp": datetime.now().isoformat(),
"rule_name": rule.name,
"environment": env.value,
"severity": rule.severity,
"value": value,
"threshold": rule.threshold,
"cooldown": rule.cooldown_seconds
}
self.alert_history.append(alert)
# 알림等级별 처리
severity_icon = {"info": "ℹ️", "warning": "⚠️", "critical": "🚨"}
icon = severity_icon.get(rule.severity, "❓")
print(f"\n{icon} [{rule.severity.upper()}] {rule.name}")
print(f" 환경: {env.value}")
print(f" 현재값: {value:.2f} (임계값: {rule.threshold})")
# 심각한 알림시 자동 조치
if rule.severity == "critical":
print(f" ⚡ 자동 롤백 트리거!")
# 실제 환경에서는 자동 롤백 실행
모니터링 설정 예시
monitor = DeploymentMonitor(deployer)
알림 규칙 설정
monitor.add_alert_rule("success_rate_low", "lt", 90.0, "critical", cooldown=60)
monitor.add_alert_rule("response_time_high", "gt", 3000.0, "warning", cooldown=300)
monitor.add_alert_rule("p95_latency_critical", "gt", 5000.0, "critical", cooldown=120)
모니터링 시작
asyncio.create_task(monitor.start_monitoring(interval=10))
HolySheep AI 블루-그린 디플로이먼트 CI/CD 통합
# GitHub Actions 워크플로우 예시
HolySheep AI를 활용한 블루-그린 디플로이먼트 자동화
name: AI Service Blue-Green Deployment
on:
push:
branches:
- main
workflow_dispatch:
inputs:
deployment_type:
description: 'Deployment Type'
required: true
default: 'canary'
type: choice
options:
- canary
- blue-green
- rollback
env:
HOLYSHEEP_API_KEY: ${{ secrets.HOLYSHEEP_API_KEY }}
DEPLOYMENT_API_URL: https://api.holysheep.ai/v1
jobs:
# 1단계: 사전 검증
pre-deployment-checks:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install httpx asyncio pytest
- name: Validate API Key
run: |
python -c "
import httpx
import os
response = httpx.get(
'${{ env.DEPLOYMENT_API_URL }}/models',
headers={'Authorization': f'Bearer {os.environ[\"HOLYSHEEP_API_KEY\"]}'}
)
if response.status_code == 200:
models = response.json().get('data', [])
print(f'✅ API 연결 성공: {len(models)}개 모델 사용 가능')
else:
print(f'❌ API 오류: {response.status_code}')
exit(1)
"
- name: Check model availability
run: |
python -c "
import httpx
import json
response = httpx.get(
'${{ env.DEPLOYMENT_API_URL }}/models',
headers={'Authorization': f'Bearer {os.environ[\"HOLYSHEEP_API_KEY\"]}'}
)
models = response.json().get('data', [])
model_ids = [m['id'] for m in models]
required = ['gpt-4.1', 'claude-sonnet-4-20250514', 'gemini-2.5-flash']
for model in required:
status = '✅' if model in model_ids else '❌'
print(f'{status} {model}')
missing = [m for m in required if m not in model_ids]
if missing:
print(f'\\n⚠️ 일부 모델 사용 불가: {missing}')
"
- name: Run integration tests
run: |
pytest tests/ -v --tb=short
# 2단계: 카나리 배포
canary-deployment:
needs: pre-deployment-checks
if: github.event.inputs.deployment_type == 'canary' || github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Run Blue-Green Deployment Script
run: |
python << 'EOF'
import httpx
import asyncio
import os
API_KEY = os.environ['HOLYSHEEP_API_KEY']
BASE_URL = 'https://api.holysheep.ai/v1'
async def deploy_canary():
print('🚀 카나리 배포 시작')
# 1단계: 10% 트래픽
print('📊 1단계: 10% 트래픽 테스트')
await simulate_traffic(10, duration=60)
# 2단계: 30% 트래픽
print('📊 2단계: 30% 트래픽 테스트')
await simulate_traffic(30, duration=120)
# 3단계: 50% 트래픽
print('📊 3단계: 50% 트래픽 테스트')
await simulate_traffic(50, duration=120)
# 4단계: 100% 트래픽
print('📊 4단계: 100% 트래픽 전환')
await simulate_traffic(100, duration=60)
print('✅ 카나리 배포 완료')
async def simulate_traffic(percentage, duration):
"""트래픽 시뮬레이션"""
headers = {
'Authorization': f'Bearer {API_KEY}',
'Content-Type': 'application/json'
}
payload = {
'model': 'claude-sonnet-4-20250514',
'messages': [
{'role': 'user', 'content': '안녕하세요, 테스트 메시지입니다.'}
],
'temperature': 0.7,
'max_tokens': 100
}
success_count = 0
total_latency = 0
async with httpx.AsyncClient(timeout=30.0) as client:
start = asyncio.get_event_loop().time()
for i in range(10): # 10개 요청
try:
resp = await client.post(
f'{BASE_URL}/chat/completions',
headers=headers,
json=payload
)
if resp.status_code == 200:
success_count += 1
total_latency += resp.elapsed.total_seconds() * 1000
else:
print(f' ⚠️ 요청 {i+1} 실패: {resp.status_code}')
except Exception as e:
print(f' ❌ 요청 {i+1} 오류: {e}')
await asyncio.sleep(duration / 10)
elapsed = asyncio.get_event_loop().time() - start
success_rate = (success_count / 10) * 100
avg_latency = total_latency / success_count if success_count > 0 else 0
print(f' 성공률: {success_rate:.0f}% | 평균 지연: {avg_latency:.0f}ms')
asyncio.run(deploy_canary())
EOF
- name: Health check after deployment
run: |
python -c "
import httpx
import time
# 3번的健康检查
for i in range(3):
response = httpx.get('https://your-service.com/health')
if response.status_code == 200:
print(f'✅ Health check {i+1}/3 통과')
else:
print(f'❌ Health check {i+1}/3 실패')
exit(1)
time.sleep(5)
"
# 3단계: 롤백 (필요시)
rollback:
needs: pre-deployment-checks
if: github.event.inputs.deployment_type == 'rollback'
runs-on: ubuntu-latest
steps:
- name: Execute Rollback
run: |
echo "🔄 롤백 실행: GREEN → BLUE 환경으로 전환"
# 실제 환경에서는 인프라별 롤백 명령 실행
# kubectl rollout undo deployment/ai-service
# aws elb deregister-targets --target-group-arn $TG_ARN --targets $OLD_TARGET
자주 발생하는 오류와 해결책
1. API 키 인증 오류 (401 Unauthorized)
# 오류 메시지
{"error": {"message": "Incorrect API key provided", "type": "invalid_request_error"}}
해결 방법
import httpx
import os
❌ 잘못된 방식 - 환경 변수 직접 참조
response = httpx.post(url, headers={"Authorization": f"Bearer {os.getenv('API_KEY')}"})
✅ 올바른 방식 - 명시적 환경 변수 설정
API_KEY = os.environ.get("HOLYSHEEP_API_KEY",