ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로메테우스 Prometheus 시작하기(docker)
    인프라/모니터링 2021. 11. 4. 18:23

    프로메테우스란 - prometheus 란

    오픈소스 모니터링 툴로 지표 수집을 통한 모니터링이 주요 기능이다. 쿠버네티스 뿐만 아니라 애플리케이션이나 서버, OS등 다양한 대상으로부터 지표(Metric)를 수집하여 모니터링 할 수 있다. 기본적으로 Pull 방식으로 데이터를 수집하는데, 이 말은 모니터링 대상이 되는 자원이 지표정보를 프로메테우스로 보내는 것이 아니라, 프로메테우스가 주기적으로 모니터링 대상에서 지표를 읽어온다는 뜻이다(Push 방식으로 지표를 수집하는 모니터링 툴은 ELK스택 또는 Telegraf & InfluxDB 등이 있다). Pull 방식으로 지표정보를 읽어올때는 각 서버에 설치된 Exporter를 통해서 정보를 읽어오며, 배치나 스케쥴 작업의 경우에는 필요한 경우에만 떠 있다가 작업이 끝나면 사라지기 때문에 Pull 방식으로 데이터 수집이 어렵다. 그럴 경우 Push방식을 사용하는 Push gateway를 통해 지표정보를 받아오는 경우도 있다. 서버의 갯수가 정해져 있다면 프로메테우스에서 모니터링 대상을 관리하는데 어려움이 없지만, 오토스케일링이 많이 사용되는 클라우드 환경이나 쿠버네티스 클러스터에서는 모니터링 대상의 IP가 동적으로 변경되기 때문에 이를 일일이 설정파일에 넣는데 한계가 있다. 이러한 문제를 해결하기 위해 프로메테우스는 DNS나 Consul, etcd와 같은 다양한 서비스 디스커버리 서비스와 연동을 통해 모니터링 목록을 가지고 모니터링을 수행한다.

     

     

    version: "3.3" # 파일 규격 버전
    services: # 이 항목 밑에 실행하려는 컨테이너 들을 정의
      prometheus: # 서비스 명
        image: prom/prometheus      # 사용할 이미지
          # restart: always
        container_name: prometheus # 컨테이너 이름 설정
        ports:
          - "9090:9090" # 접근 포트 설정 (컨테이너 외부:컨테이너 내부)
        volumes:
          - /Users/폴더명/docker_voluems/prometheus/data/prometheus:/prometheus
        command: --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml

     

    프로메테우스 설정

    프로메테우스 도커 접속

    docker exec -it 도커명 /bin/sh

    프로메테우스 내부 prometheus.yml 설정

    # my global config
    global:
      scrape_interval:     15s # Set the scrape interval to every 15 seconds. Def
      evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is
      # 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 'eva
    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 scrap
      - job_name: 'spring-boot-app'
        metrics_path: '/actuator/prometheus'
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
    
        static_configs:
        - targets: ['host.docker.internal:8080']

      - job_name: 'spring-boot-app'
        metrics_path: '/actuator/prometheus' 
    이부분은 스프링 actuator 과 연동시킬때 사용.

    scrape_configs 는 데이터를 수집할 곳의 job 정보를 작성.

    위의 예시는 내부에서 사용하는 8080 포트로 시작하는 spring boot 의 정보를 스크랩한다. 

    도커로 프로메테우스를 실행하고 내부 localhost 라면 tarets 을 host.docker.internal 로 설정해야 함.

     

    Configuration prometheus.yml 기본 spring boot 와 설정.

    targets에 모니터링할 Spring Boot 애플리케이션 IP:Port 추가
    Service Discovery를 사용한다면 관련 Service Discovery 설정 추가

    global:
      scrape_interval: "15s"
      evaluation_interval: "15s"
    scrape_configs:
    - job_name: "springboot"
      metrics_path: "/actuator/prometheus"
      static_configs:
      - targets:
        - "<my_spring_boot_app_ip>:<port>"
    - job_name: "prometheus"
      static_configs:
      - targets:
        - "localhost:9090"

     

    그라파나

    바로 promethus 로 실행해서 모니터링 하기 보기가 어렵다.

    version: "3.3" # 파일 규격 버전
    services: # 이 항목 밑에 실행하려는 컨테이너 들을 정의
      grafana: # 서비스 명
        image: grafana/grafana    # 사용할 이미지\
        # restart: always
        container_name: grafana # 컨테이너 이름 설정
        ports:
          - "3000:3000" # 접근 포트 설정 (컨테이너 외부:컨테이너 내부)
        environment: # -e 옵션
          GF_SECURITY_ADMIN_PASSWORD: secret
        volumes:
          - /Users/폴더명/docker_voluems/grafana/grafana-storage:/var/lib/grafana

    Install and Run

    • 기본 포트: 3000
    • 기본 계정 ID/PW: admin/admin
    • GF_SECURITY_ADMIN_PASSWORD 옵션으로 admin 패스워드 변경 가능

     

    반응형

    '인프라 > 모니터링' 카테고리의 다른 글

    spring cloud resilience4j 모니터링  (0) 2022.11.03
    [ELK] logstash 설치 및 실행하기  (0) 2022.07.13
    telegraf 시작하기  (0) 2022.01.04
    Burrow 시작하기  (0) 2021.12.30
    ZIPKIN 시작하기  (0) 2021.11.25

    댓글

Designed by Tistory.