Prometheus重启失败的教训
重启⽅式
直接后台运⾏
./prometheus &
或者
nohup ./prometheus --config.file=./l --ion.time=90d --web.listen-address=:9090 &
以服务⽅式启动
添加 prometheus.service ⽂件到 /etc/systemd/system/ ⽬录下,我的prometheus服务安装在 /data/prometheus/ ⽬录。
Description=Prometheus service
After=network.target
Wants=network.target
ExecStart=/data/prometheus/prometheus --config.file=/data/l --ion.time=90d --web.listen-address=:9090
Restart=always
RestartSec=20
TimeoutSec=300
User=root
Group=root
StandardOutput=journal
StandardError=journal
WorkingDirectory=/data/prometheus/
WantedBy=multi-user.target
设置开机⾃启,启动服务
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
Prometheus重启失败的案例及总结
以下内容是总结prometheus启动失败的案例。
在不同的案例中,由于⽬标机器不同,prometheus的相关配置(如:安装路径,启动参数等)会有所不同。
配置⽂件:重复的job_name
现象描述
根⽬录/root/磁盘满了,⽆论使⽤nohup命令还是以服务⽅式启动prometheus均失败。
排查原因
查⼤⽂件发现⽬录/root/data/下有prometheus存储的数据⽂件,查看/etc/systemd/system/prometheus.service⽂件,发现配置项WorkingDirectory=/root/,即将⼯作⽬录配置到了根⽬录/root/下。由于prometheus默认存储路径为data/,所以在**/root/data/**⽬录下存储了prometheus的⼤量数据⽂件。清理磁盘空间后发现仍然⽆法启动,进⼀步排查发现l配置⽂件中有重复定义的配置项。
解决⽅式
修改/etc/systemd/system/prometheus.service⽂件中的配置项为WorkingDirectory=/data/prometheus/(我的机器上的/data/⽬录磁盘空间较⼤,也可以指定其他较⼤的磁盘⽬录);
删除/root/data/⽬录,释放磁盘空间;
以服务⽅式重启prometheus,systemctl daemon-reload; systemctl start prometheus。
然⽽,经过上述处理后发现prometheus仍然⽆法重启成功,从service prometheus status打印的信息定位不到错误。使⽤nohup启动并将⽇志输出到nohup.out⽂件中,查看信息显⽰l⽂件中的scrape_configs字段下有重复定义的job_name(⾃⼰挖的坑
使⽤⾃动部署服务的脚本追加了相同字段到配置⽂件中)。
level=error ts=***********  :290 msg="Error loading config (--config.file=./l)"err="parsing YAML file ./l: found mu ltiple scrape configs with job name \"***_node\""
确保l配置⽂件中信息唯⼀后,以服务⽅式启动prometheus。
配置⽂件:job_name下多个static_configs
现象描述
修改prometheus配置⽂件后发现启动prometheus失败,报错信息如图
排查原因
执⾏./prometheus --config.file=/usr/local/l,报错信息如下
level=error ts=2021-07-30T02:31:48.084Z :355 msg="Error loading config (--config.file=/usr/local/l)"err="parsin g YAML file /usr/local/l: yaml: unmarshal errors:\n  line 43: field static_configs already set in type config.ScrapeConfig\n  line 50 : field static_configs already set in type config.ScrapeConfig"
字段下定义了多个static_configs,配置⽂件如下图解决⽅式
删除配置⽂件中job_name字段下⾯多余的static_configs,只保留顶层的⼀个。然后,重启prometheus。
另外,static_configs是默认的静态配置⽅式,每次修改该字段下的内容后,需要重启prometheus才能使配置⽣效。如果不想每次都重启prometheus,可以采⽤prometheus提供的服务发现⽅式,如:file_sd_configs,只要动态地修改指定的配置⽂件,prometheus会⾃动加载配置。
更多请参见:
告警规则⽂件:tab键缩进
现象描述
在prometheus配置⽂件中开启告警组件alertmanager,并定义告警规则⽂件,然后发现重启prometheus失败,报错信息如下:
排查原因
prometheus的配置⽂件中关于告警的配置如下图
定位问题时发现,当添加了告警规则⽂件node-up.rules后重启prometheus失败,所以问题出在该⽂件上。
解决⽅式
使⽤prometheus⾃带的⼯具promtool检查配置⽂件。如果不熟悉如何使⽤该⼯具,可先直接执⾏./promtool查看帮助信息。
.
/promtool check l
显⽰告警规则⽂件中使⽤了tab键作为缩进
l
SUCCESS: 1 rule files found
Checking /usr/local/prometheus/rules/node-up.rules
FAILED:
/usr/local/prometheus/rules/node-up.rules: yaml: line 5: found a tab character that violates indentation
/usr/local/prometheus/rules/node-up.rules: yaml: line 5: found a tab character that violates indentation
告警规则⽂件如下
1 groups:
exited2 - name: node-up
3  rules:
4  - alert: node-up
5    expr: up{job="node_exporter"}==0
6    for: 15s
7    labels:
8      severity: 1
9      team: node
10    annotations:
11      summary: "{{ $labels.instance }} has crashed over 15s! "
逐⾏检查并修改tab键的缩进后,使⽤promtool检查配置⽂件通过,然后重启prometheus成功。
prometheus.service⽂件:多余的双引号
现象描述
以服务⽅式启动prometheus时,prometheus.service配置⽂件中,将prometheus启动参数以双引号""括起来,prometheus启动失败。
排查原因
prometheus.service配置⽂件中将参数--config.file字段对应的参数以双引号""括起来,导致服务解析参数失败,根本原因尚不清楚。
需要指出的是,如果将每个独⽴的参数⽤双引号括起来是不会引发错误的,如:
"--config.file=/usr/local/l"不会导致错误;但是,如果将所有启动参数都以⼀个双引号括起来是会引发错误的。另外,如果以nohup⽅式启动prometheus,将启动参数⽤引号包括是可以成功启动的
nohup ./prometheus --config.file="/usr/local/l" --web.listen-address=":9090"&
解决⽅式
在服务的配置⽂件中,对于服务带启动参数的,不要将参数⽤双引号括起来。
当⽤引号包括启动参数时,相同的格式在命令⾏中可以⽣效,但是在配置⽂件中却⽆效,根本原因⼤概与systemd处理服务配置⽂件
(*.service)的⽅式有关。
感兴趣的朋友可以深度挖掘,也请不吝赐教。
selinux配置
现象描述
以服务⽅式启动prometheus失败,但是以nohup启动prometheus后台进程成功。查看service启动失败的⽇志(xxx-xx-x-
xxx为hostname):
Nov 3015:48:13 xxx-xx-x-xxx systemd[1]: Started Prometheus service.
Nov 3015:48:13 xxx-xx-x-xxx systemd[1]: prometheus.service: Main process exited, code=exited, status=203/EXEC
Nov 3015:48:13 xxx-xx-x-xxx systemd[1]: prometheus.service: Failed with result 'exit-code'.
Nov 3015:48:33 xxx-xx-x-xxx systemd[1]: prometheus.service: Service RestartSec=20s expired, scheduling restart.
Nov 3015:48:33 xxx-xx-x-xxx systemd[1]: prometheus.service: Scheduled restart job, restart counter is at 1.
Nov 3015:48:33 xxx-xx-x-xxx systemd[1]: Stopped Prometheus service.
排查原因
以服务⽅式启动prometheus失败,但是以nohup启动成功(prometheus的启动参数相同),说明问题出现在prometheus.service配置⽂件,或者是系统环境。进⼀步排查,定位问题在selinux配置上。
解决⽅式
临时关闭selinux(终端执⾏setenforce 0),再以服务⽅式启动prometheus。需要注意的是,当重启prometheus或者使⽤curl -XPOST ip:port/-/reload重新加载prometheus配置时,也应当临时关闭selinux,否则操作失败;另外,当重启系统后,临时关闭selinux的操作也会失效,系统根据配置⽂件/etc/selinux/config使配置⽣效。
永久关闭selinux,重启机器,再以服务⽅式启动prometheus。修改/etc/selinux/config⽂件,设置SELINUX=disabled。
放弃以服务⽅式启动prometheus,以nohup启动prometheus后台进程。
总结
❤  当修改prometheus的配置⽂件后,强烈建议先执⾏./promtool check l检查配置⽂件是否存在问题;
如果以nohup⽅式运⾏prometheus或者直接运⾏可执⾏⽂件./prometheus,可以从输出的⽇志⽂件中出服务运⾏的问题,对症解决;
如果以服务⽅式运⾏prometheus失败,检查prometheus的服务启动配置⽂件(/etc/systemd/system/prometheus.service),当使⽤service prometheus status分析不出清晰的原因时,对于centos系统,可以使⽤journalctl -u prometheus.service查看⽇志
(prometheus.service⽂件中需指定StandardOutput=journal和StandardError=journal);也可以查看 /var/log/message,过滤想要的信息;也可以使⽤nohup启动,分析⽇志记录查原因。
如果不想每次修改配置⽂件后都重启prometheus,可改⽤重载配置的⽅式。重载配置需要在prometheus的启动参数中追加--able-lifecycle(如:./prometheus --able-lifecycle)。当修改配置⽂件(⾮服务发现的部分)后,执⾏curl -XPOST ip:port/-
/reload(其中,ip:port为相应节点的ip和prometheus服务监听的端⼝)。
参考
学习Prometheus,可以阅览的相关⽂章,内容详细,条理清晰。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。