donaricano-btn

프로메테우스 알림매니저 실행 - Prometheuse alertmanager via slack


1. 정의

- 프로메테우스는 alert 시스템을 제공한다. 

- slack, hipchat 등 을 통하여 alert을 받을 수 있다.

- 만약 node_exporter 가 설치된 서버에 node_exporter가 죽으면 instance가 죽었다는 alert을 받는다.


2. AlertManager 구성

- https://prometheus.io/docs/alerting/overview/

- 두 파트로 나뉜다, Alert rules, Alert manager

- Alert rules가 Alert manager에게 alert 메시지를 전송하면 Alert manager는 슬랙, 이메일, 힙챗 등을 통하여 사용자게에 보여준다


3. AlertManger설치 및 설정

1) AlertManager 설치

- https://prometheus.io/download/ OS에 맞는 AlertManager를 다운받는다.

- simple.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
global:
  slack_api_url: 'https://xxxx.com/xxxxx/slack'
 
route:
  receiver: 'slack-notifications'
 
  group_by: ['alertname', 'cluster']
 
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  routes:
  - receiver: 'slack-notifications'
    group_wait: 10s
    match_re:
      service: mysql|cassandra
 
receivers:
- name: 'slack-notifications'
  slack_configs:
  - channel: 'test1'

- 해당 설정 파일을 통하여 AlertManager를 설정할 수 있다.

- 위 설정은 slack 을 이용한 alert를 받기 위한 설정이다. 

- global : 이 영역에 slack, hipchat 같은 api를 작성한다.

- route : 최상위 노드로서 group의 이름, 시간 등을 설정한다. 

- receivers : slack 채널을 설정

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

- ./alertmanager.exe --conifg.file=simple.yml 실행

- localhost:9093에 접속하면 AlertManger 화면에 접속할 수 있다. 


4. Alertrules 설정 및 Slack 연결 

- AlertManager를 설치 했다면 AlertRule를 설정한다. Rule 설정은 프로메테우스 서버에서 설정한다. 

1) Prometheus.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
  
  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'
  
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
   - alert.rules.yml
  # - second.rules
alerting:
  alertmanagers:
    - static_configs:
      - targets: ["localhost:9093"]
# A scrape configuration containing exactly one endpoint to scrape`:
# Here it's Prometheus itself.
scrape_configs:
......
.....
 

- rule_files : Alertrules 파일을 따로 만들어서 이곳에서 loading 되도록 한다.

- alerting :  AlertManger와 연결하는 부분

- 과거 버전(x): ./prometheus.exe --config.file=prometheus.yml --alertmanager.url=http://localhost:9093

- 최신 버전(o) : prometheus.yml 파일에 직접 셋팅한다.

2) alert.rules.yml

- 과거버전

1
2
3
4
5
6
7
8
9
ALERT InstanceDown
  IF up == 0
  FOR 5m
  LABELS { severity = "page" }
  ANNOTATIONS {
    # Prometheus templates apply here in the annotation and label fields of the alert.
    summary = "Instance {{ $labels.instance }} down",
    description = "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
  }
 

- 최신 버전

- ALERT RULE 포맷이 아닌 YAML 포맷을 따른다. 

1
2
3
4
5
6
7
8
9
10
11
12
groups:
- name: alert.rules
  rules:
  - alert: InstanceDown
    expr: up == 0
    for: 30s
    labels:
      severity: page
    annotations:
      description: '{{ $labels.instance }} of job {{ $labels.job }} has been down
        for more than 30 seconds.'
      summary: Instance {{ $labels.instance }} down
 

- 변환 방법

- rules 파일을 먼저 만들고 promtool 을 이용하여 update 하면 자동으로 변환된다.

- ./promtool.exe update rules alert.rules


5. 확인

- ./alertmanager.exe --config.file=simple.yml 을 띄운다

- ./prometheus.exe --config.file=prometheus.yml 실행

- 이후에 node_exporter가 설치된 서버에 접속하여 node_exporter 를 죽인다

- localhost:9090/alerts, localhost:9093 에 접속하여 확인






블로그 이미지

리딩리드

,
donaricano-btn

Grafana를 이용한 프로메테우스 시각화


1. Grafana

- 백엔드를 위한 모니터링 서포트 툴이다. 

- 시각화하여 다양한 정보를 보여준다. 


2. 그라파나(Grafana) 설치

- http://docs.grafana.org/installation/windows/

- 해당 주소에서 윈도우용을 다운받을 수 있다.

- 설치 이후에 서버를 띄운후 localhost:3000으로 접속하여 회원가입한다. 

- 처음 설치하면 아무것도 없다. 프러그인을 통하여 datasource 등을 설치한다. 

- 설치는 admin만 할 수 있다. (로그인 admin으로 해야함, 즉 회원가입 필요없음- -)


3. 프로메테우스를 위한 셋팅

1) add datasource

2) graph 

- home -> 자신이 추가한 prometheus 클릭 ->  graph를 클릭한다.

- 그래프에 제목을 클릭 -> edit

- edit 속성에서 화면에 그려질 query를 추가한다.



블로그 이미지

리딩리드

,
donaricano-btn

프로메테우스 아마존 EC2 자동 찾기 - Automatically monitoring EC2 Instances


1. 정의

- EC2를 클라이언트 서버로 사용하는 경우, 수동적으로 하나씩  EC2를 JOB으로 등록하기 보단 IAM 계정을 이용하여 모든 EC2를 자동으로 찾아 모니터링 할 수 있다. 

- 프로메테우스 설정의 <ec2_sd_config> 태그를 사용한다.

https://prometheus.io/docs/operating/configuration/#<ec2_sd_config>


2. IAM 계정 만들기

- 해당 설정을 하기위하여 AWS의 IAM Users계정을 만들어야한다. 

1) 권한은 AmazonEC2ReadOnlyAccess  으로 만든다.

2) ACCESS KEY, SECRET KEY를 반드시 기억해야한다. 

3) REGION 정보를 알아야한다. 

- REGION 정보가 ASIA PACIFIC(SEOUL) 이딴게 아니다. - - 삽질...

- 주소 같은거 보면 ap-northeast-2게 있다.


3. prometheus.yml 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
 
  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"
 
# 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'
 
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
 
    static_configs:
      - targets: ['localhost:9090']
 
  - job_name: 'node'
    ec2_sd_configs:
       - region: 'ap-northeast-2'
         access_key: ' 여기 키값'
         secret_key: '여기 키값'
         port: 9100
    relabel_configs:
      - source_labels: [__meta_ec2_tag_Name]
        target_label: instance
      - source_labels: [__meta_ec2_public_ip]
        regex: '(.*)'
        target_label: __address__
        replacement: '${1}:9100'
 
  - job_name: 'test-server'
 #  scrape_interval: 10s
 
    static_configs:
      - targets: ['111.222.333.444:9100']

- region : 위 같이 입력한다. 

- relabel_configs: 라벨 값을 변경하는 것이다. 라벨 값을 설정하지 않는다면 해당 ec2에 접속못할 것이다.

- 기본적으로 ec2_sd_configs를 설정하면 private ip가 설정된다. 그렇기 때문에  relabel 작업을통해 public으로 변경한다.

- 띄어쓰기 중요하다. 특히 enter, tab 등.....- - 

블로그 이미지

리딩리드

,
donaricano-btn

프로메테우스 node_exporter 연결하기 


1. 기본 구성

- AWS의 리눅스(111.222.333.444)에 node_exporter설치 -> window 프로메테우스 서버에서 상황 확인


2. node_exporter 설치

1) AWS 인스턴스에 접속한다. 

2) $ yum install wget, 설치 패키지 인스톨

3) $ wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz 실행하여 node_exporter 를 설치한다

- 설치 시, 위치는 상관없다. 

4) $ tar -zxvf node_exporter.... 압축해제 한다

5) $ cd node_exporter.... 이동

6) $ nohup ./node_exporter &  로 배경에서 데몬을 돌린다. 

7) 기본적으로 포트 9100을 사용한다.


3. AWS 설정

- security_group을 설정하여 해당 아이피와 포트에 접근할 수 있도록 추가한다. 


4. prometheus.yml 설정

- 윈도우가 설치된 서버(프로메테우스서버) 로 돌아와 설정 파일에 잡을 추가한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
 
  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"
 
# 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'
 
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
 
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'test-server'
 
    scrape_interval: 10s
 
    static_configs:
      - targets: ['111.222.333.444:9100']


5. web browser에서 확인

- node_exporter metric 정보가 올바르게 출력가능한지 해당 ip와 port에 접속해서 확인한다. 

- 111.222.333.444:9100/metrics

- localhost:9090/targets에 접속하여 서버의 상태를 확인할 수 있다. 

- 이후에 node_cpu 쿼리를 이용하여 해당 서버의 cpu 상태를 확인 할 수 있다. 

- 쿼리문 중에 node_* 쿼리들이 자동 생성됨



블로그 이미지

리딩리드

,