1. 개요
오픈 소스 모니터링 솔루션 중 하나인 Prometheus(프로메테우스)를 활용하여 도커 컨테이너의 지표를 수집하고 확인하는 법을 소개한다.
프로메테우스는 모니터링 대상에서 지표를 주기적으로 읽어오는 방식이므로, 도커에서 지표를 제공하는 방법과, 프로메테우스 설정에 대해 설명한다.
* Docker Desktop for Mac 을 기준으로 설명함.
2. Docker Daemon 설정
Docker Daemon을 프로메테우스의 모니터링 대상으로 설정하기 위해 'metrics-address'를 지정해야 한다. 이 설정은 daemon.json에서 변경 가능하다.
Docker Desktop -> Preferences -> Advanced에서 확인 가능하며, 아래 내용을 추가한 후, restart 하면 설정은 완료된다.
{
"metrics-addr" : "127.0.0.1:9323",
"experimental" : true
}
위 설정을 마치면, 다음과 같이 9323번 포트를 통해 프로메테우스에서 수집할 데이터를 제공하게 된다.

3. Prometheus 설정 및 실행
* Docker Swarm이 구성되어 있다는 전제 하에 진행함.
prometheus.yml 파일을 다음과 같이 작성한다.
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first.rules"
# - "second.rules"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['<manager IP>:9090']
- job_name: 'docker'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['<로컬 IP>:9323']
다음과 같이 프로메테우스 서비스를 생성한다. (이번 예제에서는 manager에 서비스를 생성함)
$ docker service create --name my-prometheus --mount type=bind,src=<prometheus.yml위치>/prometheus.yml,dst=/etc/prometheus/prometheus.yml --publish published=9090,target=9090,protocol=tcp prom/prometheus
http://<매니저 IP>:9090/targets로 접속해보면 모니터링 대상을 확인할 수 있다.

상단 메뉴 중 Graph 메뉴로 이동하여 지표를 선택하면 지표를 그래프 형태로 조회할 수 있다.
