1. 程式人生 > 實用技巧 >Prometheus+Grafana監控宿主機

Prometheus+Grafana監控宿主機

Prometheus是非常優秀的監控工具,準確的說是一套完整的監控方案。提供了資料收集,儲存,處理,加工展示,告警等一系列完整解決方案

關鍵元件

Prometheus關鍵元件包括:Prometheus Server,Exporter,視覺化元件,Alertmanager四大模組

  • Prometheus Server

​ Prometheus Server負責從Exporter拉取及儲存資料,並且提供了一套查詢語句(PromQL)供使用者使用

  • Exporter

Exporter負責收集目標系統的各類效能資料,目標系統可以使host也可以是container容器。並且通過HTTP介面提供給Prometheus Server獲取

  • Alertmanager

Alertmanager提供了基於監控資料的告警規則,一旦Alertmanager收到告警就會通過預先定義的方式發出告警通知

  • 視覺化元件

資料的視覺化是監控工具至關重要的,Prometheus有自己開發的展示方案,後來被更加優秀的開源產品Grafana替代,Grafana能與Prometheus完美結合提供完美的資料展示能力

實戰

1.環境搭建

例項通過prometheus監控兩臺宿主機,監控host單個層面,也選擇適當的DashBoard監控container環境。

本實驗將以容器方式執行以下的元件:

(1)Prometheus Server會以容器方式執行在192.168.0.102上

(2)Exporter

  • Node Exporter

    負責收集宿主機的硬體以及作業系統的資料,以容器方式執行在宿主機上

  • cAdvisor

    負責收集容器資料,以容器方式執行在宿主機上

(3)Grafana:用於展示監控資料

ip Node Exporter cAdvisor Prometheus Server Grafana
192.168.0.101
192.168.0.102

2.執行Node Exporter

在兩個host上執行如下的命令:

docker run -d --name node-exporter  -p 9100:9100 \
-v "/proc:/host/proc:ro"   -v "/sys:/host/sys:ro"   -v "/:/rootfs:ro" \
--restart=always   --net="host"  \
prom/node-exporter \ 
--path.procfs /host/proc --path.sysfs /host/sys \
--collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)" 

使用--net="host"目的是為了Node Exporter和Prometheus Server直接通訊

執行完上述命令後就可以訪問任意ip的:9100來獲取收集到的資料了。在瀏覽器上輸入http://192.168.0.101:9100/metrics測試

3.執行cAdvisor

在兩個host上執行如下命令啟動cAdvisor容器

 docker run -v /:/rootfs:ro -v /var/run:/var/run:rw \
 -v /sys:/sys:ro -v /var/lib/docker:/var/lib/docker:ro \
 -p8080:8080 -t --name cadvisor --net="host" \
 google/cadvisor:latest

同樣使用--net="host"使cAdvisor與Prometheus Server直接通訊

4.執行Prometheus Server

在192.168.0.102主機上執行如下命令啟動Prometheus Server

docker run -d -p 9090:9090 \ 
-v /root/prometheus.yml:/etc/prometheus/prometheus.yml \
--name prometheus --net host \
prom/prometheus

其中prometheus.yml是Prometheus Server的配置檔案,如下配置所示,最重要的配置就是最下面的static_config段,它指定了從哪些exporter抓取資料

# 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).
 
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - alertmanager:9093
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
 
# 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'.
    scrape_interval: 5s
    static_configs:
    - targets: ['localhost:9090','localhost:8080','localhost:9100','192.168.0.101:9100','192.168.0.101:8080']

開啟http://192.168.0.102:9090,點選選單欄的Status切換到Targets,輸出如下:

所有State均為UP,表示Prometheus Server正常獲取到所有監控資料

5.安裝Grafana

在192.168.0.102上執行如下命令執行Grafana

docker run -id -p 3000:3000 \ 
-e "GF_SERVER_ROOT_URL=http://grafana.server.com" \
-e "GF_SECURITY_ADMIN_PASSWORD=admin@123" \
--net="host" grafana/grafana

這裡使用-e"GF_SECURITY_ADMIN_PASSWORD=admin@123"指定Grafana admin使用者密碼為admin@123

訪問http://192.168.0.102:3000進入Grafana介面,Grafana將會引導我們配置Data Source

配置完Data Source後需要通過Dashboard展示資料,配置展示資料很困難,可以使用開源社群的力量

使用現成的Dashboard,下載現成的Dashboard的json檔案匯入到Grafana就可以展示監控資料了,這裡我選擇了只監控host的Dashboard