1. Prometheus의 데이터 수집 방식
Prometheus는 Pull 방식(Pull-Based)을 사용하여 대상(target)으로부터 데이터를 직접 가져오는 구조를 가집니다. 이는 일반적인 Push 방식(Push-Based)과 차별화되는 개념입니다.
📌 Pull vs Push 방식 비교
방식개념동작 방식사용 예
Pull 방식 | Prometheus가 대상에서 데이터를 가져옴 | Prometheus가 일정 간격마다 HTTP 요청(GET)을 통해 메트릭을 가져옴 | Prometheus, Grafana Agent |
Push 방식 | 대상이 직접 데이터를 전송 | 애플리케이션이 특정 서버(Push Gateway)에 메트릭을 직접 보냄 | StatsD, InfluxDB, AWS CloudWatch |
2. Prometheus의 Pull 방식 동작 원리
✅ 기본 구조
- Prometheus 서버(Prometheus Server)
- HTTP GET 요청을 사용하여 대상에서 메트릭을 수집
- 일정 주기(기본 15초)마다 타겟을 스크랩(scrape)
- prometheus.yml에서 스크랩할 타겟을 정의
- 메트릭을 제공하는 Exporter (타겟)
- Prometheus는 Exporter에서 메트릭을 가져옴
- Exporter는 /metrics 엔드포인트를 제공해야 함
- 대표적인 Exporter 예시:
- Node Exporter (서버 리소스 모니터링)
- cAdvisor (컨테이너 모니터링)
- Blackbox Exporter (HTTP/HTTPS 모니터링)
✅ Pull 방식 동작 예제
- Prometheus가 Node Exporter에서 CPU 사용량을 가져오는 과정
1. Prometheus가 Node Exporter에게 HTTP 요청(GET) 전송
➜ GET http://node-exporter:9100/metrics
2. Node Exporter가 CPU 사용량 등의 데이터를 반환
➜ Response:
cpu_usage 0.25
memory_usage 512MB
3. Prometheus는 데이터를 저장하고 Grafana에서 시각화 가능
3. Pull 방식의 장점과 단점
✅ Pull 방식의 장점
🔹 단순한 설정: Prometheus가 설정된 타겟에서 직접 데이터를 가져오므로, 추가적인 설정이 필요 없음
🔹 서비스 검색(Service Discovery)과 유연성: Kubernetes, EC2 등에서 자동으로 타겟을 발견(Service Discovery) 가능
🔹 보안 강화: 서버에서 데이터를 가져오는 방식이므로 방화벽 규칙을 더 쉽게 관리 가능 (Push 방식은 외부에서 서버로 전송해야 하므로 보안 이슈 발생 가능)
🔹 스크랩 주기 설정 가능: 각 타겟마다 다른 수집 주기를 설정할 수 있어 성능 최적화 가능
⚠ Pull 방식의 단점
❌ Push 기반 시스템과 통합이 어려움:
- Prometheus는 기본적으로 Pull 방식이므로, Push 방식만 지원하는 애플리케이션과 통합하려면 Push Gateway가 필요
❌ 짧은 간격의 고빈도 이벤트 감지가 어려움: - Prometheus가 일정 간격으로 데이터를 가져오므로, 짧은 시간 안에 발생하는 이벤트는 놓칠 가능성이 있음
- 해결책: Alertmanager 또는 Push Gateway 사용
4. Pull 방식 설정 방법 (prometheus.yml 예제)
📌 Prometheus 설정 파일 (prometheus.yml)
global:
scrape_interval: 15s # 기본 스크랩 주기 (15초)
scrape_configs:
- job_name: 'node_exporter'
scrape_interval: 5s # 특정 타겟만 5초 주기로 수집
static_configs:
- targets: ['localhost:9100'] # Node Exporter 실행 중인 서버
- scrape_interval: Prometheus가 데이터를 가져오는 주기 (기본값 15초)
- targets: Prometheus가 데이터를 수집할 엔드포인트 (http://localhost:9100/metrics)
5. Prometheus Pull 방식과 Kubernetes 연동
Kubernetes 환경에서는 Service Discovery 기능을 이용해 자동으로 타겟을 검색할 수 있습니다.
📌 Kubernetes에서 Pull 방식 설정 (prometheus.yml 예제)
scrape_configs:
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__address__]
regex: '(.*):10250'
replacement: '${1}:9100'
target_label: __address__
- kubernetes_sd_configs: Kubernetes의 노드를 자동 검색하여 Pull 방식으로 메트릭 수집
- relabel_configs: 특정 포트(10250)를 9100으로 변환 (Node Exporter 사용 시 필요)
6. 결론: Prometheus Pull 방식 요약
✔ Prometheus는 기본적으로 Pull 방식(Pull-Based Monitoring) 사용
✔ HTTP GET 요청을 통해 대상(exporter)에서 주기적으로 데이터를 수집
✔ Push 방식이 필요한 경우 Push Gateway 사용 가능
✔ 보안이 뛰어나고, 서비스 디스커버리(Kubernetes, EC2)와 잘 연동됨
✔ 짧은 간격의 이벤트 모니터링에는 부적절할 수 있음 (Alertmanager 필요)
🚀 Prometheus Pull 방식은 효율적인 모니터링을 위한 강력한 솔루션이지만, 특정 상황에서는 Push 방식(Push Gateway)과 혼용하여 사용해야 할 수도 있습니다.