当Prometheus持久化目录遇到权限被拒绝的情况时,应采取的对策是什么?

请您解释一下

使用Prometheus的Docker映像,在以下设置中部署Prometheus。

volumes:
  - /data/prometheus:/prometheus

部署完成后,检查日志时,

err="opening storage failed: lock DB directory: open /prometheus/lock: permission denied"

容器因为这个错误而停止运行。

There’s only one option for paraphrasing “原因” natively in Chinese:

缘故

Prometheus公式的Dockerfile

...
RUN mkdir -p /prometheus && \
    chown -R nobody:nogroup etc/prometheus /prometheus

USER       nobody
EXPOSE     9090
...

/data/prometheus的目录信息(例如)

$ ls -l /data
drwxrwxr-x  3 user1 users 4096  5月 21 00:00 prometheus/

查看Dockerfile时可以看到设置了USER nobody。
因此,写入目录时会使用nobody用户,但由于挂载的/data/prometheus目录的所有者不是nobody,所以导致错误发生。

顺便提一句,Prometheus容器的/prometheus目录的权限默认如下。

$ ls -l /
drwxr-xr-x    3 nobody   nogroup       4096 May 21 00:00 prometheus

解决方案

将执行用户更改为 root。

以root用户身份运行Prometheus。
由于使用root用户进行写入操作,因此不会引发错误,
但是为容器分配了超过所需的权限,因此不建议这样做。

在docker-compose.yml中添加user: root

version: "2"
services:
  prometheus:
    image: prom/prometheus:v2.9.2
    container_name: prometheus
    user: root #追記
    ports:
      - 9090:9090
    volumes:
      - /data/prometheus:/prometheus

2. 给其他用户添加读写权限

需要注意的是,每个人都能够熟练阅读和书写。

使用chmod命令修改权限

$ sudo chmod 777 /data/prometheus

3. 更改目录的所有者

使用chown命令更改所有者的操作

$ sudo chown nobody /data/prometheus

将目录的群组设置为管理员群组。

$ sudo chgrp monitor /data/prometheus

请参考

    What is the purpose of the nobody user
广告
将在 10 秒后关闭
bannerAds