1. 安裝Prometheus Server
cd /usr/local/src
tar zvxf prometheus-2.21.0.linux-amd64.tar.gz -C /opt/
ln -s /opt/prometheus-2.21.0.linux-amd64/ /opt/prometheus
vim /opt/prometheus/prometheus.yml
useradd -s /sbin/nologin -M prometheus
mkdir /data/prometheus -p
chown -R prometheus:prometheus /opt/prometheus/
chown -R prometheus:prometheus /data/prometheus/
vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus
打開 http://localhost:9090/graph (要找開對應端口的防火牆)
Status-Targets 查看 Endpoint 對應的 State 是 UP ,即代表成功
2. Grafana的安裝
cd /usr/local/src
tar zvxf grafana-7.2.1.linux-amd64.tar.gz -C /opt
ln -s /opt/grafana-7.2.1/ /opt/grafana
useradd -s /sbin/nologin -M grafana
mkdir /data/grafana
chown -R grafana:grafana /opt/grafana
chown -R grafana:grafana /data/grafana
vim /opt/grafana/conf/defaults.ini
data = /data/grafana/data
logs = /data/grafana/log
plugins = /data/grafana/plugins
provisioning = /data/grafana/conf/provisioning
保存,退出
vim /etc/systemd/system/grafana-server.service
[Unit]
Description=Grafana
After=network.target
[Service]
User=grafana
Group=grafana
Type=notify
ExecStart=/opt/grafana/bin/grafana-server -homepath /opt/grafana
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl enable grafana-server
systemctl start grafana-server
systemctl status grafana-server
打開 http://localhost:3000/
grafana 添加 prometheus 數據源
Configure - DataSources - Add data source 選擇 Prometheus
HTTP URL 填寫 http://IP:9090 (Prometheus對應的IP和端口)
點擊 Save & Test 提交
Dashboards 選擇 Prometheus 2.0 stats - Import
Search - General - Prometheus 2.0 Stats 打開圖表查看
3. 使用Node Exporter采集主機運行數據
cd /usr/local/src
tar zvxf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt
ln -s /opt/node_exporter-1.0.1.linux-amd64/ /opt/node_exporter
直接打開node_exporter的可執行文件即可啟動 node export,默認會啟動9100端口。
建議使用nohup來啟動
vim /etc/rc.local
nohup /opt/node_exporter/node_exporter >/dev/null 2>&1 &
編輯prometheus.yml文件,增加后面4行.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
#采集node exporter監控數據
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']
然后重啟prometheus,打開prometheus頁面查看是不是有對應的數據了
導入grafana模板,數據展示(grafana模板官網 https://grafana.com/dashboards)
打開 http://localhost:3000/
Create - Import - Import via grafana.com
輸入9276號模板 - Load - 選擇 Prometheus數據源 - 點擊Import提交
Search - General - 1主機基礎監控(CPU,內存,磁盤,網絡)
分組 選擇:Node IP地址選擇:localhost:9100
即可以顯示數據
// 刪除某個組下某個實例的所有數據
curl -X DELETE http://192.168.0.10:9091/metrics/job/pushgateway-microservice/instance/pushgateway-microservice
// 刪除某個組下的所有數據
curl -X DELETE http://192.168.0.10:9091/metrics/job/pushgateway-microservice
刪除全部數據
curl -X POST -g 'http://192.168.0.10:9090//api/v1/admin/tsdb/delete_series?match[]={__name__=~".+"}'
啟動
./prometheus --storage.tsdb.retention=180d --web.enable-admin-api
//清理db
curl -XPOST http://192.168.0.10:9090/api/v1/admin/tsdb/clean_tombstones