서버를 운영하다 보면 CPU, 메모리, 디스크 I/O 등 리소스 사용량을 모니터링하는 것이 선택이자 필수이다.
내 개인서비스나 회사에서는 도커를 사용하기 때문에 이 부분에 초점을 두어 Prometheus(메트릭 수집), cAdvisor(컨테이너 메트릭 노출), Grafana(시각화)를 조합하여 컨테이너 모니터링 환경을 구축해보았다.
아키텍처 개요

- cAdvisor: Docker 컨테이너의 리소스 메트릭 수집 및 노출
- Prometheus: cAdvisor에서 메트릭을 주기적으로 수집하여 저장
- Grafana: Prometheus 데이터를 대시보드로 시각화
설치 및 구성
1. cAdvisor 실행
docker run \
--name cadvisor \
--detach \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 \
gcr.io/cadvisor/cadvisor:latest
cAdvisor는 호스트의 Docker 소켓과 시스템 정보에 접근해야 하므로 위의 볼륨 마운팅이 필요하다.
2. Prometheus 구성
prometheus.yml 파일의 경우 아래와 같이 작성할 수 있다.
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'cadvisor'
static_configs:
- targets: ['localhost:8080']
Prometheus 를 Docker 로 실행한다면 아래와 같이 실행할 수 있다.
docker run \
--name prometheus \
--detach \
--volume /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
--volume prometheus_data:/prometheus \
--publish 9090:9090 \
prom/prometheus:latest \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus
3. Grafana 구성 및 대시보드 설정
docker run \
--name grafana \
--detach \
--volume grafana_data:/var/lib/grafana \
--publish 3000:3000 \
-e GF_SECURITY_ADMIN_PASSWORD=admin \
grafana/grafana:latest
Prometheus 데이터 소스 추가
- Grafana 접속: http://localhost:3000 (기본 ID: admin, PW: admin)
- Connections → Data Sources → Add data source
- Prometheus 선택
- URL: http://prometheus:9090 (Docker Compose 사용 시)
- Save & Test
대시보드 가져오기
- Dashboards → New → Import
- Grafana 공식 대시보드 ID 사용:
- 893 - Docker and Host Metrics
- 11074 - Node Exporter for Prometheus (컨테이너 전체 모니터링)
- 데이터 소스로 Prometheus 선택 후 Import
주요 모니터링 메트릭 (promql)
CPU 사용률
rate(container_cpu_usage_seconds_total[5m]) * 100
메모리 사용량 (MB)
container_memory_usage_bytes / 1024 / 1024
네트워크 트래픽 (수신/송신)
rate(container_network_receive_bytes_total[5m])
rate(container_network_transmit_bytes_total[5m])
디스크 I/O (읽기/쓰기)
rate(container_fs_io_current[5m])
Docker Compose로 통합 구성
version: '3.8'
services:
cadvisor:
image: gcr.io/cadvisor/cadvisor:latest
container_name: cadvisor
volumes:
- /:/rootfs:ro
- /var/run:/var/run:ro
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
- /dev/disk/:/dev/disk:ro
ports:
- "8080:8080"
networks:
- monitoring
prometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/prometheus
command:
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
ports:
- "9090:9090"
depends_on:
- cadvisor
networks:
- monitoring
grafana:
image: grafana/grafana:latest
container_name: grafana
volumes:
- grafana_data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
ports:
- "3000:3000"
depends_on:
- prometheus
networks:
- monitoring
volumes:
prometheus_data:
grafana_data:
networks:
monitoring:
driver: bridge
실행
docker-compose up -d
커스텀 대시보드 작성 예시
대시보드 패널 1: 컨테이너별 CPU 사용률
Panel Title: Container CPU Usage Query:
sum(rate(container_cpu_usage_seconds_total[5m]) * 100) by (container_label_com_docker_compose_service)
Visualization: Graph Legend: {{container_label_com_docker_compose_service}}
대시보드 패널 2: 메모리 사용 현황
Panel Title: Memory Usage by Container Query:
sum(container_memory_usage_bytes / 1024 / 1024) by (name)
Visualization: Bar Gauge Unit: MB
트러블슈팅
cAdvisor가 메트릭을 노출하지 않음
- Docker 데몬이 실행 중인지 확인
- 볼륨 마운팅 권한 확인 (--volume=/var/lib/docker/:/var/lib/docker:ro)
Prometheus에서 cAdvisor 대상을 찾을 수 없음
- prometheus.yml에서 targets 설정 확인
- Docker Compose 사용 시 서비스명으로 연결 (예: cadvisor:8080)
- http://localhost:9090/targets 에서 상태 확인
Grafana에서 "No data"
- Prometheus 데이터 소스가 정상 연결되었는지 확인
- PromQL 쿼리가 올바른지 확인 (Prometheus UI에서 먼저 테스트)
- 메트릭 수집 시간이 충분히 경과했는지 확인 (최소 1-2 스크래이프 주기)
성능 최적화
- Prometheus 보존 정책: 장기 저장 필요 시 --storage.tsdb.retention.time=30d 추가
- 스크래이프 간격 조정: 실시간 모니터링 필요 시 scrape_interval 감소 (수집 부하 감소, 저장소 감소)
- 메트릭 필터링: 불필요한 메트릭 제외로 성능 향상
metric_relabel_configs:
- source_labels: [__name__]
regex: 'container_(network|fs).*'
action: drop
결론
Grafana + Prometheus + cAdvisor 조합은 컨테이너 모니터링의 사실상 표준인 것 같다.. 이 스택을 통해 다음을 얻을 수 있습니다:
- 실시간 컨테이너 리소스 모니터링
- 장기 메트릭 데이터 저장 및 분석
- 직관적인 시각화 대시보드
- 오픈소스 기반의 확장 가능한 구조
이 스택을 기반으로 알림(Alert Rules), 자동 확장(Autoscaling) 등의 기능을 추가하여 모니터링 시스템 완성이 가능하다.
참고 자료
'DevOps > 모니터링' 카테고리의 다른 글
| [모니터링] - 그라파나 알림 매니저 (Grafana AlertManager) 활용하기 (0) | 2024.02.16 |
|---|---|
| [모니터링] - Node.js로 그라파나에서 DB 데이터 로그 보기 (0) | 2024.02.16 |
| [DevOps] - Jenkins와 Spring Boot로 구축하는 CI/CD 파이프라인 (0) | 2024.02.16 |
| [모니터링] - Grafana Loki로 도커 컨테이너 로그 보기 (0) | 2024.02.15 |
| [모니터링] - 그라파나 + 프로메테우스로 네트워크 로그 수집하기 (0) | 2024.02.15 |