從零開始:使用Prometheus與Grafana搭建監(jiān)控系統(tǒng)
Prometheus是一款開源的監(jiān)控系統(tǒng),主要用于收集、存儲和查詢時間序列數據,以便于對系統(tǒng)進行監(jiān)控和分析。
Prometheus的架構由四個主要組件組成:
- Prometheus Server :Prometheus Server是Prometheus的核心組件,主要負責從各個目標(target)中收集指標(metrics)數據,并對這些數據進行存儲、聚合和查詢。
- Client Libraries:Prometheus提供了多種客戶端庫,用于在應用程序中嵌入Prometheus的指標收集功能。
- Exporters:Exporters是用于將第三方系統(tǒng)的監(jiān)控數據導出為Prometheus格式的組件。Prometheus支持多種Exporters,例如Node Exporter、MySQL Exporter、HAProxy Exporter等。
- Alertmanager:Alertmanager是Prometheus的告警組件,用于根據用戶定義的規(guī)則對監(jiān)控數據進行告警。
同時Prometheus有以下優(yōu)點:
- 靈活的數據模型:Prometheus采用的是key-value對的形式存儲指標數據,每個指標都可以包含多個標簽(labels),這樣可以更加靈活地描述指標數據
- 高效的存儲和查詢:Prometheus使用自己的時間序列數據庫,可以高效地存儲和查詢大量的指標數據。
- 強大的可視化和告警功能:Prometheus提供了Web界面和API,可以方便地展示和查詢監(jiān)控數據。
- 可擴展性強:Prometheus的架構非常靈活,可以根據需要選擇合適的組件進行配置。CNCF的成員項目:Prometheus作為CNCF的項目之一,得到了廣泛的關注和支持,并且得到了來自全球各地的貢獻者的積極參與和開發(fā)。
Prometheus介紹
下面就Prometheus基于本地環(huán)境進行監(jiān)控報警進行講解。
1.下載
docker pull prom/prometheus:v2.43.0
2.配置
創(chuàng)建文件夾data
創(chuàng)建配置文件prometheus.yml,可以根據需要進行調整:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
因為路徑過長,創(chuàng)建軟鏈目錄/data/prometheus:
ln -s /Users/weizhao.dong/Documents/soft/prometheus /data/prometheus
3.啟動
docker run --name prometheus -d -p 9090:9090 -v /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /data/prometheus:/prometheus prom/prometheus:v2.43.0
Grafana安裝
1.下載
docker pull grafana/grafana-enterprise:8.5.22
2.啟動
docker run -d --name=grafana -p 3000:3000 grafana/grafana-enterprise:8.5.22
3.配置數據源
添加prometheus數據源:
Linux服務器資源監(jiān)控
1.下載node-exporter
選擇指定版本,并下載:
wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-arm64.tar.gz
下載解壓執(zhí)行node_exporter文件暴漏9100端口,即可采集到監(jiān)控信息
2.安裝node-exporter
由于直接啟動node-exporter關閉窗口此進程就會掛掉,不能滿足需求,因此可以采用systemctl方式進行配置。
(1) 在/usr/lib/systemd/system/目錄,創(chuàng)建node_exporter.service文件,內容如下,ExecStart指向的就是node_exporter執(zhí)行文件:;
[Unit]
Description=Node Exporter
[Service]
ExecStart=/usr/local/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
(2) 執(zhí)行systemctl daemon-reload
(3) 執(zhí)行systemctl start node_exporter啟動node_exporter
(4) 執(zhí)行netstat -aon|grep 9100查看9100是否啟動成功
3.修改prometheus配置文件
增加以下任務,5s采集一次,這種方式屬于Promethues的Pull 模式,即主動發(fā)起請求拉取目標數據:
- job_name: 'linux'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['10.211.55.4:9100']
4.重啟prometheus
docker restart prometheus
5.Grafana文件配置
訪問https://grafana.com/grafana/dashboards/,下載node_export配置文件:
點擊右邊的DownloadJson文件進行下載:
將下載文件導入到Grafana:
導入完成以后,可以看到相關數據已采集。
總結
在本文中,我們介紹了什么是Prometheus,如何安裝Prometheus,以及使用Prometheus的Pull(拉取)模式來采集Linux服務器資源,并在Grafana進行展現(xiàn)。