1

context : 我在一个空的 centos VM 中添加了一些脚本来安装一些监控工具,包括 prometheus 2.0。

问题:安装在非root sudo 用户的主目录后,我将写入的prometheus.service 复制到“/etc/systemd/system”,运行sudo systemctl daemon-reload, sudo systemctl enable prometheus.service, sudo systemctl start prometheus.service但服务失败。

注意:我可以使用相同的命令直接在终端中运行 prometheus 二进制文件,没有任何问题,但我不能将它作为服务运行。

这是我的 .service 文件:

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target

[Service]
User=centos
ExecStart=/home/centos/prometheus/prometheus --config.file="/home/centos/prometheus/prometheus.yml" --storage.tsdb.path="/home/centos/prometheus/data"

[Install]
WantedBy=multi-user.target

这是一些日志:

...
Nov 21 12:41:55 localhost.localdomain prometheus[1554]: level=info ts=2017-11-21T17:41:55.114757834Z caller=main.go:314 msg="Starting TSDB"
Nov 21 12:41:55 localhost.localdomain prometheus[1554]: level=error ts=2017-11-21T17:41:55.114819195Z caller=main.go:323 msg="Opening storage failed" err="mkdir \": permission denied"
Nov 21 12:41:55 localhost.localdomain systemd[1]: prometheus.service: control process exited, code=exited status=1
Nov 21 12:41:55 localhost.localdomain systemd[1]: Failed to start Prometheus Server.
...

我是 linux 服务管理的新手,我花了很多时间在线阅读,但我不确定服务的权限是如何工作的,以及为什么它无法创建它需要创建的目录。

我试过了:

  • 将“SELINUX=enforcing”更改为“SELINUX=permissive”

  • 修改prometheus目录的权限为777

  • ...

4

3 回答 3

2

您还必须设置--web.console.templates--web.console.libraries。您可以从提取的存档中复制这些目录。例如:

sudo cp -R ~/prometheus-2.0.0.linux-amd64/consoles /etc/prometheus
sudo cp -R ~/prometheus-2.0.0.linux-amd64/console_libraries /etc/prometheus

工作服务示例(为您更改路径):

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml \
    --storage.tsdb.path=/var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target

PS 灵感来自这里的建议。

于 2017-12-02T07:21:27.517 回答
0

如果 SELinux 停止启动,请始终咨询 journalctl -xe 以查看 SELinux 警报。有建议采取的行动。

我在 CentOS 8 上使用 SELinux 设置了 prometheus,没有问题。我不同意那些建议禁用 SELinux 的人。

作为参考 Redhat 有一个很好的视频供您观看:

https://www.youtube.com/watch?v=_WOKRaM-HI4&t=1464s

这是我的 prometheus.service 文件。

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target

[Service]
User=prometheus
#Restart=on-failure

#Change this line if you download the
#Prometheus on different path user
ExecStart=/home/prometheus/prometheus-2.22.0.linux-amd64/prometheus \
  --config.file=/home/prometheus/prometheus-2.22.0.linux-amd64/prometheus.yml \
  --storage.tsdb.path=/home/prometheus/prometheus-2.22.0.linux-amd64/data \
  --web.listen-address="0.0.0.0:9091"

[Install]
WantedBy=multi-user.target
于 2020-11-22T16:02:31.803 回答
0

Prometheus 的数据目录应具有 prometheus 应用程序用户的写入权限。如果您从容器运行它并外部挂载数据目录,则可以在原始文件夹上设置 777 权限。

于 2019-05-03T15:52:54.350 回答