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?