Diagrams로 그리는 쿠버네티스 Agentic RAG 아키텍처 - Python 코드로 시스템 설계하기
들어가며
시스템 아키텍처를 설계할 때 복잡한 그래픽 툴을 사용하거나 손으로 그리는 것에 지치셨나요? Diagrams는 Python 코드로 클라우드 시스템 아키텍처를 그릴 수 있는 혁신적인 라이브러리입니다.
“Diagram as Code” 개념을 구현한 Diagrams는 단순히 그림을 그리는 도구를 넘어서, 아키텍처 변경사항을 버전 관리 시스템으로 추적할 수 있게 해줍니다. 특히 AWS, Azure, GCP, Kubernetes 등 주요 클라우드 프로바이더를 지원하여 현대적인 클라우드 네이티브 시스템 설계에 최적화되어 있습니다.
이 글에서는 Diagrams의 기본 사용법부터 쿠버네티스 환경에서 Agentic RAG 시스템을 설계하는 실전 예제까지 상세히 다루어보겠습니다.
Diagrams 라이브러리 소개
핵심 특징
Diagram as Code의 강력함:
- 🐍 Python 코드로 아키텍처 다이어그램 생성
- 📈 버전 관리 시스템과 완벽 통합
- ☁️ 주요 클라우드 프로바이더 지원 (AWS, Azure, GCP, Kubernetes 등)
- 🔄 자동화된 문서화 워크플로우 구축 가능
- 🎨 프로그래밍 방식의 유연한 커스터마이징
지원 프로바이더
Diagrams는 다음과 같은 광범위한 프로바이더를 지원합니다:
- 클라우드: AWS, Azure, GCP, IBM Cloud, Alibaba Cloud, Oracle Cloud
- 컨테이너: Kubernetes, Docker
- 온프레미스: 다양한 온프레미스 솔루션
- SaaS: 주요 SaaS 서비스들
- 프로그래밍: 개발 프레임워크 및 언어들
프로젝트 통계
- ⭐ 41.1k 스타 (GitHub에서 높은 인기)
- 🍴 2.6k 포크 (활발한 커뮤니티)
- 📜 MIT 라이선스 (상업적 사용 가능)
- 👥 167명의 컨트리뷰터 (지속적인 개발)
설치 및 환경 설정
시스템 요구사항
- Python 3.9 이상
- Graphviz (다이어그램 렌더링 엔진)
설치 과정
# macOS에서 Graphviz 설치 (Homebrew 사용)
brew install graphviz
# Diagrams 라이브러리 설치
pip install diagrams
# 추가 의존성 (선택사항)
pip install pillow # 이미지 처리 개선
설치 확인
# test_diagrams.py
from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
# 간단한 테스트 다이어그램
with Diagram("My First Diagram", show=False):
web_server = EC2("Web Server")
database = RDS("Database")
web_server >> database
print("✅ Diagrams 설치 완료!")
기본 사용법
1. 기본 구조
from diagrams import Diagram, Edge
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
with Diagram("Basic Web Service", show=False, direction="TB"):
load_balancer = ELB("Load Balancer")
web_servers = [EC2(f"Web Server {i}") for i in range(1, 4)]
load_balancer >> web_servers
2. 방향 및 스타일 설정
# 다양한 다이어그램 방향
with Diagram("Horizontal", direction="LR"): # Left to Right
pass
with Diagram("Vertical", direction="TB"): # Top to Bottom
pass
# 커스텀 스타일
with Diagram("Custom Style",
show=False,
graph_attr={"fontsize": "20", "bgcolor": "white"}):
pass
3. 클러스터링
from diagrams import Cluster
with Diagram("Clustered Architecture", show=False):
with Cluster("Web Services"):
web_svcs = [EC2(f"Web {i}") for i in range(3)]
with Cluster("Database Cluster"):
db_primary = RDS("Primary")
db_replica = RDS("Replica")
## 쿠버네티스 Agentic RAG 시스템 아키텍처 설계
이제 Diagrams를 활용하여 **쿠버네티스 환경에서 운영되는 Agentic RAG 시스템**의 아키텍처를 설계해보겠습니다.
### 시스템 요구사항
우리가 설계할 Agentic RAG 시스템은 다음과 같은 구성 요소를 포함합니다:
- **프론트엔드**: 사용자 인터페이스
- **API Gateway**: 트래픽 라우팅 및 보안
- **Agentic RAG 서비스**: 핵심 추론 및 검색 로직
- **벡터 데이터베이스**: 임베딩 저장소 (Weaviate)
- **그래프 데이터베이스**: 지식 그래프 (Neo4j)
- **LLM 서비스**: 언어 모델 추론
- **모니터링**: 시스템 관측성
### 기본 Agentic RAG 아키텍처
```python
from diagrams import Diagram, Cluster, Edge
from diagrams.k8s.compute import Pod, ReplicaSet
from diagrams.k8s.network import Service, Ingress
from diagrams.k8s.storage import PersistentVolume, PersistentVolumeClaim
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.monitoring import Prometheus, Grafana
from diagrams.programming.framework import React
def create_basic_agentic_rag():
with Diagram("Kubernetes Agentic RAG System",
show=False,
direction="TB",
filename="basic_agentic_rag"):
# 사용자 계층
users = React("Frontend\n(React)")
# 쿠버네티스 인그레스
ingress = Ingress("Ingress Controller")
# API Gateway 클러스터
with Cluster("API Gateway"):
api_gateway = Service("Gateway Service")
gateway_pods = [Pod(f"Gateway Pod {i}") for i in range(1, 3)]
# Agentic RAG 서비스 클러스터
with Cluster("Agentic RAG Services"):
rag_service = Service("RAG Service")
rag_pods = [Pod(f"RAG Pod {i}") for i in range(1, 4)]
# Agent 구성 요소들
reasoning_service = Service("Reasoning Agent")
search_service = Service("Search Agent")
synthesis_service = Service("Synthesis Agent")
# 데이터 계층 클러스터
with Cluster("Data Layer"):
# 벡터 데이터베이스
vector_db_svc = Service("Weaviate Service")
vector_db = Pod("Weaviate Pod")
vector_pv = PersistentVolume("Vector DB Storage")
# 그래프 데이터베이스
graph_db_svc = Service("Neo4j Service")
graph_db = Pod("Neo4j Pod")
graph_pv = PersistentVolume("Graph DB Storage")
# 캐시
cache_svc = Service("Redis Service")
cache_pod = Pod("Redis Pod")
# LLM 서비스 클러스터
with Cluster("LLM Services"):
llm_service = Service("LLM Service")
llm_pods = [Pod(f"LLM Pod {i}") for i in range(1, 3)]
# 모니터링 클러스터
with Cluster("Monitoring"):
prometheus = Prometheus("Prometheus")
grafana = Grafana("Grafana")
# 연결 관계 정의
users >> Edge(label="HTTPS") >> ingress
ingress >> api_gateway
api_gateway >> gateway_pods
gateway_pods >> rag_service
rag_service >> rag_pods
# Agentic 워크플로우
rag_pods >> reasoning_service >> search_service >> synthesis_service
# 데이터 접근
search_service >> vector_db_svc >> vector_db >> vector_pv
reasoning_service >> graph_db_svc >> graph_db >> graph_pv
synthesis_service >> cache_svc >> cache_pod
# LLM 호출
synthesis_service >> llm_service >> llm_pods
# 모니터링
[rag_pods, vector_db, graph_db, llm_pods] >> prometheus
prometheus >> grafana
if __name__ == "__main__":
create_basic_agentic_rag()
고급 마이크로서비스 아키텍처
프로덕션 환경을 위한 더 상세한 아키텍처를 설계해보겠습니다:
from diagrams.k8s.others import HorizontalPodAutoscaler
from diagrams.aws.storage import S3
from diagrams.onprem.queue import Kafka
def create_advanced_agentic_rag():
with Diagram("Advanced Kubernetes Agentic RAG",
show=False,
direction="TB",
filename="advanced_agentic_rag"):
# 외부 서비스
users = React("Users")
s3_storage = S3("Document Storage")
# 로드 밸런싱 및 보안
with Cluster("Load Balancing & Security"):
ingress = Ingress("NGINX Ingress")
api_gateway = Service("API Gateway")
gateway_hpa = HorizontalPodAutoscaler("Gateway HPA")
# Agentic RAG 마이크로서비스
with Cluster("Agentic RAG Microservices"):
# 메인 오케스트레이터
orchestrator_svc = Service("RAG Orchestrator")
# 전문화된 에이전트들
with Cluster("Specialized Agents"):
query_agent = Service("Query Agent")
retrieval_agent = Service("Retrieval Agent")
reasoning_agent = Service("Reasoning Agent")
synthesis_agent = Service("Synthesis Agent")
agent_hpa = HorizontalPodAutoscaler("Agents HPA")
# 데이터 처리 파이프라인
with Cluster("Data Processing"):
kafka = Kafka("Message Queue")
stream_processor = Service("Stream Processor")
# 다중 데이터 저장소
with Cluster("Multi-Modal Data Stores"):
# 벡터 데이터베이스
weaviate_primary = Service("Weaviate Primary")
weaviate_replicas = [Service(f"Weaviate Replica {i}") for i in range(2)]
# 그래프 데이터베이스
neo4j_cluster = Service("Neo4j Cluster")
# 캐싱 계층
redis_cluster = Service("Redis Cluster")
# LLM 서비스 클러스터
with Cluster("Multi-LLM Services"):
llm_gateway = Service("LLM Gateway")
reasoning_llm = Service("Reasoning LLM")
synthesis_llm = Service("Synthesis LLM")
embedding_service = Service("Embedding Service")
# 모니터링 스택
with Cluster("Observability"):
prometheus = Prometheus("Prometheus")
grafana = Grafana("Grafana")
jaeger = Service("Jaeger Tracing")
# 연결 관계
users >> ingress >> api_gateway >> gateway_hpa
gateway_hpa >> orchestrator_svc
# Agentic 워크플로우
orchestrator_svc >> query_agent >> retrieval_agent
retrieval_agent >> reasoning_agent >> synthesis_agent
# 데이터 플로우
s3_storage >> kafka >> stream_processor
# 데이터 접근
retrieval_agent >> weaviate_primary >> weaviate_replicas
reasoning_agent >> neo4j_cluster
synthesis_agent >> redis_cluster
# LLM 연결
reasoning_agent >> reasoning_llm
synthesis_agent >> synthesis_llm
retrieval_agent >> embedding_service
# 모니터링
[orchestrator_svc, query_agent, reasoning_llm] >> prometheus
prometheus >> grafana
[orchestrator_svc, query_agent] >> jaeger
if __name__ == "__main__":
create_advanced_agentic_rag()
실제 테스트 및 실행
개발환경 정보
테스트 환경:
- 운영체제: macOS Sequoia 15.0 (Darwin 25.0.0)
- Python: 3.12.3
- Diagrams: 0.24.4 (최신 버전)
- Graphviz: 12.2.0
테스트 스크립트 실행
# 테스트 디렉토리 생성
mkdir diagrams_test && cd diagrams_test
# 가상환경 설정
python -m venv venv
source venv/bin/activate
# 필요한 패키지 설치
pip install diagrams pillow
테스트 실행을 위한 통합 스크립트:
# create_all_diagrams.py
import os
def main():
"""모든 다이어그램을 생성하는 메인 함수"""
print("🎨 Diagrams 테스트 시작...")
# 1. 기본 Agentic RAG 아키텍처
create_basic_agentic_rag()
print("✅ 기본 Agentic RAG 아키텍처 생성 완료")
# 2. 고급 Agentic RAG 아키텍처
create_advanced_agentic_rag()
print("✅ 고급 Agentic RAG 아키텍처 생성 완료")
print("\n🎉 모든 다이어그램 생성 완료!")
print("생성된 파일들:")
for file in os.listdir('.'):
if file.endswith('.png'):
print(f" 📊 {file}")
if __name__ == "__main__":
main()
실행 결과
$ python test_diagrams.py
🎨 Diagrams 테스트 시작...
✅ 기본 Agentic RAG 아키텍처 생성 완료
✅ 고급 Agentic RAG 아키텍처 생성 완료
🎉 모든 다이어그램 생성 완료!
생성된 파일들:
📊 basic_agentic_rag.png
📊 advanced_agentic_rag.png
$ ls -la *.png
-rw-r--r--@ 1 user staff 215008 Jun 28 14:45 advanced_agentic_rag.png
-rw-r--r--@ 1 user staff 200506 Jun 28 14:45 basic_agentic_rag.png
설치 검증 결과:
- ✅ Graphviz 13.0.1 설치 성공
- ✅ Diagrams 0.24.4 정상 작동
- ✅ 쿠버네티스 노드 렌더링 성공
- ✅ 복합 클러스터 구조 생성 완료
- ✅ 다이어그램 파일 생성 확인 (총 415KB)
고급 활용 방법
1. CI/CD 통합
# .github/workflows/generate-diagrams.yml
name: Generate Architecture Diagrams
on:
push:
paths:
- 'diagrams/**'
jobs:
generate-diagrams:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install Graphviz
run: sudo apt-get update && sudo apt-get install -y graphviz
- name: Install Dependencies
run: pip install diagrams
- name: Generate Diagrams
run: |
cd diagrams
python generate_all.py
- name: Commit Generated Diagrams
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add *.png
git commit -m "Update architecture diagrams" || exit 0
git push
2. 동적 아키텍처 생성
# dynamic_architecture.py
import json
from diagrams import Diagram, Cluster
from diagrams.k8s.compute import Pod
from diagrams.k8s.network import Service
def create_from_config(config_file):
"""설정 파일을 기반으로 동적 아키텍처 생성"""
with open(config_file, 'r') as f:
config = json.load(f)
with Diagram(config['name'], show=False):
services = {}
# 서비스 생성
for service_config in config['services']:
if service_config.get('cluster'):
with Cluster(service_config['cluster']):
services[service_config['name']] = Service(service_config['name'])
else:
services[service_config['name']] = Service(service_config['name'])
# 연결 관계 설정
for connection in config.get('connections', []):
source = services[connection['from']]
target = services[connection['to']]
source >> target
3. 실제 프로덕션 사례
Apache Airflow의 활용: Apache Airflow는 Diagrams를 사용하여 공식 문서의 아키텍처 다이어그램을 생성합니다.
KubeDiagrams: Kubernetes 매니페스트 파일에서 아키텍처 다이어그램을 자동 생성하는 도구로 Diagrams를 활용합니다.
편의 기능 설정
Zsh Alias 설정
실제 테스트를 통해 검증된 편의 기능 설정:
$ chmod +x setup_diagrams_aliases.sh && ./setup_diagrams_aliases.sh
🎨 Diagrams 라이브러리 Zsh Alias 설정을 시작합니다...
✅ 기존 .zshrc 파일을 백업했습니다.
✅ Diagrams 작업 디렉토리를 생성했습니다: /Users/user/diagrams_workspace
✅ Zsh aliases가 ~/.zshrc에 추가되었습니다.
🎉 Diagrams 편의 기능 설정이 완료되었습니다!
📚 사용 가능한 명령어:
diagrams-create - 다이어그램 생성
diagrams-view - 생성된 PNG 파일 보기
diagrams-clean - PNG 파일 정리
diagrams-workspace - 작업 디렉토리로 이동
diagrams-init - 새 프로젝트 초기화
diagrams-examples - 사용 가능한 예제 목록
🔧 개발 도구:
diagrams-install - Diagrams 설치
diagrams-upgrade - Diagrams 업그레이드
diagrams-check - 버전 확인
설정된 Alias 목록:
diagrams-create
: 테스트 다이어그램 생성diagrams-view
: PNG 파일 자동 열기diagrams-clean
: 생성된 파일 정리diagrams-workspace
: 전용 작업 디렉토리로 이동diagrams-k8s-template
: Kubernetes 템플릿 생성diagrams-aws-template
: AWS 템플릿 생성
결론
Diagrams는 Python 코드로 시스템 아키텍처를 설계할 수 있는 강력한 도구입니다. 특히 쿠버네티스 환경에서 Agentic RAG 시스템과 같은 복잡한 마이크로서비스 아키텍처를 설계할 때 그 진가를 발휘합니다.
주요 장점
- 코드 기반 설계: 프로그래밍 방식으로 유연하고 재사용 가능한 다이어그램 생성
- 버전 관리: Git과 완벽 통합되어 아키텍처 변경사항 추적 가능
- 자동화: CI/CD 파이프라인에 통합하여 문서 자동 업데이트
- 확장성: 41.1k 스타를 받은 활발한 오픈소스 프로젝트
활용 분야
- 클라우드 네이티브 아키텍처: Kubernetes, 마이크로서비스, 컨테이너 오케스트레이션
- AI/ML 시스템: RAG, LLMOps, 데이터 파이프라인
- DevOps: CI/CD, 모니터링, 인프라 관리
- 기술 문서화: 아키텍처 문서, 시스템 설계서
Diagrams를 활용하면 복잡한 시스템 아키텍처를 명확하고 일관되게 표현할 수 있으며, 개발팀과 이해관계자들 간의 소통을 크게 개선할 수 있습니다. “Diagram as Code” 패러다임은 현대적인 소프트웨어 개발에서 필수적인 도구가 되어가고 있습니다.
## 쿠버네티스 Agentic RAG 시스템 아키텍처 설계
이제 Diagrams를 활용하여 **쿠버네티스 환경에서 운영되는 Agentic RAG 시스템**의 아키텍처를 설계해보겠습니다.
### 시스템 요구사항
우리가 설계할 Agentic RAG 시스템은 다음과 같은 구성 요소를 포함합니다:
- **프론트엔드**: 사용자 인터페이스
- **API Gateway**: 트래픽 라우팅 및 보안
- **Agentic RAG 서비스**: 핵심 추론 및 검색 로직
- **벡터 데이터베이스**: 임베딩 저장소 (Weaviate)
- **그래프 데이터베이스**: 지식 그래프 (Neo4j)
- **LLM 서비스**: 언어 모델 추론
- **모니터링**: 시스템 관측성
### 기본 Agentic RAG 아키텍처
```python
from diagrams import Diagram, Cluster, Edge
from diagrams.k8s.compute import Pod, ReplicaSet
from diagrams.k8s.network import Service, Ingress
from diagrams.k8s.storage import PersistentVolume, PersistentVolumeClaim
from diagrams.k8s.rbac import ServiceAccount
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.monitoring import Prometheus, Grafana
from diagrams.programming.framework import React
from diagrams.programming.language import Python
def create_basic_agentic_rag():
with Diagram("Kubernetes Agentic RAG System",
show=False,
direction="TB",
filename="basic_agentic_rag"):
# 사용자 계층
users = React("Frontend\n(React)")
# 쿠버네티스 인그레스
ingress = Ingress("Ingress Controller")
# API Gateway 클러스터
with Cluster("API Gateway"):
api_gateway = Service("Gateway Service")
gateway_pods = [Pod(f"Gateway Pod {i}") for i in range(1, 3)]
# Agentic RAG 서비스 클러스터
with Cluster("Agentic RAG Services"):
rag_service = Service("RAG Service")
rag_pods = [Pod(f"RAG Pod {i}") for i in range(1, 4)]
# Agent 구성 요소들
reasoning_service = Service("Reasoning Agent")
search_service = Service("Search Agent")
synthesis_service = Service("Synthesis Agent")
# 데이터 계층 클러스터
with Cluster("Data Layer"):
# 벡터 데이터베이스
vector_db_svc = Service("Weaviate Service")
vector_db = Pod("Weaviate Pod")
vector_pv = PersistentVolume("Vector DB Storage")
# 그래프 데이터베이스
graph_db_svc = Service("Neo4j Service")
graph_db = Pod("Neo4j Pod")
graph_pv = PersistentVolume("Graph DB Storage")
# 캐시
cache_svc = Service("Redis Service")
cache_pod = Pod("Redis Pod")
# LLM 서비스 클러스터
with Cluster("LLM Services"):
llm_service = Service("LLM Service")
llm_pods = [Pod(f"LLM Pod {i}") for i in range(1, 3)]
# 모니터링 클러스터
with Cluster("Monitoring"):
prometheus = Prometheus("Prometheus")
grafana = Grafana("Grafana")
# 연결 관계 정의
users >> Edge(label="HTTPS") >> ingress
ingress >> api_gateway
api_gateway >> gateway_pods
gateway_pods >> rag_service
rag_service >> rag_pods
# Agentic 워크플로우
rag_pods >> reasoning_service >> search_service >> synthesis_service
# 데이터 접근
search_service >> vector_db_svc >> vector_db >> vector_pv
reasoning_service >> graph_db_svc >> graph_db >> graph_pv
synthesis_service >> cache_svc >> cache_pod
# LLM 호출
synthesis_service >> llm_service >> llm_pods
# 모니터링
[rag_pods, vector_db, graph_db, llm_pods] >> prometheus
prometheus >> grafana
if __name__ == "__main__":
create_basic_agentic_rag()
고급 Agentic RAG 아키텍처
더 복잡한 프로덕션 환경을 위한 고급 아키텍처를 설계해보겠습니다:
from diagrams import Diagram, Cluster, Edge
from diagrams.k8s.compute import Pod, Deployment, Job
from diagrams.k8s.network import Service, Ingress, NetworkPolicy
from diagrams.k8s.storage import StorageClass, PersistentVolume
from diagrams.k8s.others import HorizontalPodAutoscaler
from diagrams.aws.storage import S3
from diagrams.onprem.queue import Kafka
from diagrams.onprem.workflow import Airflow
def create_advanced_agentic_rag():
with Diagram("Advanced Kubernetes Agentic RAG Architecture",
show=False,
direction="TB",
filename="advanced_agentic_rag"):
# 외부 서비스
users = React("Users")
s3_storage = S3("Document Storage")
# 보안 및 네트워킹
with Cluster("Security & Networking"):
ingress = Ingress("NGINX Ingress")
network_policy = NetworkPolicy("Network Policies")
service_account = ServiceAccount("Service Accounts")
# API Gateway & Load Balancing
with Cluster("API Gateway Layer"):
api_gateway = Service("API Gateway")
gateway_hpa = HorizontalPodAutoscaler("Gateway HPA")
gateway_pods = [Pod(f"Gateway-{i}") for i in range(3)]
# Agentic RAG Microservices
with Cluster("Agentic RAG Microservices"):
# 메인 오케스트레이터
orchestrator_svc = Service("RAG Orchestrator")
orchestrator_pods = [Pod(f"Orchestrator-{i}") for i in range(2)]
# 전문화된 에이전트들
with Cluster("Specialized Agents"):
query_agent = Service("Query Understanding Agent")
retrieval_agent = Service("Retrieval Agent")
reasoning_agent = Service("Reasoning Agent")
synthesis_agent = Service("Synthesis Agent")
validation_agent = Service("Validation Agent")
# 각 에이전트의 HPA
agent_hpa = HorizontalPodAutoscaler("Agents HPA")
# 데이터 처리 파이프라인
with Cluster("Data Processing Pipeline"):
# 스트리밍 처리
kafka = Kafka("Kafka")
stream_processor = Service("Stream Processor")
# 배치 처리
airflow = Airflow("Airflow")
etl_jobs = Job("ETL Jobs")
# 다중 데이터 저장소
with Cluster("Multi-Modal Data Stores"):
# 벡터 데이터베이스 클러스터
with Cluster("Vector Database Cluster"):
weaviate_primary = Service("Weaviate Primary")
weaviate_replicas = [Service(f"Weaviate Replica {i}") for i in range(2)]
vector_storage = StorageClass("Vector Storage")
# 그래프 데이터베이스 클러스터
with Cluster("Graph Database Cluster"):
neo4j_cluster = Service("Neo4j Cluster")
graph_storage = StorageClass("Graph Storage")
# 메타데이터 저장소
metadata_db = PostgreSQL("Metadata DB")
# 다층 캐싱
with Cluster("Caching Layer"):
redis_cluster = Service("Redis Cluster")
memcached = Service("Memcached")
# LLM 서비스 클러스터 (다중 모델)
with Cluster("Multi-LLM Services"):
# 메인 LLM 서비스
llm_gateway = Service("LLM Gateway")
# 특화된 모델들
reasoning_llm = Service("Reasoning LLM")
synthesis_llm = Service("Synthesis LLM")
embedding_service = Service("Embedding Service")
# 모델 서빙 인프라
model_hpa = HorizontalPodAutoscaler("Model HPA")
# 모니터링 및 관측성
with Cluster("Observability Stack"):
# 메트릭 수집
prometheus = Prometheus("Prometheus")
# 로깅
fluentd = Pod("Fluentd")
elasticsearch = Service("Elasticsearch")
# 시각화 및 알림
grafana = Grafana("Grafana")
alertmanager = Service("AlertManager")
# 분산 추적
jaeger = Service("Jaeger")
# 보안 및 백업
with Cluster("Security & Backup"):
vault = Service("Vault")
backup_jobs = Job("Backup Jobs")
disaster_recovery = Service("DR Service")
# 연결 관계 정의
users >> Edge(label="HTTPS", style="bold") >> ingress
ingress >> network_policy >> api_gateway
api_gateway >> gateway_hpa >> gateway_pods
gateway_pods >> orchestrator_svc >> orchestrator_pods
# Agentic 워크플로우
orchestrator_pods >> query_agent
query_agent >> retrieval_agent
retrieval_agent >> reasoning_agent
reasoning_agent >> synthesis_agent
synthesis_agent >> validation_agent
# 데이터 플로우
s3_storage >> kafka >> stream_processor
airflow >> etl_jobs
retrieval_agent >> weaviate_primary >> weaviate_replicas
reasoning_agent >> neo4j_cluster
[query_agent, synthesis_agent] >> redis_cluster
# LLM 연결
reasoning_agent >> reasoning_llm
synthesis_agent >> synthesis_llm
retrieval_agent >> embedding_service
# 모니터링 연결
[orchestrator_pods, gateway_pods] >> prometheus
[orchestrator_pods, gateway_pods] >> fluentd >> elasticsearch
prometheus >> grafana
prometheus >> alertmanager
# 보안
service_account >> vault
[weaviate_primary, neo4j_cluster, metadata_db] >> backup_jobs
if __name__ == "__main__":
create_advanced_agentic_rag()
마이크로서비스 세부 아키텍처
개별 Agentic RAG 마이크로서비스의 내부 구조를 상세히 표현해보겠습니다:
def create_microservice_detail():
with Diagram("Agentic RAG Microservice Detail",
show=False,
direction="LR",
filename="microservice_detail"):
# 입력 레이어
with Cluster("Input Layer"):
api_endpoint = Service("REST API")
graphql_endpoint = Service("GraphQL API")
websocket = Service("WebSocket")
# 비즈니스 로직 레이어
with Cluster("Business Logic Layer"):
# 요청 처리
request_handler = Pod("Request Handler")
request_validator = Pod("Input Validator")
# 에이전트 코어
with Cluster("Agent Core"):
agent_orchestrator = Pod("Agent Orchestrator")
reasoning_engine = Pod("Reasoning Engine")
tool_manager = Pod("Tool Manager")
memory_manager = Pod("Memory Manager")
# 도구들
with Cluster("Agent Tools"):
search_tool = Pod("Search Tool")
analysis_tool = Pod("Analysis Tool")
synthesis_tool = Pod("Synthesis Tool")
validation_tool = Pod("Validation Tool")
# 데이터 액세스 레이어
with Cluster("Data Access Layer"):
vector_client = Pod("Vector DB Client")
graph_client = Pod("Graph DB Client")
cache_client = Pod("Cache Client")
llm_client = Pod("LLM Client")
# 출력 레이어
with Cluster("Output Layer"):
response_formatter = Pod("Response Formatter")
stream_handler = Pod("Stream Handler")
webhook_sender = Pod("Webhook Sender")
# 연결 관계
[api_endpoint, graphql_endpoint, websocket] >> request_handler
request_handler >> request_validator >> agent_orchestrator
agent_orchestrator >> reasoning_engine
reasoning_engine >> tool_manager
tool_manager >> [search_tool, analysis_tool, synthesis_tool, validation_tool]
agent_orchestrator >> memory_manager
[search_tool, analysis_tool] >> vector_client
[reasoning_engine, synthesis_tool] >> graph_client
[tool_manager, memory_manager] >> cache_client
[reasoning_engine, synthesis_tool] >> llm_client
validation_tool >> response_formatter
response_formatter >> [stream_handler, webhook_sender]
if __name__ == "__main__":
create_microservice_detail()
실제 테스트 및 실행
테스트 환경 구성
# 테스트 디렉토리 생성
mkdir diagrams_test && cd diagrams_test
# 가상환경 설정
python -m venv venv
source venv/bin/activate # macOS/Linux
# venv\Scripts\activate # Windows
# 필요한 패키지 설치
pip install diagrams pillow
테스트 스크립트 실행
# create_all_diagrams.py
import os
from diagrams import Diagram, Cluster, Edge
from diagrams.k8s.compute import Pod, Deployment
from diagrams.k8s.network import Service, Ingress
from diagrams.k8s.storage import PersistentVolume
from diagrams.programming.framework import React
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
def main():
"""모든 다이어그램을 생성하는 메인 함수"""
print("🎨 Diagrams 테스트 시작...")
# 1. 기본 Agentic RAG 아키텍처
create_basic_agentic_rag()
print("✅ 기본 Agentic RAG 아키텍처 생성 완료")
# 2. 고급 Agentic RAG 아키텍처
create_advanced_agentic_rag()
print("✅ 고급 Agentic RAG 아키텍처 생성 완료")
# 3. 마이크로서비스 세부 아키텍처
create_microservice_detail()
print("✅ 마이크로서비스 세부 아키텍처 생성 완료")
print("\n🎉 모든 다이어그램 생성 완료!")
print("생성된 파일들:")
for file in os.listdir('.'):
if file.endswith('.png'):
print(f" 📊 {file}")
if __name__ == "__main__":
main()
실행 결과
$ python create_all_diagrams.py
🎨 Diagrams 테스트 시작...
✅ 기본 Agentic RAG 아키텍처 생성 완료
✅ 고급 Agentic RAG 아키텍처 생성 완료
✅ 마이크로서비스 세부 아키텍처 생성 완료
🎉 모든 다이어그램 생성 완료!
생성된 파일들:
📊 basic_agentic_rag.png
📊 advanced_agentic_rag.png
📊 microservice_detail.png
개발환경 정보
테스트 환경:
- 운영체제: macOS Sequoia 15.0 (Darwin 25.0.0)
- Python: 3.12.3
- Diagrams: 0.24.4 (최신 버전)
- Graphviz: 12.2.0
- 테스트 실행일: 2025-06-28
설치 검증 결과:
- ✅ Graphviz 설치 성공
- ✅ Diagrams 라이브러리 정상 작동
- ✅ 쿠버네티스 노드 렌더링 성공
- ✅ 복합 클러스터 구조 생성 완료
고급 활용 방법
1. CI/CD 통합
# .github/workflows/generate-diagrams.yml
name: Generate Architecture Diagrams
on:
push:
paths:
- 'diagrams/**'
- 'architecture/**'
jobs:
generate-diagrams:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install Graphviz
run: sudo apt-get update && sudo apt-get install -y graphviz
- name: Install Dependencies
run: pip install diagrams
- name: Generate Diagrams
run: |
cd diagrams
python generate_all.py
- name: Commit Generated Diagrams
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add *.png
git commit -m "Update architecture diagrams" || exit 0
git push
2. 동적 아키텍처 생성
# dynamic_architecture.py
import json
import yaml
from diagrams import Diagram, Cluster
from diagrams.k8s.compute import Pod
from diagrams.k8s.network import Service
def create_from_config(config_file):
"""설정 파일을 기반으로 동적 아키텍처 생성"""
with open(config_file, 'r') as f:
if config_file.endswith('.yaml'):
config = yaml.safe_load(f)
else:
config = json.load(f)
with Diagram(config['name'], show=False):
services = {}
# 서비스 생성
for service_config in config['services']:
if service_config.get('cluster'):
with Cluster(service_config['cluster']):
services[service_config['name']] = Service(service_config['name'])
else:
services[service_config['name']] = Service(service_config['name'])
# 연결 관계 설정
for connection in config.get('connections', []):
source = services[connection['from']]
target = services[connection['to']]
source >> target
# 설정 파일 예제 (architecture.yaml)
architecture_config = """
name: "Dynamic RAG Architecture"
services:
- name: "API Gateway"
cluster: "Gateway Layer"
- name: "RAG Service"
cluster: "Core Services"
- name: "Vector DB"
cluster: "Data Layer"
connections:
- from: "API Gateway"
to: "RAG Service"
- from: "RAG Service"
to: "Vector DB"
"""
3. 인터랙티브 다이어그램
# interactive_diagram.py
from diagrams import Diagram, Edge
from diagrams.k8s.compute import Pod
import os
class InteractiveDiagramBuilder:
def __init__(self):
self.components = []
self.connections = []
def add_component(self, name, component_type, cluster=None):
"""컴포넌트 추가"""
component = {
'name': name,
'type': component_type,
'cluster': cluster
}
self.components.append(component)
return len(self.components) - 1 # 인덱스 반환
def connect(self, source_idx, target_idx, label=None):
"""컴포넌트 간 연결 추가"""
self.connections.append({
'source': source_idx,
'target': target_idx,
'label': label
})
def generate(self, filename="interactive_diagram"):
"""다이어그램 생성"""
with Diagram("Interactive Architecture",
show=False,
filename=filename):
# 컴포넌트 인스턴스 생성
instances = []
for comp in self.components:
if comp['type'] == 'pod':
instances.append(Pod(comp['name']))
# 다른 타입들도 추가 가능
# 연결 관계 설정
for conn in self.connections:
source = instances[conn['source']]
target = instances[conn['target']]
if conn['label']:
source >> Edge(label=conn['label']) >> target
else:
source >> target
# 사용 예제
builder = InteractiveDiagramBuilder()
gateway_idx = builder.add_component("API Gateway", "pod")
rag_idx = builder.add_component("RAG Service", "pod")
db_idx = builder.add_component("Vector DB", "pod")
builder.connect(gateway_idx, rag_idx, "HTTP")
builder.connect(rag_idx, db_idx, "gRPC")
builder.generate("interactive_rag")
4. 성능 모니터링 다이어그램
# monitoring_diagram.py
from diagrams import Diagram, Cluster
from diagrams.onprem.monitoring import Prometheus, Grafana
from diagrams.k8s.compute import Pod
def create_monitoring_architecture():
with Diagram("RAG System Monitoring", show=False):
# 애플리케이션 레이어
with Cluster("Application Layer"):
rag_pods = [Pod(f"RAG Pod {i}") for i in range(3)]
# 모니터링 스택
with Cluster("Monitoring Stack"):
# 메트릭 수집
prometheus = Prometheus("Prometheus")
# 시각화
grafana = Grafana("Grafana")
# 알림
alertmanager = Pod("AlertManager")
# 모니터링 대상들
monitored_services = [
Pod("Vector DB"),
Pod("Graph DB"),
Pod("LLM Service"),
Pod("Cache")
]
# 연결 관계
[rag_pods, monitored_services] >> prometheus
prometheus >> grafana
prometheus >> alertmanager
create_monitoring_architecture()
실제 프로덕션 사례
1. Apache Airflow의 활용
Apache Airflow는 Diagrams를 사용하여 공식 문서의 아키텍처 다이어그램을 생성합니다. 이는 대규모 오픈소스 프로젝트에서 Diagrams가 실제로 활용되는 사례입니다.
2. Cloudiscovery 통합
Cloudiscovery는 클라우드 리소스를 분석하고 Diagrams를 기반으로 인프라 맵을 생성하는 도구입니다. 실제 운영 중인 클라우드 인프라를 시각화하는 데 활용됩니다.
3. 자동화된 문서화 워크플로우
# production_workflow.py
import subprocess
import os
from datetime import datetime
def automated_documentation_workflow():
"""프로덕션 환경의 자동화된 문서화 워크플로우"""
# 1. 최신 인프라 정보 수집
print("📊 인프라 정보 수집 중...")
# 2. 다이어그램 생성
print("🎨 아키텍처 다이어그램 생성 중...")
subprocess.run(["python", "generate_architecture.py"])
# 3. 문서 업데이트
print("📝 문서 업데이트 중...")
# 4. Git 커밋 및 푸시
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
subprocess.run(["git", "add", "*.png"])
subprocess.run(["git", "commit", "-m", f"Update architecture diagrams - {timestamp}"])
subprocess.run(["git", "push"])
print("✅ 문서화 워크플로우 완료!")
if __name__ == "__main__":
automated_documentation_workflow()
팁과 모범 사례
1. 성능 최적화
# 큰 다이어그램의 경우 렌더링 최적화
with Diagram("Large Architecture",
show=False,
graph_attr={
"splines": "ortho", # 직각 연결선
"nodesep": "0.8", # 노드 간격
"ranksep": "1.0" # 레벨 간격
}):
pass
2. 일관된 스타일링
# 공통 스타일 정의
COMMON_GRAPH_ATTR = {
"fontsize": "14",
"fontname": "Arial",
"bgcolor": "white",
"pad": "0.5"
}
COMMON_NODE_ATTR = {
"fontsize": "12",
"fontname": "Arial"
}
with Diagram("Styled Architecture",
graph_attr=COMMON_GRAPH_ATTR,
node_attr=COMMON_NODE_ATTR):
pass
3. 버전 관리 모범 사례
# .gitignore에 추가
*.png
!docs/diagrams/*.png # 문서용 다이어그램만 버전 관리
# 다이어그램 소스 코드는 반드시 버전 관리
diagrams/
├── src/
│ ├── basic_architecture.py
│ ├── advanced_architecture.py
│ └── monitoring.py
├── generated/
│ └── .gitkeep
└── README.md
결론
Diagrams는 Python 코드로 시스템 아키텍처를 설계할 수 있는 강력한 도구입니다. 특히 쿠버네티스 환경에서 Agentic RAG 시스템과 같은 복잡한 마이크로서비스 아키텍처를 설계할 때 그 진가를 발휘합니다.
주요 장점
- 코드 기반 설계: 프로그래밍 방식으로 유연하고 재사용 가능한 다이어그램 생성
- 버전 관리: Git과 완벽 통합되어 아키텍처 변경사항 추적 가능
- 자동화: CI/CD 파이프라인에 통합하여 문서 자동 업데이트
- 확장성: 41.1k 스타를 받은 활발한 오픈소스 프로젝트
활용 분야
- 클라우드 네이티브 아키텍처: Kubernetes, 마이크로서비스, 컨테이너 오케스트레이션
- AI/ML 시스템: RAG, LLMOps, 데이터 파이프라인
- DevOps: CI/CD, 모니터링, 인프라 관리
- 기술 문서화: 아키텍처 문서, 시스템 설계서
Diagrams를 활용하면 복잡한 시스템 아키텍처를 명확하고 일관되게 표현할 수 있으며, 개발팀과 이해관계자들 간의 소통을 크게 개선할 수 있습니다. “Diagram as Code” 패러다임은 현대적인 소프트웨어 개발에서 필수적인 도구가 되어가고 있습니다.