Prometheus로 Docker 메트릭 수집하기

Prometheus로 Docker 메트릭 수집하기 (Collect Docker metrics with Prometheus)

Prometheus는 오픈소스 시스템 모니터링 및 알림 툴킷이에요. Docker를 Prometheus 대상으로 구성할 수 있어요.

Warning 사용 가능한 메트릭과 그 이름은 개발 중이며 언제든 바뀔 수 있어요.

현재 Docker 자체만 모니터링할 수 있어요. Docker 대상을 사용해 애플리케이션을 모니터링하는 것은 현재 불가능해요.

출처: 문서

본문

예시 (Example)

다음 예시는 Docker 데몬을 구성하고, Prometheus가 로컬 머신에서 컨테이너로 실행되도록 설정하고, Prometheus로 Docker 인스턴스를 모니터링하는 방법을 보여줘요.

데몬 구성 (Configure the daemon)

Docker 데몬을 Prometheus 대상으로 구성하려면 daemon.json 구성 파일에 metrics-address를 지정해야 해요. 이 데몬은 기본적으로 다음 위치 중 하나에 파일이 있기를 기대해요. 파일이 없으면 만들어요.

  • Linux: /etc/docker/daemon.json
  • Windows Server: C:\ProgramData\docker\config\daemon.json
  • Docker Desktop: Docker Desktop 설정을 열고 Docker Engine을 선택해 파일을 편집해요.

다음 구성을 추가해요:

{
  "metrics-addr": "127.0.0.1:9323"
}

파일을 저장하거나, Docker Desktop for Mac 또는 Docker Desktop for Windows의 경우 구성을 저장해요. Docker를 재시작해요.

이제 Docker는 루프백 인터페이스를 통해 포트 9323에서 Prometheus 호환 메트릭을 노출해요. 와일드카드 주소 0.0.0.0을 사용하도록 구성할 수도 있지만, 그러면 Prometheus 포트가 더 넓은 네트워크에 노출돼요. 어떤 옵션이 환경에 가장 적합한지 결정할 때 위협 모델을 신중히 고려하세요.

Prometheus 구성 파일 생성 (Create a Prometheus configuration)

다음 구성 파일을 복사해 원하는 위치(예: /tmp/prometheus.yml)에 저장해요. 이것은 파일 하단에 Docker job 정의가 추가된 것을 제외하면 표준 Prometheus 구성 파일이에요.

# 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=<job_name>` to any timeseries scraped from this config.
  - job_name: prometheus

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: docker
      # metrics_path defaults to '/metrics'
      # scheme defaults to 'http'.

    static_configs:
      - targets: ["host.docker.internal:9323"]

컨테이너에서 Prometheus 실행 (Run Prometheus in a container)

다음으로 이 구성을 사용해 Prometheus 컨테이너를 시작해요.

$ docker run --name my-prometheus \
    --mount type=bind,source=/tmp/prometheus.yml,destination=/etc/prometheus/prometheus.yml \
    -p 9090:9090 \
    --add-host host.docker.internal=host-gateway \
    prom/prometheus

Docker Desktop을 사용한다면 --add-host 플래그는 선택 사항이에요. 이 플래그는 호스트의 내부 IP가 Prometheus 컨테이너에 노출되도록 보장해요. Docker Desktop은 이를 기본으로 해요. 호스트 IP는 host.docker.internal 호스트 이름으로 노출돼요. 이는 이전 단계의 prometheus.yml에 정의된 구성과 일치해요.

Prometheus 대시보드 열기 (Open the Prometheus Dashboard)

Docker 대상이 http://localhost:9090/targets/에 나열되어 있는지 확인해요.

Note Docker Desktop을 사용하면 이 페이지에서 엔드포인트 URL에 직접 접근할 수 없어요.

Prometheus 사용 (Use Prometheus)

그래프를 만들어요. Prometheus UI에서 Graphs 링크를 선택해요. Execute 버튼 오른쪽의 콤보 박스에서 메트릭을 선택하고 Execute를 클릭해요. 아래 스크린샷은 engine_daemon_network_actions_seconds_count의 그래프를 보여줘요.

그래프는 시스템에서 이미 활성 워크로드를 실행 중이지 않은 한 꽤 유휴 상태의 Docker 인스턴스를 보여줘요.

그래프를 더 흥미롭게 만들려면 패키지 매니저로 일부 패키지 다운로드를 시작해 네트워크 작업을 사용하는 컨테이너를 실행해요:

$ docker run --rm alpine apk add git make musl-dev go

몇 초(기본 스크레이프 간격은 15초) 기다리고 그래프를 다시 로드해요. 방금 실행한 컨테이너가 유발한 증가한 네트워크 트래픽을 보여주는 그래프의 상승을 볼 수 있어요.

다음 단계 (Next steps)

여기 제공된 예시는 로컬 시스템에서 Prometheus를 컨테이너로 실행하는 방법을 보여줘요. 실제로는 다른 시스템이나 어딘가의 클라우드 서비스로 Prometheus를 실행할 가능성이 높아요. 그러한 맥락에서도 Docker 데몬을 Prometheus 대상으로 설정할 수 있어요. 데몬의 metrics-addr을 구성하고 Prometheus 구성에 데몬 주소를 스크레이프 엔드포인트로 추가해요.

- job_name: docker
  static_configs:
    - targets: ["docker.daemon.example:<PORT>"]

Prometheus에 대한 자세한 내용은 Prometheus 문서를 참고하세요.

더 알아보기 (Learn more)