모니터링

[ prometheus 서버 ] 도커로 컨테이너 빌드하기

블루빔 2023. 7. 27. 11:30

이번 글에서는 Dockerfile로 prometheus 커스텀 도커이미지를 생성하고 prometheus.yml 파일을 미리 작성 후 compose.yml 파일에서 호스트 볼륨을 참조하여 애플리케이션 배포를 실행해 보려고 합니다.

목차
▶ 전체적인 파일 구조
▶ 도커 이미지 파일
▶ 도커 이미지 빌드 및 확인
▶ prometheus.yml 파일 작성
▶ compose.yml 파일 작성
▶ 컨테이너 빌드 & 확인

 

전체적인 파일 구조

── prometheus
     ├── compose.yml
     ├── Dockerfile
     └── prometheus
        └── prometheus.yml

 

도커 이미지 파일 

  prometheus 디렉터리를 생성 후 하위에 Dockerfile을 작성합니다.

FROM prom/prometheus

user root
run sed -i 's/UTC0/UTC\-9/g' /etc/localtime

USER       nobody
EXPOSE     9090
VOLUME     [ "/prometheus" ]
ENTRYPOINT [ "/bin/prometheus" ]
CMD        [ "--config.file=/etc/prometheus/prometheus.yml", \
             "--storage.tsdb.path=/prometheus", \
             "--web.console.libraries=/usr/share/prometheus/console_libraries", \
             "--web.console.templates=/usr/share/prometheus/consoles", \
             "--storage.tsdb.retention.time=365d", \
             "--web.enable-lifecycle", \
             "--log.level=debug"]

 

 

도커 이미지 빌드 및 확인 

docker build . -t 도커이미지네임
docker images

예시 : 

 

prometheus.yml 파일 작성

prometheus config file로 사용될 파일입니다. 해당 글에서는 alerting 설정과 rule 설정을 진행하지 않고 stackdriver-exporter의 메트릭을 prometheus 서버에 가져올 수 있게만 작성하였습니다. 

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).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 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: ["192.168.0.35:9090"]

  - job_name: "stackdriver-exporter"
    # stackdirver exporter ip:port
    static_configs:
      - targets: ["192.168.0.35:9255"]

 

compose.yml 파일 작성

compose.yml 파일에 prometheus 설치에 필요한 서비스 설정들을 기재합니다. 

version: '3.8'

services:
    prometheus:
        container_name: prometheus-server
        image: prometheus-server-img
        restart: unless-stopped
        
        #####호스트 OS에서 공개할 포트:컨테이너내부 포트#####    
        ports:
            - "9090:9090"
        user: root
        
        ####호스트 경로:컨테이너내부 경로 ####
        volumes:
            - ./prometheus:/etc/prometheus
            - ./data:/prometheus

 

컨테이너 빌드 

docker compose up

 

확인

curl http://localhost:9090/metrics

 

콘솔에서 stackdriver agent 메트릭들이 정상 수집되고 있는지 확인하려면 외부에서 호스트 공인 ip : 9090으로 접속할 수 있게 방화벽을 오픈하여야 합니다. 

참고 이미지 :