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