devops2023-08-01·8 min·344/348

Prometheus 모니터링 설정 에러 해결

Prometheus 모니터링 시스템 설정 시 발생하는 에러를 진단하고 해결하는 방법을 알아봅니다.

Prometheus 모니터링 설정 에러 해결

Prometheus는 강력한 모니터링 시스템이지만, 설정 파일 구성과 메트릭 수집에서 various 문제가 발생할 수 있습니다.

Environment

$ prometheus --version
prometheus, version 2.48.1

$ promtool --version
promtool, version 2.48.1

$ curl -s http://localhost:9090/-/healthy
Prometheus Server is Healthy.

Problem: Target Down 또는 Scrape 실패

# Prometheus UI에서 확인
$ curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[].health'
"down"
"down"

에러 로그:

$ docker logs prometheus 2>&1 | tail -20
level=error ts=2024-01-15T10:30:00.123Z caller=scrape.go:1391 msg="Scrape failed" 
err="Get \"http://localhost:8080/metrics\": dial tcp 127.0.0.1:8080: connect: connection refused"

Analysis: 설정 파일 검증

# prometheus.yml 검증
global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  
  - job_name: 'myapp'
    static_configs:
      - targets: ['localhost:8080']
# 설정 파일 문법 검증
$ promtool check config prometheus.yml

# 메트릭 직접 확인
$ curl -s http://localhost:8080/metrics | head -20

# Target 상태 확인
$ curl -s http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | {instance: .labels.instance, health: .health, lastError: .lastError}'

Solution: Prometheus 설정 에러 해결

방법 1: 설정 파일 검증

# prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s
  
  # 추가 설정
  scrape_timeout: 10s
  external_labels:
    cluster: 'production'
    environment: 'prod'

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  
  - job_name: 'node-exporter'
    static_configs:
      - targets: ['node1:9100', 'node2:9100']
    
    # 타임아웃 설정
    scrape_timeout: 5s
    
    # 레이블 리라이팅
    relabel_configs:
      - source_labels: [__address__]
        regex: '(.+):(\d+)'
        target_label: instance
        replacement: '${1}'

방법 2: Service Discovery 설정

# Kubernetes 서비스 디스커버리
scrape_configs:
  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
    
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)

# Docker Swarm 디스커버리
scrape_configs:
  - job_name: 'docker'
    docker_sd_configs:
      - host: unix:///var/run/docker.sock
        filters:
          - name: label
            values: ['prometheus.scrape=true']

방법 3: 알림 규칙 설정

# alerts.yml
groups:
  - name: example
    rules:
      - alert: HighCPUUsage
        expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "High CPU usage on {{ $labels.instance }}"
          description: "CPU usage is above 80% for 5 minutes"
      
      - alert: HighMemoryUsage
        expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 90
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "High memory usage on {{ $labels.instance }}"

방법 4: Recording Rules

# recording_rules.yml
groups:
  - name: node
    rules:
      - record: instance:node_cpu_utilization:rate5m
        expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
      
      - record: instance:node_memory_utilization:ratio
        expr: 1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)

방법 5: Federation 설정

# federate.yml - Central Prometheus scraping from local instances
scrape_configs:
  - job_name: 'federation'
    honor_labels: true
    metrics_path: '/federate'
    
    params:
      'match[]':
        - '{job=~".+"}'
        - 'instance:node_cpu_utilization:rate5m'
    
    static_configs:
      - targets:
          - 'prometheus-1:9090'
          - 'prometheus-2:9090'
          - 'prometheus-3:9090'

Troubleshooting Commands

# 1. 설정 검증
$ promtool check config prometheus.yml
$ promtool check rules alerts.yml

# 2. 쿼리 테스트
$ promtool test rules test.yml

# 3. Target 상태 확인
$ curl -s http://localhost:9090/api/v1/targets | jq

# 4. 메트릭 확인
$ curl -s 'http://localhost:9090/api/v1/query?query=up' | jq

# 5. 리로드
$ curl -X POST http://localhost:9090/-/reload
$ kill -HUP $(pgrep prometheus)

# 6. 로그 확인
$ journalctl -u prometheus -f

Lessons Learned

  1. 설정 검증 필수: promtool check config로 설정 파일 문법을 반드시 검증하세요.

  2. scrape_timeout 설정: 대상이 응답하지 않을 때 무한 대기를 방지하려면 scrape_timeout을 설정하세요.

  3. ** relabel_configs 활용**: 메트릭을 수집하기 전에 레이블을 수정하거나 필터링할 때 유용합니다.

  4. Recording Rules: 자주 사용되는 쿼리는 미리 계산하여 Prometheus 부하를 줄이세요.

  5. 알림 테스트: 알림 규칙을 배포 전에 promtool test rules로 테스트하세요.


This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.