basic

source-code : https://github.com/teamsmiley/devops-public/tree/main/monitoring/2.grafana-provisioning

서버나 프로그램을 모니터링 하기 위해 사용한다.

특징은 pull방식이라는것. 이것이 신의 한수

Prometheus 서버가 노드에 데이터를 요청하면 노드나 프로그램등은 데이터를 보내주면 된다. 노드는 전혀 prometheus 서버에 대해 알 필요가 없고 데이터의 전달에 대해서 고민하지 않아도 된다.

  • push : 각각 모니터되는 서버에서 주기적으로 모니터링 서버로 데이터를 보내는 방식

  • pull : 각각 모니터되는 서버에서 데몬만 돌고있고 모니터링 서버에서 request를 던저서 pull하는 방식

구성

  • prometheus : 데이터를 요청하고 가져온 데이터를 저장하고 쿼리함

  • exporter: 요청을 받으면 데이터를 만들어서 요청자에게 리턴해 준다.

  • push gateway : 데몬형태로 프로그램이 떠있는게 아니고 배치형태로 실행되고 사라지는 프로세스는 pull을 할수가 없으므로 이걸 이용해서 prometheus에 보내준다.

  • alarm manager : 데이터가 특정 조건이 되면 알림을 알려준다. (슬랙 또는 이메일 등등 많다.)

  • dashboard : 웹화면으로 데이터를 보여준다. 기본적인 화면이 prometheus가 가지고 있으니 Grafana를 대부분 사용한다.

모니터링할 노드 설정 추가

https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus.yml

global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

prometheus.yml을 설정하고 나서 도커를 실행하자.

docker-compose up -d

서비스가 실행되면 로컬에서 확인해보자

이제 웹브라우저를 통해 http://localhost:9090 을 확인해보면 알수 있다.

![]({{ site.baseurl }}/assets/2019-11-25-15-25-24.png)

이제 확인해보자.

다음그림처럼 up을 넣고 엔터를 처보면

![]({{ site.baseurl }}/assets/2019-11-25-15-29-07.png)

up은 Prometheus가 추가하는 특별한 형태의 메트릭이다. 1은 성공이라는 의미이다.

모니터링 하기

  • Node Exporter 설치 (linux)

이제 모니터링 할 서버를 설정하자. Prometheus가 요청하면 데이터를 만들어서 준다.

Node Exporter는 cpu 메모리 디스크공간 디스크 i/o 네트워크 대역폭 같은 모든 표준 메트릭을 제공한다.

노드 exporter는 바이너리로 설치

https://prometheus.io/download

wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

tar -xvzf node_exporter-0.18.1.linux-amd64.tar.gz

cd node_exporter-0.18.1.linux-amd64/

mv node_exporter /usr/local/bin/

vi /etc/systemd/system/node_exporter.service

[Unit]
Description=Node Exporter
After=network.target

[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
systemctl daemon-reload

systemctl start node_exporter

systemctl enable node_exporter

모니터링 당하는 서버에 메트릭을 가져와보자.

http://192.168.0.101:9100/metrics

결과가 쭉 나온다 node exporter는 동작한다. 이제 프로메테우스가 주기적으로 이걸 가져오면된다.

Prometheus 설정에 추가하자.

https://github.com/teamsmiley/devops-public/blob/main/monitoring/6.prometheus-oauth/prometheus/prometheus.yaml

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['192.168.0.101:9100']
      # - targets: ['192.168.0.101:9100','192.168.0.102:9100'] #여러대면 이렇게 추가하면된다.

Prometheus 타겟에 추가가 된건지 확인하자.

http://localhost:9090/targets

node01에서 재부팅해보자 서비스만 꺼도 됨.

ssh node01
systemctl stop node_exporter
systemctl status node_exporter
systemctl start node_exporter
systemctl status node_exporter

이제 http://localhost:9090/graph 에서 up을 확인해보자 내려갔다가 올라온것을 확인할수 있다.

![]({{ site.baseurl }}/assets/2020-01-11-09-53-36.png)

Prometheus 잘 동작하고 node exporter도 잘 동작하는것을 알수가 있다.

이제 docker-compose로 만들어서보자.

version: '3.7'
services:
  localhost:
    image: prom/prometheus
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    ports:
      - 9090:9090
    restart: always

volumes:
  prometheus_data: {}
docker-compose up -d

윈도우 node 모니터링

참고 https://medium.com/@facundofarias/setting-up-a-prometheus-exporter-on-windows-b3e45f1235a5

https://github.com/martinlindhe/wmi_exporter

https://grafana.com/grafana/dashboards/2129 theme

https://github.com/martinlindhe/wmi_exporter/releases 다운후 설치하면 서비스로 설치 완료

![]({{ site.baseurl }}/assets/2019-12-07-07-21-23.png)

msi download후 노드에 설치

설치 노드에서 다음 확인

http://localhost:9182/metrics

외부에서 다음 테스트

http://10.1.5.40:9182/metrics

Prometheus에 추가하여 붙이자. 9182 포트를 쓴다.

vi prometheus.yml

global:
  scrape_interval: 15s
scrape_configs:
---
- job_name: 'win_node_exporter'
  static_configs:
    - targets: ['10.1.5.40:9182', '10.1.5.57:9182']

이렇게 하면 윈도우도 모니터링이 된다.

알림

node export node01을꺼보자.

ssh node01 systemctl stop node_exporter

http://localhost:9090/targets 에서 체크해보면 꺼진것을 알수 있다.

그래프에서 up == 0 을 넣어서 꺼진것만 확인해보자.

1개만 나온다.

![]({{ site.baseurl }}/assets/2019-11-25-18-48-03.png)

이제 다 된듯

알림 규칙 추가

vi /data/git/docker/prometheus/prometheus.yml

global:
  ...
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.

rule_files:
  - "rules.yml"

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - alertmanager:9093

vi rules.yml

groups:
  - name: example
    rules:
      - alert:InstanceDown
        expr: up == 0
        for: 1m
docker run -d -p 9090:9090 -v /data/git/docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /data/git/docker/prometheus/rules.yml:/etc/prometheus/rules.yml --name prom prom/prometheus

http://localhost:9090/alerts

![]({{ site.baseurl }}/assets/2019-11-25-19-22-14.png)

이제 알림이 확보가 됬다.

알람 매니저 설치

바이너리와 도커로 올리는 방식 2가지가 있다. 도커를 사용한다.

ssh prometheus

https://github.com/prometheus/alertmanager/blob/master/doc/examples/simple.yml 참고

vi alertmanager.yml

global:
  smtp_smarthost: 'localhost:25'
  smtp_from: 'alertmanager@example.org'
route:
  receiver: team-mails
receivers:
  - name: 'team-mails'
    email_configs:
      - to: 'teamsmiley@gmail.com'
docker run -p 9090:9090 -v /data/git/docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /data/git/docker/prometheus/rules.yml:/etc/prometheus/rules.yml -v /data/git/docker/prometheus/alertmanager.yml:/etc/prometheus/alertmanager.yml prom/prometheus

./alertmanager를 실행하면 이메일을 받는다.

Last updated

Was this helpful?