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