CodeGym /행동 /Docker SELF /Prometheus를 사용한 컨테이너 모니터링 설정

Prometheus를 사용한 컨테이너 모니터링 설정

Docker SELF
레벨 21 , 레슨 3
사용 가능

4.1 Prometheus 설치

Prometheus — 오픈 소스 기반의 강력한 모니터링 및 알림 시스템으로, Docker 컨테이너를 포함한 다양한 소스에서 메트릭을 수집하고 저장하도록 설계돼 있어. 이번 강의에서는 Prometheus를 사용한 컨테이너 모니터링 설정 과정을, 설치와 메트릭 수집 설정, Docker와의 통합까지 자세히 다룰 거야.

1. Prometheus 다운로드 및 설치

우선 공식 웹사이트에서 최신 버전의 Prometheus를 다운로드해:

터미널

wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz

압축을 풀어:

터미널

tar xvfz prometheus-*.tar.gz
cd prometheus-*      

바이너리 파일을 /usr/local/bin로 이동시켜:

터미널

sudo mv prometheus /usr/local/bin/
sudo mv promtool /usr/local/bin/

4.2 Prometheus 설정 파일 설정

Prometheus는 prometheus.yml 설정 파일을 사용해서 메트릭 소스와 데이터 수집 설정을 정의해. prometheus.yml 파일을 생성하거나 수정해:

Yaml

global:
  scrape_interval: 15s  # 메트릭 수집 간격
      
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
      
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['localhost:8080']

이 설정 파일은 두 개의 작업(job)을 포함해:

  1. prometheus: Prometheus 자체에서 메트릭을 수집하기 위한 설정.
  2. cadvisor: Docker 컨테이너를 모니터링하기 위해 사용되는 cAdvisor의 메트릭을 수집하기 위한 설정.

5.3 Prometheus 실행하기

생성된 설정 파일을 사용해서 Prometheus를 실행해봐:

Terminal

prometheus --config.file=prometheus.yml

Prometheus는 http://localhost:9090에서 접근할 수 있어.

5.4 cAdvisor 설치 및 설정

cAdvisor (Container Advisor) — Docker 컨테이너를 위한 메트릭을 수집하고 제공하는 컨테이너 모니터링 도구야. 사용하려면 아래 단계를 따라 하면 돼:

cAdvisor를 Docker 컨테이너에서 실행하세요:

터미널

docker run -d \
  --name=cadvisor \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:ro \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --publish=8080:8080 \
  --privileged \
  gcr.io/cadvisor/cadvisor:latest

cAdvisorhttp://localhost:8080에서 접근 가능해.

5.5 cAdvisor용 Prometheus 설정 업데이트

Prometheus 설정이 cAdvisor에 대한 작업을 포함하는지 확인해봐. 설정 예제는 위에 나와 있어.

변경 내용을 적용시키기 위해 Prometheus를 재시작해:

Terminal

prometheus --config.file=prometheus.yml

5.6 메트릭 수집 확인

브라우저에서 Prometheus 웹 인터페이스(http://localhost:9090)로 이동하고 Prometheus가 cAdvisor에서 메트릭을 가져오는지 확인해봐. 이를 위해 Prometheus에서 다음 요청을 실행하면 돼:

Terminal

container_cpu_usage_seconds_total

Docker 컨테이너에서 수집된 데이터를 볼 수 있을 거야.

5.7 추가 메트릭 추가하기

Prometheus 설정 파일에 추가 메트릭을 추가해서 컨테이너에 대한 더 많은 데이터를 수집할 수 있어. 추가 메트릭 예시:

Yaml

  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']

이를 위해 node_exporter를 설치해야 해. 이 도구는 호스트 시스템 상태에 대한 메트릭을 제공해 줘.

node_exporter를 다운로드하고 설치해:

Terminal

wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
sudo mv node_exporter-1.2.2.linux-amd64/node_exporter /usr/local/bin/

node_exporter를 실행해:

Terminal

node_exporter &

이제 node_exporter를 http://localhost:9100에서 사용할 수 있어.

5.8 알림 규칙 만들기

Prometheus는 관리자들에게 문제를 알려주는 알림 규칙을 만드는 걸 지원해. prometheus.yml 설정 파일에 알림 규칙을 추가해:

Yaml

rule_files:
  - "alert.rules"
      
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - "localhost:9093"

alert.rules 파일을 만들고 규칙 예제를 추가해봐:

Yaml

groups:
- name: example
  rules:
  - alert: HighCPUUsage
    expr: container_cpu_usage_seconds_total{job="cadvisor"} > 0.85
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "높은 CPU 사용량 감지됨"
      description: "컨테이너가 1분 이상 CPU를 85% 넘게 사용 중."
코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION