본문 바로가기

서버 리뷰/Proxmox

Prometheus와 Grafana를 이용한 데이터 수집 및 시각화 구축 가이드

728x90
반응형

이 가이드는 Prometheus를 사용하여 메트릭 데이터를 수집하고, Grafana를 통해 이를 시각화하여 대시보드 형태로 표시하는 시스템을 구축하는 상세한 단계를 안내합니다. Prometheus는 오픈소스 모니터링 및 알림 도구로, 시간 기반 메트릭을 수집합니다. Grafana는 이러한 데이터를 시각화하는 강력한 대시보드 도구입니다.

이 가이드는 Linux 기반 시스템 (예: Ubuntu)을 기준으로 하며, Docker를 사용한 설치도 포함합니다. 실제 환경에 맞게 조정하세요. 모든 명령어는 root 또는 sudo 권한으로 실행하세요.

1. 사전 요구사항 (Prerequisites)

  • 운영 체제: Linux (Ubuntu 20.04 이상 추천), macOS, 또는 Windows (Docker 사용 시).
  • 필요한 패키지: wget, tar, curl 등 기본 유틸리티.
  • 포트: Prometheus 기본 포트 9090, Grafana 기본 포트 3000 (방화벽에서 열기).
  • 하드웨어: 최소 2GB RAM, 2코어 CPU (테스트 환경 기준).
  • Docker (선택): 컨테이너화된 설치 시 사용.
  • 기본 지식: YAML 설정 파일 편집, 기본 명령어 사용.

2. Prometheus 설치 및 설정

Prometheus를 설치하고 기본 메트릭 수집을 설정합니다.

2.1. 바이너리 설치 (Linux)

  1. Prometheus 다운로드:

    wget https://github.com/prometheus/prometheus/releases/download/v2.53.1/prometheus-2.53.1.linux-amd64.tar.gz
    tar xvfz prometheus-2.53.1.linux-amd64.tar.gz
    cd prometheus-2.53.1.linux-amd64
  2. Prometheus 실행:

    ./prometheus --config.file=prometheus.yml
    • 브라우저에서 http://localhost:9090에 접근하여 확인.
  3. 시스템 서비스로 등록 (백그라운드 실행):

    • 사용자 생성: sudo useradd --no-create-home --shell /bin/false prometheus

    • 디렉토리 생성: sudo mkdir /etc/prometheus /var/lib/prometheus

    • 파일 이동:

      sudo cp prometheus promtool /usr/local/bin/
      sudo cp -r consoles console_libraries /etc/prometheus
      sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
    • systemd 서비스 파일 생성 (/etc/systemd/system/prometheus.service):

      [Unit]
      Description=Prometheus
      Wants=network-online.target
      After=network-online.target
      
      [Service]
      User=prometheus
      Group=prometheus
      Type=simple
      ExecStart=/usr/local/bin/prometheus \
          --config.file /etc/prometheus/prometheus.yml \
          --storage.tsdb.path /var/lib/prometheus/ \
          --web.console.templates=/etc/prometheus/consoles \
          --web.console.libraries=/etc/prometheus/console_libraries
      
      [Install]
      WantedBy=multi-user.target
    • 서비스 시작: sudo systemctl daemon-reload && sudo systemctl start prometheus && sudo systemctl enable prometheus

2.2. Docker 설치

  1. Docker 이미지 pull:

    docker pull prom/prometheus
  2. 컨테이너 실행:

    docker run -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
    • /path/to/prometheus.yml은 호스트의 설정 파일 경로.

2.3. Prometheus 설정 (prometheus.yml)

기본 설정 파일을 편집하여 메트릭 수집 타겟을 지정합니다. 예시:

global:
  scrape_interval: 15s  # 메트릭 수집 주기

scrape_configs:
  - job_name: 'prometheus'  # Prometheus 자체 모니터링
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node'  # Node Exporter (시스템 메트릭 수집, 별도 설치 필요)
    static_configs:
      - targets: ['localhost:9100']
  • Node Exporter 설치 (옵션, 시스템 메트릭 수집):

    wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
    tar xvfz node_exporter-1.8.2.linux-amd64.tar.gz
    cd node_exporter-1.8.2.linux-amd64
    ./node_exporter
    • 서비스로 등록 (Prometheus와 유사).
  • 설정 적용 후 Prometheus 재시작: sudo systemctl restart prometheus

3. Grafana 설치 및 설정

Grafana를 설치하고 Prometheus를 데이터 소스로 연결합니다.

3.1. 바이너리 설치 (Linux)

  1. Grafana 다운로드 및 설치:

    sudo apt-get install -y apt-transport-https software-properties-common wget
    sudo mkdir -p /etc/apt/keyrings/
    wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg > /dev/null
    echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
    sudo apt-get update && sudo apt-get install grafana
  2. 서비스 시작:

    sudo systemctl start grafana-server && sudo systemctl enable grafana-server
    • 브라우저에서 http://localhost:3000에 접근 (기본 로그인: admin/admin).

3.2. Docker 설치

docker run -d -p 3000:3000 --name=grafana grafana/grafana

3.3. Grafana 초기 설정

  1. 로그인 후 비밀번호 변경.
  2. 데이터 소스 추가:
    • 왼쪽 메뉴 > Connections > Data sources > Add data source > Prometheus 선택.
    • URL: http://localhost:9090 (Prometheus 서버 주소).
    • Save & Test 클릭하여 연결 확인.

4. 대시보드 생성 및 시각화

Grafana에서 Prometheus 데이터를 시각화합니다.

4.1. 새 대시보드 생성

  1. 왼쪽 메뉴 > Dashboards > New > New Dashboard.
  2. Add a new panel 클릭.

4.2. 패널 설정 (시각화 예시)

  • 쿼리: Prometheus 쿼리 언어 (PromQL) 사용.
    • 예: CPU 사용률: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
    • 메트릭 선택: Data source를 Prometheus로 설정 후, Metrics browser에서 메트릭 검색.
  • 시각화 유형: Graph, Gauge, Table 등 선택.
    • Graph: 시간 경과에 따른 변화 표시.
    • Gauge: 현재 값 표시 (예: 메모리 사용량).

4.3. 고급 대시보드 기능

  • 변수 추가: 대시보드 상단 > Settings > Variables > Add variable.
    • 예: 인스턴스 선택 변수: Query = label_values(node_uname_info, instance)
  • 템플릿팅: 여러 타겟을 동적으로 표시.
  • 알림 설정: 패널 > Alert 탭에서 조건 설정 (예: CPU > 80% 시 알림).
  • 가져오기: Grafana Labs에서 미리 만들어진 대시보드 임포트 (예: ID 1860 for Node Exporter Full).
    • Dashboards > Import > ID 입력 또는 JSON 업로드.

4.4. 예시 대시보드 구성

  • 패널 1: 시스템 CPU 사용률 (Graph).
  • 패널 2: 메모리 사용량 (Gauge).
  • 패널 3: 디스크 I/O (Table).
  • 레이아웃: 행/열로 배치, 크기 조정.

5. 테스트 및 트러블슈팅

  • Prometheus UI (http://localhost:9090/targets): 타겟 상태 확인 (UP인지).
  • Grafana Explore: 쿼리 테스트.
  • 일반 오류:
    • 연결 실패: 포트 확인 (sudo ufw allow 9090, sudo ufw allow 3000).
    • 메트릭 없음: prometheus.yml에서 scrape_configs 확인.
    • 로그 확인: journalctl -u prometheus 또는 journalctl -u grafana-server.
  • 보안: Grafana에서 API 키 생성, Prometheus에 basic auth 추가.

6. 베스트 프랙티스 및 확장

  • Federation: 대규모 클러스터 시 Prometheus federation 사용.
  • Alertmanager: Prometheus와 연동하여 알림 (Slack, Email).
  • 백업: Prometheus 데이터 디렉토리 (/var/lib/prometheus) 백업.
  • 클라우드: AWS, GCP에서 관리형 서비스 (Amazon Managed Service for Prometheus + Grafana) 사용.
  • 문서 참조: Prometheus 공식 문서 (prometheus.io), Grafana 문서 (grafana.com/docs).

이 가이드를 따라하면 기본적인 모니터링 시스템을 구축할 수 있습니다. 실제 프로덕션 환경에서는 보안과 스케일링을 추가로 고려하세요.

반응형