DevOps/Prometheus

Prometheus_

게임이 더 좋아 2023. 1. 13. 10:12
반응형
728x170

정의

시계열 데이터들을 모니터링하기 좋은 시스템 도구

특히 로그보다는 메트릭을 수집하여 모니터링할 때 빛나는 도구

Pull-Based Model => target을 지정해주어야 함

 

 

 

 

설치

여러가지 경로로 다운로드 받을 수 있음 - https://prometheus.io/download/

# 다른 버전 사용 시,다른 버전에 맞추어 설치하면 됨
wget https://github.com/prometheus/prometheus/releases/download/v2.37.0/prometheus-2.37.0.linux-amd64.tar.gz


# 압축 해제 및 이름 변경
tar -xvf ./prometheus-2.37.0.linux-amd64.tar.gz
mv prometheus-2.37.0.linux-amd64.tar.gz prometheus

 

※프로메테우스는 기본적으로 9090 port를 listen 하고 있음 (http://localhost:9090)

 

 

환경 설정

 

일반적으로 3가지 section이 존재

  1. global configuration → 전반적인 설정
  2. scrape configuration → 어디서 pulling 할 것인지
  3. alerting rules → 어떤 상황에서 alert할 것인지

 

prometheus.yml

# 프로메테우스 동작에 대한 configuration
global:
  external_labels:
    environment: production
  scrape_interval: 15s # Pulling 하는 주기, 초기 값 1분
  evaluation_interval: 15s # rules 들을 evaluate 하는 주기, 초기 값 1분
  # scrape_timeout is set to the global default (10s).

scrape_configs:
	#  The job name is used to identify the metrics that are scraped from a particular target and can be used to filter and query the metrics in the Prometheus web UI or API.
   - job_name: 'webapp'

	# metrics_path의 기본 경로는 '/metrics'이고 scheme의 기본 값은 `http`다
    static_configs:
		#The targets can be specified as a list of URLs or IP addresses.
      - targets: ['localhost:8080']

# 규칙을 로딩하고 'evaluation_interval' 설정에 따라 정기적으로 평가한다.
rule_files:
  # - "first.rules"
  # - "second.rules"



alerting:
  #어디다 Alert을 보낼지 설정, alert managers can be specified as a list of URLs or IP addresses.
  alertmanagers:
  - static_configs:
    - targets:
      - 'localhost:9093'

  #Alert를 보내는 포맷 및 조건, a boolean expression that determines when the alert should be triggered, and optional labels and annotations that provide additional context for the alert.
   rules:
  - alert: 'WebappResponseTimeHigh'
    expr: avg(webapp_response_time_seconds) > 0.5
    for: 1m
    labels:
      severity: high
    annotations:
      summary: 'Webapp response time is high'

 

 

각 섹션에 대해 더 자세히 알아보면

 

scrape_configs 

scrape_configs:
  - job_name: example
    scrape_interval: 30s
    metrics_path: /custom_metrics_path # scrape를 하는 경로, default는 '/metrics'를 쓰고 있음
    scheme: https # default로는 'http'를 쓰고 있음
    params:     # scrape URL에 query parameter를 활용할 수 있음
      param1: [value1, value2]
      param2: [value3]
    static_configs: # 실제로 target이 되는 path를 가지고 같은 그룹에 있는 target들은 label set을 공유함
      - targets: ['localhost:9090']
      - targets: ['localhost:9091']
        labels:
          group: test
    relabel_configs: # 스크래핑 전에 타겟 레이블에 적용되는 relabeling rule에 대한 목록, 이러한 rule은 target에서 label을 추출하거나, 새로운 label을 더하거나 수정할 때 사용하게 됨
      - source_labels: [__address__] #rule을 적용해야 하는 대상 label name
        target_label: instance # rule을 적용해서 새로운 label을 만들거나 수정할 때의 대상 label name
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
        target_label: scrape
        action: keep # rule이 매칭됐을 때 해야하는 Action : ( keep | drop | replace )
      - target_label: __address__
        replacement: 127.0.0.1:9100  # The blackbox exporter.
      - source_labels: [__meta_kubernetes_namespace]
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_pod_name]
        target_label: kubernetes_pod_name

특히 relabel_configs는 3가지 역할을 함

  1. Extract label
  2. Add a new label
  3. Modify an existing label

 

1:

regex를 이용해서 label을 매칭시킬 수 있음

예를 들어 instance label을 __address__ label에서 추출하려면 __address__ 를 source_label로 instance를 target_label로 설정하면 됨

2:

group : 'test' 라는 label을 새롭게 추가하려면 'group'을 target_label로 'test'를 replacement로 설정하면 됨

3:

기존의 label을 수정하려면 기존의 label을 target_label에 바꾸려는 label을 replacement로 설정하면 됨

 

 

 

global_configs

global:
  scrape_interval: 30s #scrape를 하는 주기
  evaluation_interval: 1m # rule을 evaluation 하는 주기
  external_labels: # 전역으로 label을 선언하는 것 -> 모든 메트릭에 대해 identification을 가짐
    environment: production
    datacenter: dc1

특히 external_label에 대해서 

environment와 datacenter 라는 label이 추가 되었는데 

이러한 추가적으로 label을 만들어 붙임으로써 프로메테우스가 수집하는 메트릭에 대한 추가적 context를 제공하는 것임

이러한 label을 통해 querying을 하여 필터링할 수 있음

 

 

alerting

alerting:
  alertmanagers: # Alert를 보낼 대상
    - static_configs: # Alert를 보낼 특정 경로
        - targets:
          - localhost:9093
          - localhost:9094
    - static_configs:
        - targets:
          - localhost:9095
          - localhost:9096
  alert_relabel_configs: # scrape label과 같음
    - source_labels: [alertname]
      target_label: alert_type
      action: keep
    - source_labels: [instance]
      target_label: host
      regex: (.*):.*
      replacement: $1
    - target_label: datacenter
      replacement: dc1
  inhibit_rules: # alert에 대해 다시 한 번 필터링을 거쳐서 alertmanager에게 전달할지 최종적으로 결정
    - source_labels: [alertname]
      target_label: alertname
      regex: degraded.*
      equals: [service_down]
  rules: # Alert을 발생시키는 조건
  - alert: HighCPUUtilization # Alert name
    expr: avg(cpu_utilization) by (server_group) > 0.7 and time() > 5m # Alert expression은 promQL 로 작성되어 있음 : {5분 이상} {해당 서버 그룹}의 {CPU의 점유율}이 {70%}가 {넘을 때}
    labels:
      severity: critical
      type: performance
    annotations:
      summary: "High CPU utilization on {{ $labels.server_group }}"
      description: "The average CPU utilization for the {{ $labels.server_group }} server group is currently at {{ $value }}%."

 

실행

#해당 yml 파일 설정으로 프로메테우스 실행
./prometheus --config.file=prometheus.yml

 

 

 


참고 링크

 

https://prometheus.io/docs/prometheus/latest/configuration/configuration/

반응형
그리드형

'DevOps > Prometheus' 카테고리의 다른 글

Prometheus - Practice  (0) 2023.07.09
Exporter 연동  (0) 2023.01.13
프로메테우스, Prometheus Up & Running - ch.1  (0) 2022.11.07
Prometheus, 프로메테우스 기본 개념  (0) 2022.10.02