Skip to content

Server와 Grafana연동하기

KWAKMANBO edited this page Jan 21, 2025 · 1 revision

🤔 Grafana란?

  • 시각화 및 분석도구
  • 시계열 매트릭 데이터를 시각화 하는데 최적화된 대시보드를 제공해주는 오픈소스 툴킷
  • Grafana를 이용해서 다양한 DB를 연결하여 DB의 데이터를 가져와 시각화 할 수 있음
    • Prometheus, InfluxDB, Elasticserach등의 데이터 소스와 통합이 가능
  • 간단하게 클릭으로 그래프를 그릴 수 있음
  • Grafana는 시계열 매트릭스 데이터 수집에 특화되어 있는 만큼, 활용하는 부분도 서버 리소소의 매트릭 정보나 로그 같은 데이터를 시각화 하는데 많이 사용함
    • 시각화한 그래프에서 특정 수치 이상으로 값이 치솟을 때 알림을 전달 받을 수도 있음
  • Grafana와 Prometheus 둘다 Grafana Labs에서 관리하고 있어, 궁합이 좋은편이라 이 둘을 같이 사용하는 경우가 일반적임

🤔 Promehteus란?

  • SoundCloud에서 만든 오픈소스 모니터링 툴 Pasted image 20250120170636
  • Prometheus는 위와같은 구조를 가짐
  • Prometheus가 취급하는 데이터는 전부 시계열 데이터로, 시계열 메트릭 데이터를 수집하고 저장하는 데이터 베이스 역할을 수행
  • 왼쪽 및 Jobs/exporters가 메트릭을 수집하는 역할을 수행하고, Prometheus server는 수집한 데이터를 pull을 통해서 가져옴
  • 오른쪽 Alertmanager은 수집한 메트릭 정보가 rule에 위반된 경우 다양한 미디어를 통해 알림을 보내줌
  • Grafana는 수집한 메트릭 정보를 시각화 해주는 visual dashboard 역할을 수행
  • 오픈소스 모니터링 alerting 툴
  • 매트릭을 수집하고, 저장하고, 쿼리를 통해서 데이터를 조회할 수 있음.
  • 시계열 데이터를 기반으로 하기 때문에, 매트릭 정보는 timestamp와 함께 저장됨
  • prmetheus는 Pull 방식을 사용해서 주기적으로 클라이언트로부터 데이터를 가져오는 방식을 사용
  • Prometheus는 메트릭 지표 수집에 nodejs exporter를 띄워서 사용

🔨Prometheus를 설치해보자

  • GrafanaPrometheus를 사용하는 조합이 일반적이라고 하니 이 두개를 사용해보려한다.
  • Grafana는 데이터를 시각화 해주는 툴이고, Prometheus는 데이터를 수집하는 툴이므로 데이터를 수집하는Prometheus부터 먼저 설치해보자

Prometheus 설치하기

docker pull prom/prometheus
  • prometheus이미지를 먼저 pull해오자

Prometheus 실행하기

  • Prometheus를 실행하기전에 설정파일을 volume에 마운트 해줘야한다.
  • prometheus.yml파일을 마운트 해줘야하는데 밑에서 자세하게 설명할예정이므로 간단하게 형식만 알고 넘어가자
  • Prometheus yml에서 좀더 자세한 방법을 알 수 있다.
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']
  • prometheus.yml의 기본적인 구조는 위와같음
  • globalprometheus의 전역설정을 의미함
    • scrape_interval exporte로부터 데이터를 pull 해오는 주기를 의미 여기서는 15초 주기를 사용
    • external_labels은 Prometheus가 외부 시스템과 통신할 때 모든 시계열 데이터에 추가될 전역 데이터를 의미
    • 외부 시스템은 alert manager, remote storage등을의미
  • scrape_configs에서는 Prometheus가 어떤 타겟에서 메트릭 데이터를 수집할지 정의
    • job_name : 하나의 job을 정의하며, 데이터에 라벨로 추가됨
    • scrape_interval 데이터 수집 주기를 의미, global로 설정해도 scrape_config값이 덮어씌워짐
  • static_configs에서는 고정된 타겟의 엔드포인트를 정의
    • targets: ['localhost:9090']Prometheus가 실행중이 서버 자체에서 데이터를 수집한다는 의미
    • [도메인(ip):port]으로 작성해주면됨

⚒️ Grafana를 설치해보자

  • 환경구축을 쉽게 하기위해서 docker를 이용해서 grafana를 이용해볼 예정이다.

grafana 설치하기

docker pull grafana/grafana
  • 위 명령어로 grafana 이미지를 pull해오자

grafana 실행하기

docker run -d --name=grafana -p 3000:3000 grafana/grafana
  • 위 처럼 grafana를 실행할 수 있다.우리는 docker compose를 사용할 예정이라 grafana 를 실행하는 방법만 간단하게 알아보고 넘어가자

🤝 Grafana와 Prometheus 연동하기

  • 이제 서버에서 Grafana와 Prometheus를 연동하는 방법을 알아보자
  • Grafana와 Prometheus 연동 전에 Backend Server에 Node Exporter를 설치해서 메트릭 데이터를 수집하는 것부터 등록이 필요했다.

1. docker-compose를 통해 Grafana와 Prometheus 환경 구축하기

version: '3'  
  
services:  
  prometheus:  
    image: prom/prometheus  
    container_name: prometheus  
    volumes:  
      - ./prometheus.yml:/prometheus/prometheus.yml:ro  
    ports:  
      - 9090:9090  
    command:  
      - "--web.enable-lifecycle"  
    restart: always  
    networks:  
      - promnet  
    user: root  
  
  grafana:  
    image: grafana/grafana  
    container_name: grafana  
    volumes:  
      - ./grafana-volume:/var/lib/grafana  
    restart: always  
    networks:  
      - promnet  
    ports:  
      - 3000:3000  
    user: root  
  
networks:  
  promnet:  
    driver: bridge
  • docker-compose.yml파일을 통해서 GrafanaPrometheus를 연동할 수 있도록한 yml파일
  • 이때 prometheus가 데이터를 수집할 도메인의 엔드포인튼 prometheus.yml에 명시해야된다.
global:  
  scrape_interval: 15s  
  scrape_timeout: 15s  
  evaluation_interval: 15s  
  
  external_labels:  
    monitor: 'codelab-monitor'  
    query_log_file: /var/log/prometheus/query_log_file.log  
  
scrape_configs:  
  - job_name: 'monitoring_prometheus'  
    scrape_interval: 10s  
    scrape_timeout: 10s  
    metrics_path: '/metrics'  
    scheme: 'http'  
  
    static_configs:  
      - targets: [ 'localhost:9090' ]  
        labels:  
          service: 'monitor'  
  
  - job_name: 'public_server'  
    scrape_interval: 10s  
    scrape_timeout: 10s  
    metrics_path: '/metrics'  
    scheme: 'http'  
  
    static_configs:  
      - targets: [ '10.0.0.6:9100' ]  
        labels:  
          group: 'node_exporter'
  • 우리 프로젝트 상황에 맞게 작성한 prometheus.yml파일
    • 아직 MySQL exporter와 NestJS Exporter를 적용하지 못해서 내용이 빠져있지만, 지금 당장 필요한 Node Exporter만 사용해볼 예정이다.
docker compose up -d

Pasted image 20250121165012

  • 위 명령어로 실행해보니 Pasted image 20250121165222 Pasted image 20250121165242

  • 정상적으로 GrafanaPrometheus가 실행되고 있는 것을 확인할 수 있다.

2. Grafana와 Prometheus 연동

Pasted image 20250121165853

  • 공인IP:3000 으로 Grafana에 접속해서 Data Source에 Promethus를 추가해줘야한다.
  • Data Soruces탭에서 Prometheus를 찾고 추가해보자
  • Connectionhttp://localhost:9090 말고 http://prometheus:9090을 입력해줘야한다. GrafanaPrometheus가 같은 네트워크로 묶여있어서 컨테이너 이름으로 접근하는게 가능하다고 한다. Pasted image 20250121170739
  • Data Source가 추가된것을 확인할 수 있다.

대쉬보드 디자인 고르기

  • Grafana Labs에서 대쉬보드 디자인을 고를 수 있으니 맘에드느걸로 골라주자

  • 우리는 Node Exporter를 사용하니 Node Exporter 대쉬보를 사용려한다. ID는 1860이다. Pasted image 20250121171018

  • Dashboard 탭에서 방금 고를 대쉬보드 디자인을 import해주자 Pasted image 20250121171058

  • ID를 입력한 후 DataSource를 입력하면 Import할 수 있다. Pasted image 20250121171142

  • 성공적으로 뜨는것을 확인할 수 있다.

Pasted image 20250121172150

  • 부하테스트를 하니 CPU 점유량이 올라가는것을 확인할 수 있었다.

참고문헌

팀 빌딩

📚팀 빌딩
📝Git 전략

회의록

1주차

🤝1월 7일
🤝1월 8일
🤝1월 9일

2주차

🤝주간 계획(1월 13일)

3주차

🤝주간 계획(1월 20일)

인공지능 리팩토링 1주차

🤝주간 계획(2월 3일)

인공지능 리팩토링 2주차

🤝주간 계획(2월 10일)

개발일지

AI 리팩토링 기획안

AI 리팩토링 개발일지

성능개선

리팩토링

팀회고

학습 정리

Clone this wiki locally