1. 監控軟件選型
市面上有不少監控軟件,關于prometheus與其它監控軟件的區別網上有很多資料,這里不再贅述,只簡單說一下當時為什么選擇了prometheus:
- prometheus是開源軟件,免費使用。
- prometheus社區生態比較好,在國內應用廣泛,網上有很多資料。
- prometheus擁有眾多exporter,基本覆蓋公司內使用的數據庫與中間件產品。
- 公司有把業務系統遷移到k8s上的打算,prometheus天然適配k8s。
2. prometheus主機資源選擇
prometheus需要的主機規格與較多因素有關,如采集樣本數,采集頻率、數據保留時間等,要根據實際需求進行評估。磁盤建議選擇高IO的SSD盤,我們初期選用了IO較低的機械盤,結果導致監控數據查詢很慢,grafana上的圖要很久才能刷新出來。
3. prometheus安裝包下載
- 官網://prometheus.io/download/
- github://github.com/prometheus/prometheus
4. 部署prometheus
-
解壓安裝包:
tar -xvf prometheus-2.xx.x.linux-amd64.tar.gz mv prometheus-2.xx.x.linux-amd64 /app/prometheus -
prometheus支持的啟動參數可以通過
./prometheus --help獲取,我們主要關注以下幾個啟動參數:# 指定prometheus配置文件: --config.file # tsdb數據存儲路徑: --storage.tsdb.path # tsdb數據存儲時間: --storage.tsdb.retention.time # tsdb數據存儲大小: --storage.tsdb.retention.size # prometheus的web地址 --web.listen-address # 管理控制操作啟用API端點 --web.enable-admin-api # 啟用是否通過HTTP請求重新加載 --web.enable-lifecycle # 日志等級 --log.level=info -
promtool提供檢查配置文件的功能
./promtool check config prometheus.yml -
另外prometheus還提供了reload接口,使我們在更改配置文件后不必重啟prometheus就可將更改內容應用。
curl -X POST //127.0.0.1:9090/-/reload -
基于以上知識點,我們創建以下prometheus.service文件
vi /usr/lib/systemd/system/prometheus.service [Unit] Description=Prometheus After=network.target [Service] Type=simple User=root ExecStartPre=/app/prometheus/promtool check config /app/prometheus/prometheus.yml ExecStart=/app/prometheus/prometheus \ --config.file=/app/prometheus/prometheus.yml \ --storage.tsdb.path=/app/prometheus/data \ --storage.tsdb.retention.time=30d \ --web.listen-address=0.0.0.0:9090 \ --web.enable-admin-api \ --web.enable-lifecycle \ --log.level=info ExecReload=/bin/curl -X POST //127.0.0.1:9090/-/reload TimeoutStopSec=20s Restart=on-failure [Install] WantedBy=multi-user.target -
如果公司禁止使用root用戶啟動應用,那這里要把User改成其它用戶,如prometheus普通用戶,并且要將相關目錄授權給prometheus普通用戶。
chown -R prometheus:prometheus /app/prometheus/ -
啟動prometheus服務
systemctl daemon-reload systemctl enable --now prometheus.service systemctl status prometheus.service -
如果prometheus服務啟動失敗,可以使用以下命令檢查失敗原因:
journalctl -u prometheus.service -
prometheus服務正常啟動后,我們就可以通過瀏覽器訪問prometheus了。
//IP:9090/