Pushgateway 개요
Pushgateway 소개
Pushgateway는 Prometheus의 컴포넌트 중 하나로, 기본적으로 Prometheus 서버는 Exporter로부터 데이터를 주기적으로 가져옵니다(Pull 방식). 하지만 Pushgateway를 사용하면, 다양한 소스에서 수집된 데이터를 Pushgateway에 전송하고, 이를 통해 Prometheus 서버가 데이터를 수집할 수 있습니다.
Pushgateway의 장점
- Prometheus가 직접 접근할 수 없는 네트워크 환경에서도 데이터 수집이 가능합니다.
- 다양한 원본에서 수집된 데이터를 한 곳에서 통합하여 관리할 수 있습니다.
Pushgateway의 단점
- Prometheus가 Pushgateway만 모니터링하므로, 각 노드별 상태 파악에는 한계가 있습니다.
- Pushgateway에 문제가 발생하면 전체 데이터 수집에 영향을 줄 수 있습니다.
- 모니터링 대상이 종료되어도 이전 데이터가 남아 있어 수동으로 삭제해야 합니다.
테스트 환경
| IP | 호스트명 |
|---|---|
| 192.168.2.139 | master1 |
| 192.168.40.140 | node1 |
설치 및 테스트
Pushgateway 설치
docker pull prom/pushgateway
docker run -d --name my-pushgw -p 9091:9091 prom/pushgateway
브라우저에서 http://192.168.2.140:9091 에 접속하여 UI를 확인할 수 있습니다.
Prometheus 설정에 Pushgateway 추가
- job_name: 'my-pushgw'
scrape_interval: 5s
static_configs:
- targets: ['192.168.2.140:9091']
honor_labels: true
kubectl apply -f prometheus-config.yaml
kubectl delete -f prometheus-deployment.yaml
kubectl apply -f prometheus-deployment.yaml
Prometheus 대시보드에서 http://192.168.2.139:30242/targets 를 통해 확인 가능합니다.
특정 형식의 데이터 Pushgateway로 전송
단일 데이터 추가
echo "metric_value 7.2" | curl --data-binary @- http://192.168.2.140:9091/metrics/job/my-job
복잡한 데이터 추가
cat <<EOF | curl --data-binary @- http://192.168.2.140:9091/metrics/job/my-job/instance/my-instance
# TYPE system_cpu_usage gauge
system_cpu_usage 50
# TYPE total_memory gauge
total_memory 16000
EOF
</code>
Python 스크립트를 이용한 데이터 전송
# 필요한 라이브러리 임포트
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
if __name__ == '__main__':
registry = CollectorRegistry()
g_one = Gauge('api_requests_total', 'Total API requests', registry=registry)
g_two = Gauge('avg_api_response_time', 'Average response time for APIs', registry=registry)
g_one.set(100)
g_two.set(0.5)
push_to_gateway('http://192.168.2.140:9091', job='APIStats', registry=registry)