Prometheus
Prometheus簡介
Prometheus 是一套開源的系統監控報警框架。它啟發於 Google 的 borgmon 監控系統,由工作在 SoundCloud 的 google 前員工在 2012 年創建,作為社區開源項目進行開發,並於 2015 年正式發布。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,成為受歡迎度僅次於 Kubernetes 的項目。
安裝Prometheus
Prometheus基於Golang編寫,編譯后的軟件包,不依賴於任何的第三方依賴。用戶只需要下載對應平台的二進制包,解壓並且添加基本的配置即可正常啟動Prometheus Server。
1.首先從下載頁面下載最新的Prometheus Server安裝包,然后解壓它:
wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz
tar xvfz prometheus-2.23.0.linux-amd64.tar.gz
mv prometheus-2.23.0.linux-amd64 /opt/prometheus
2.修改Prometheus服務器配置文件
cd /opt/prometheus
cat prometheus.yml
# 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']
,詳細請參考官網配置文件。
注:每次修改配置完成,用promtool檢測配置文件是否正確
[root@server1 prometheus]# ./promtool check config prometheus.yml
3.啟動prometheus服務器方法
第一種啟動方法:
[root@prometheus /opt/prometheus]# nohup ./prometheus --config.file=./prometheus.yml &
第二種啟動方法:
[root@prometheus /opt/prometheus]# ./prometheus &
第二種方法啟動前需要進行的操作如下:
啟動問題1:
level=error ts=2018-11-19T06:01:05.697957445Z caller=main.go:625
err="opening storage failed: lock DB directory: resource temporarily unavailable
解決:刪除 lock 文件
rm -f /opt/prometheus/data/lock
啟動問題2:
level=error ts=2018-11-19T06:04:47.83421089Z caller=main.go:625
err="error starting web server: listen tcp 0.0.0.0:9090: bind: address already in use"
解決:查找使用9090端口的PID並刪掉
yum install net-tools
netstat -apn | grep 9090
kill -9 <pid>
4.將Prometheus配置為系統服務
4.1 systemd目錄下創建文件:touch /usr/lib/systemd/system/prometheus.service
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Restart=on-failure
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus/prometheus.yml --storage.tsdb.retention.time=5d
[Install]
WantedBy=multi-user.target
注:storage.tsdb.retention.time是數據存儲時長,存儲時間默認是15d(天),單位:y, w, d, h, m, s, ms
4.2 啟動服務,設置開機自啟
systemctl enable prometheus
systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus
5.防火牆添加端口
firewall-cmd --add-port=9090/tcp --permanent ##永久添加 9090 端口
firewall-cmd --add-port=9100/tcp --permanent ##永久添加 9100 端口
systemctl restart firewalld ##重啟防火牆
firewall-cmd --list-ports ##列出開放的端口
systemctl status firewalld ##查看防火牆狀態
6.啟動后訪問prometheus服務器http://服務器的ip:9090,啟動成功,查看Status->Targets可以看到節點正常
7.重啟服務
ps aux | grep prometheus
可以用kill -HUP 進程id 自動加載新配置文件
8.繪圖
訪問http://服務器的ip:9090/metrics 查看從exporter具體能抓到的數據
9.被監控的客戶端安裝node_exporter(收集服務器數據)
官網有若干度量采集器,這里介紹監控Linux主機采集器。
監控客戶端從官網下載最新的node_exporter,然后解壓它:
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
tar zxvf node_exporter-1.0.1.linux-amd64.tar.gz
mv node_exporter-1.0.1.linux-amd64 /opt/node_exporter
9.1 啟動node_exporter
cd /opt/node_exporter/
nohup ./node_exporter &
9.2.將Prometheus配置為系統服務
9.2.1 systemd目錄下創建文件:touch /usr/lib/systemd/system/node_exporter.service
vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Restart=on-failure
ExecStart=/opt/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
9.2.2 啟動服務,設置開機自啟
systemctl enable node_exporter
systemctl start node_exporter
9.3 防火牆添加端口
firewall-cmd --add-port=9100/tcp --permanent ##永久添加 9100 端口
systemctl restart firewalld ##重啟防火牆
firewall-cmd --list-ports ##列出開放的端口
systemctl status firewalld ##查看防火牆狀態
9.4 修改服務器Prometheus配置文件
- job_name: 'node1'
static_configs:
- targets: ['客戶端IP:9100']
labels:
instance: 'nd1'
重啟服務器Prometheus服務
ps aux | grep prometheus
kill -HUP 7557
Grafana
Grafana(發音)簡介
Grafana是一個跨平台的開源的度量分析和可視化工具,可以通過將采集的數據查詢然后可視化的展示,並及時通知。
安裝Grafana
1.根據自己的系統版本,從官網選擇下載grafana安裝包
wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.3.4-1.x86_64.rpm
mv grafana-7.3.4-1.x86_64.rpm /opt/
2.Grafana離線包下載
wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.3.4-1.x86_64.rpm
rpm -ivh grafana-7.3.4-1.x86_64.rpm //查看rpm依賴包
依賴rpm包下載網址: https://www.rpmfind.net/
下載依賴包urw-fonts,和grafana-6.6.1-1.x86_64.rpm放入一個文件夾,提供離線安裝使用
yum install --downloadonly --downloaddir=/root/ urw-fonts //聯網環境下載依賴包urw-fonts
注:只下載包,不安裝包
yum install --downloadonly --downloaddir=[download_dir] [package]
上傳grafana相關所有離線包到離線環境
離線安裝
切換到grafana離線包所在目錄
yum clean all ; yum localinstall –y --skip-broken ./*
grafana-server -v //查看版本
3.安裝並啟動Grafana服務
cd /opt/
yum install grafana-7.3.4-1.x86_64.rpm
systemctl enable grafana-server
systemctl start grafana-server
systemctl status grafana-server
4.防火牆添加端口
firewall-cmd --add-port=3000/tcp --permanent ##永久添加 3000 端口
systemctl restart firewalld ##重啟防火牆
firewall-cmd --list-ports ##列出開放的端口
systemctl status firewalld ##查看防火牆狀態
web訪問Grafana服務
[http://服務器的IP:3000/],用戶名:初始密碼 admin/admin
讓Grafana從Prometheus中拉取數據
1.添加一個data source ,基礎配置如下:
2.導入對node監控的DashBoard
首先去官網下載對主機監控的Dashboard, 搜素 Node Exporter:
再點開第一個 ,拷貝對應的Dashboard Id 8919:
離線導入的話,可以選擇上面的”Download JSON”,再在Import中的Upload JSON file選擇下載的文件或者把json全部內容粘貼到Import via panel json中點擊Load。
然后回到我們的圖形監控平台Grafana,Dashboard ---> import:
輸入 dashboard ID 8919,選擇數據源為prometheus即可,然后點擊導入:
最后即可查看數據
監控Jmeter測試結果
請參考如何用 JMeter + Kubernetes + Prometheus + Grafana + Alert Manager 實時監控你的服務器、JMeter, Prometheus, and Grafana Integration。









