HashiCorp Consul은 서비스 디스커버리, 상태 점검, 키-값 저장, 서비스 메시를 제공하는 분산 시스템 도구다. 마이크로서비스 환경에서 서비스 간 연결을 자동화한다.
Consul 핵심 기능
| 기능 | 설명 |
|---|
| 서비스 등록/발견 | DNS 또는 HTTP API로 서비스 탐색 |
| 상태 점검 | HTTP, TCP, gRPC, Script 기반 헬스체크 |
| KV 스토어 | 설정 데이터 저장 (etcd 유사) |
| Connect | mTLS 서비스 메시 (Envoy 사이드카) |
| ACL | 세분화된 접근 제어 |
서비스 등록 (설정 파일)
json
{
"service": {
"name": "order-service",
"id": "order-service-1",
"address": "10.0.1.5",
"port": 8080,
"tags": ["v2", "primary"],
"meta": {"version": "2.1.0", "env": "production"},
"checks": [
{
"http": "http://localhost:8080/health",
"interval": "10s",
"timeout": "3s",
"deregister_critical_service_after": "1m"
}
]
}
}
API 기반 서비스 등록
python
import consul
c = consul.Consul(host='consul-server', port=8500)
# 서비스 등록
c.agent.service.register(
name='order-service',
service_id='order-service-1',
address='10.0.1.5',
port=8080,
tags=['v2'],
check=consul.Check.http(
url='http://localhost:8080/health',
interval='10s',
timeout='3s',
),
)
# 서비스 발견
index, services = c.health.service('order-service', passing=True)
for svc in services:
addr = svc['Service']['Address']
port = svc['Service']['Port']
print(f"발견된 인스턴스: {addr}:{port}")
# KV 스토어
c.kv.put('config/order-service/db-url', 'postgresql://...')
index, data = c.kv.get('config/order-service/db-url')
print(data['Value'].decode('utf-8'))
DNS 서비스 발견
bash
# Consul DNS를 통한 서비스 조회
dig @127.0.0.1 -p 8600 order-service.service.consul
# 특정 태그로 필터링
dig @127.0.0.1 -p 8600 v2.order-service.service.consul
# SRV 레코드 (포트 포함)
dig @127.0.0.1 -p 8600 order-service.service.consul SRV
Consul vs etcd vs ZooKeeper
| 항목 | Consul | etcd | ZooKeeper |
|---|
| 서비스 디스커버리 | 네이티브 | 없음 | 가능 |
| 헬스체크 | 내장 | 없음 | 없음 |
| 서비스 메시 | Consul Connect | 없음 | 없음 |
| 주요 사용 | 서비스 메시 | Kubernetes | Kafka, Hadoop |