簡介
運維監控系統,不用多介紹,網上有很多介紹的文章。
為什么選擇Prometheus呢?
- 部署簡單,Go寫的程序基本上解壓就可以使用,不用太多的依賴安裝。
- 服務自動化發現,可以使用console,也可以使用動態文件的方式(這個是我想用的)。
- 高性能,單點就有很高的性能,目前很多監控系統都支持分布式,單點性能不好,靠分布式來緩解壓力。
- 強大的查詢語句,有很多實用的函數,可以提供給我們查詢分析。
- 結合grafana可以快速地實現圖表。
部署測試
在Prometheus的官網,可以快速下載到安裝包,就是一個 壓縮包而已。有支持了很多平台,我們還是習慣在Linux上進行部署。
tar zxvf prometheus-2.20.0.linux-amd64.tar.gz
prometheus的配置文件是yaml格式的,默認配置文件:
# 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'. static_configs: - targets: ['localhost:9090']
- global:prometheus的全局配置, 比如采集間隔,抓取超時時間等。
- alerting:配置altermanager,prometheus將報警規則推送到指定的alertmanager實例地址
- rule_files:報警規則文件, prometheus根據規則信息,會推送報警信息到alertmanager中。
- scrape_configs:抓取配置,prometheus通過這里的配置采集數據。
默認的監聽端口是9090,這里默認配置就有抓取prometheus本身指標的配置。
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'. static_configs: - targets: ['localhost:9090']
配置啟動
prometheus沒有Daemon形式執行的,可以使用nohup在后台運行。也可以利用systemd來管理,直接編寫一個service文件就可以了。
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/prometheus/prometheus
ExecStart=/opt/prometheus/prometheus --config.file="/opt/prometheus/prometheus.yml"
LimitNOFILE=65536
PrivateTmp=true
RestartSec=2
StartLimitInterval=0
Restart=always
[Install]
WantedBy=multi-user.target
systemd時候記得一定要加上WorkingDirectory的工作目錄,這個決定你的數據存放到什么地方。
systemd管理:
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl stop prometheus
systemctl restart prometheus
部署node_exporter
node_exporter是用於獲取主機監控指標的程序,也是Go寫的,部署一樣方便,二進制直接執行就可以了。
tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz
同樣也可以使用systemd執行服務管理
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/node_expaorter/node_expaorter
ExecStart=/opt/node_expaorter/node_expaorter
LimitNOFILE=65536
PrivateTmp=true
RestartSec=2
StartLimitInterval=0
Restart=always
[Install]
WantedBy=multi-user.target
systemd管理:
systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
systemctl stop node_exporter
systemctl restart node_exporter
增加監控
編輯prometheus的配置文件prometheus.yaml,添加上job。
復制上面的prometheus的job_name,注意yaml的格式,需要相同的縮進,如果只有幾台固定的機器,直接使用靜態配置就可以了。
- job_name: 'node_exporter'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'. static_configs: - targets: ['192.168.122.100:9100','192.168.122.101:9100','192.168.122.101:9100']
如果有很多台主機需要監控,而且有可能隨時需要增加或者刪除,這時候最簡單的方式就是使用監聽目錄的文件變化來加載對應的配置。配置也很簡單
- job_name: 'node_exporter' refresh_interval: 1m file_sd_configs: - files: - /opt/prometheus/servers/*.json
這樣子就設置根據文件的變化來修改對應的配置,比較靈活。
refresh_interval 指定文件檢測的間隔,默認是5分鍾。
只要是配置目錄下后綴名是.json的文件都會被自動加載,當然文件格式是有要求的。
[ { "targets": [ "192.168.122.100:9100" ], "labels": { "instance": "192.168.122.100", "job": "node_exporter" } }, { "targets": [ "192.168.122.101:9100" ], "labels": { "instance": "192.168.122.101", "job": "node_exporter" } } ]
這里可以對標簽進行修改或者增加。也可以分成多個文件。格式都要一樣的。
重載配置
如果是靜態配置需要重載Prometheus的進程,通過kill -HUP [pid] 的方式就可以重載了。
也可以通過啟用管理api進行重載,這個需要在啟動的時候加--web.enable-admin-api的參數
然后通過訪問:
curl http://localhost:9090/-/reload
如果是動態文件的方式就不用管了,配置好文件,到時間間隔檢測的文件變化就會自動加載了。
總結
不是很簡單,一會就可以完成部署,但是還有很多優化的空間,最好是安全的問題:
- systemd服務默認是使用root用戶啟動服務的,我們可以通過設置User=username,來使用普通用戶執行,如果之前使用root用戶開啟過了,就需要修改一下數據目錄data的權限。
- 進行不要開啟admin-api,雖然很方便,但是也存在風險,使用動態文件配置就可以輕松解決重載的問題了。
- 默認服務都是通過http訪問的沒有密碼驗證、ssl加密等安全措施,也是一個風險點。解決!prometheus的端口可以使用nginx進行代理,增加https證書即可;針對node_exporter在1.0版本之后也可以使用https+basic_auth的方式增加安全性。