参考地址:
https://www.prometheus.wang/quickstart/install-prometheus-server.html
https://spex.top/archives/systemd-prometheus-node-exporter.html
Prometheus
#创建容器之前先创建prometheus.yml文件,可百度查找模板或去官网下载prometheus的安装包里查找
docker run --name prom -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus
容器启动以后在浏览器打开ip:9090即可访问
node_exporter
#下载
curl -OL https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
#解压
tar -xzf node_exporter-0.17.0.linux-amd64.tar.gz
cd node_exporter-0.17.0.linux-amd64/
#更改路径
mv node_exporter /usr/local/bin/
#后台运行
/usr/local/bin/node_exporter &
#node_exporter启动的默认端口是9100,启动以后在浏览器打开ip:9100 即可查看监控数据
#查看node_exporter更多参数,可以用./node_exporter -h命令
使用systemctl来管理node_exporter
#创建管理文件
vi /etc/systemd/system/node_exporter.service
#内容
[Unit]
Description=Prometheus node_exporter
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9111
[Install]
WantedBy=multi-user.target
启动服务
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
为node_exporter添加nginx访问验证
如果没有htpasswd这个命令可以使用yum install httpd-tools
来安装
$ htpasswd -c .htpasswd admin
New password:
Re-type new password:
Adding password for user admin
#admin为账号
添加反向代理配置,这里我用的9110作为反向代理的端口
server {
listen 0.0.0.0:9110;
location / {
auth_basic "Prometheus";
auth_basic_user_file "/usr/local/nginx/conf/servers/.htpasswd";#注意htpasswd的路径
proxy_pass http://localhost:9111/;
}
}
然后重载nginx,配置到这一步可以打开浏览器ip:9110
访问就会提示需要输入账号密码
prometheus.yml添加node_exporter的代理验证信息
global:
scrape_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ["localhost:9090"]
- job_name: local
static_configs:
- targets: ["192.168.110.6:9110"]
basic_auth: #这里注意缩进行,不然prometheus可能无法启动
username: admin #反向代理配置的账号
password: admin #反向代理配置的密码