服务器监控和报警软件有很多,为什么我们会选择Prometheus而不是其他软件呢?
因为它有以下优点:
下面我们就开始安装吧。
首先是下载,这里我们使用sudo wget直接从github下载
sudo wget https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz
然后解压
sudo tar -xvf prometheus-2.48.1.linux-amd64.tar.gz
移动到软件目录
sudo mv prometheus-2.48.1.linux-amd64 /usr/local/bin/prometheus
如果我们需要修改配置,进入到目录并修改prometheus.yml即可
cd /usr/local/bin
vi prometheus.yml
?创建运行prometheus的账户并给予权限
sudo useradd -M -r -s /bin/false prometheus
参数含义
目录赋权
sudo chown -R prometheus:prometheus /usr/local/bin/prometheus
创建systemd单元文件
sudo vi /etc/systemd/system/prometheus.service
写入如下内容
[Unit]
Description=Prometheus
After=network.target
[Service]
ExecStart=/usr/local/bin/prometheus/prometheus --config.file=/usr/local/bin/prometheus/prometheus.yml
Restart=always
User=prometheus
Environment="PATH=/usr/local/bin/prometheus:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/usr/local/bin/prometheus"
[Install]
WantedBy=default.target
保持配置之后重载systemd配置
sudo systemctl daemon-reload
创建服务所需的data目录
sudo mkdir -p /data
sudo chown -R prometheus:prometheus /data
?启动服务和设置开机启动
sudo systemctl start prometheus
sudo systemctl enable prometheus
?查看服务状态
sudo systemctl status prometheus
添加防火墙策略
sudo ufw allow 9090
根据配置访问对应的IP和页面
如果服务未启动成功,我们需要配置服务,并创建对应的日志文件来排查问题。
创建日志文件夹及文件,并赋权
sudo mkdir -p /var/log/prometheus
sudo touch /var/log/prometheus/prometheus.log
sudo touch /var/log/prometheus/prometheus_error.log
sudo chown -R prometheus:prometheus /var/log/prometheus
sudo chmod -R 755 /var/log/prometheus
在配置文件的[Service]部分添加日志输出
[Service]
ExecStart=/usr/local/bin/prometheus/prometheus --config.file=/usr/local/bin/prometheus/prometheus.yml
Restart=always
User=prometheus
Environment="PATH=/usr/local/bin/prometheus:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/usr/local/bin/prometheus"
StandardOutput=file:/var/log/prometheus/prometheus.log
StandardError=file:/var/log/prometheus/prometheus_error.log
重载systemd
sudo systemctl daemon-reload
查看错误日志
cy@cy:/var/log/prometheus$ cat /var/log/prometheus/prometheus_error.log
解决对应问题即可。