K8s中使用自定义prometheus.yml部署Prometheus找不到配置文件如何解决
问题根因分析
你遇到的启动报错:
level=error ts=2021-10-14T15:26:02.525Z caller=main.go:347 msg="Error loading config (--config.file=/etc/config/prometheus.yml)" err="open /etc/config/prometheus.yml: no such file or directory"
通常是以下两个原因导致:
- 构建自定义镜像时,上下文目录下的
config文件夹内不存在prometheus.yml文件,或者文件权限不符合Prometheus默认运行用户(UID 65534)的读取要求 - 社区Prometheus Helm Chart默认会自动生成配置ConfigMap并挂载到容器内,同时设置默认启动参数,你没有覆盖默认的挂载和参数配置时,你镜像内的
/etc/config路径可能被空目录覆盖,或者启动参数和你自定义的配置路径不匹配
推荐解决方案(无需自定义镜像,适配独立文件管理配置的需求)
该方案更新配置无需重新构建镜像,灵活度更高:
- 本地存放好你的独立
prometheus.yml配置文件,执行命令创建专属ConfigMap:kubectl create configmap prometheus-custom-config --from-file=prometheus.yml=./本地配置路径/prometheus.yml -n 部署命名空间 - 编写自定义
values.yaml,覆盖Chart默认配置逻辑:
server: # 挂载自定义ConfigMap extraVolumes: - name: custom-config configMap: name: prometheus-custom-config extraVolumeMounts: - name: custom-config mountPath: /etc/custom-config readOnly: true # 自定义启动参数,指定配置文件路径 args: - --config.file=/etc/custom-config/prometheus.yml - --storage.tsdb.path=/prometheus - --web.console.libraries=/etc/prometheus/console_libraries - --web.console.templates=/etc/prometheus/consoles # 按需添加其他启动参数 # 关闭Chart默认的配置生成逻辑 configmap: enabled: false
- 执行
helm install或者helm upgrade命令加载上述values配置部署即可。
坚持使用自定义镜像的调整方案
如果你必须使用内置配置的自定义镜像,按如下要求调整:
- 优化Dockerfile,显式配置文件权限,避免权限异常:
FROM quay.io/prometheus/prometheus:v2.26.0 ADD config /etc/config/ USER root RUN chown -R 65534:65534 /etc/config USER 65534
- 调整values.yaml配置,关闭默认配置逻辑,指定启动参数:
server: # 替换为你自己的镜像信息 image: repository: 自定义镜像仓库地址 tag: 自定义镜像版本 # 指定启动参数 args: - --config.file=/etc/config/prometheus.yml - --storage.tsdb.path=/prometheus - --web.console.libraries=/etc/prometheus/console_libraries - --web.console.templates=/etc/prometheus/consoles # 关闭默认配置生成 configmap: enabled: false
内容的提问来源于stack exchange,提问作者manithams
相关产品推荐
相关产品推荐

