Prometheus, Grafana 간단 적용(배포환경/Docker)

LeeYulhee·2023년 8월 13일
0

👉 스프링부트 프로젝트에 설정


  • docker-compose.yml
    version: "3.7"
    
    services:
      prometheus:
        image: prom/prometheus
        volumes:
          - 해당 파일의 경로/prometheus.yml:/etc/prometheus/prometheus.yml  # 현재 디렉토리의 prometheus.yml 파일을 컨테이너의 /etc/prometheus/prometheus.yml 위치에 마운트
        ports:
          - "9090:9090"
    
      grafana:
        image: grafana/grafana
        ports:
          - "3000:3000"
    • 📌 파일 설명
      • docker-compose.yml 파일은 여러 Docker 컨테이너들을 정의하고 실행하기 위한 설정을 담고 있음
      • volumes로 프로젝트 내에 만든 prometheus.yml 파일을 원래 위치해야 하는 파일 위치와 마운트

  • prometheus.yml
    # my global config
    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    # Alertmanager configuration
    alerting:
      alertmanagers:
        - static_configs:
            - targets:
    
    rule_files:
    scrape_configs:
      - job_name: "prometheus"
        static_configs:
          - targets: ["172.17.0.1:9090"]
    
      - job_name: "spring-actuator"  # 아무거나 가능
        metrics_path: '/actuator/prometheus'
        scrape_interval: 1s
        static_configs:
          - targets: [ '172.17.0.1:8080' ]
    • 📌 파일 설명
      • targets으로 local 수준에서 서로 통신할 수 있게 설정
        • 현재는 서버가 1대일 때 기준이라 local로 설정해도 문제가 없지만 서버가 늘어나면 아마 다른 설정이 필요할 듯



👉 도커에서 Prometheus 및 Grafana 실행


  • docker compose up -d
    • docker compose로 실행된 서비스들을 백그라운드에서 실행
  • 만약 “docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: (중략)” 와 같은 오류가 뜨는 경우
    • docker-compose.yml에 적은 prometheus.yml 파일의 경로가 잘못되었거나 없는 경우
    • 파일이 배포 환경에 없으면 vi prometheus.yml로 파일 생성해서 스프링부트에 작성한 내용 복사 붙여넣기 해서 저장 후 실행



👉 NPM(Nginx Proxy Manager)에서 /actuator로 접근 불가하게 설정


  • /actuator로 Prometheus와 Grafana가 정보를 주고받기 때문에 보안의 이유로 설정
    location ~ ^/actuator {
        deny all;
        return 403;
    }
    • NPM에서 Proxy Host → 사이트 도메인 → … → Edit → Advanced에서 설정
    • 이렇게 설정하면 Prometheus에는 URL을 통해 직접적으로 접근하지 못하게 될 수 있고, Grafana의 대시보드를 통해 모니터링 해야 함
profile
공부 중인 신입 백엔드 개발자입니다

1개의 댓글

comment-user-thumbnail
2023년 8월 13일

큰 도움이 되었습니다, 감사합니다.

답글 달기