Prometheus监控之安装配置

时间:2022-11-21 11:03:56

1、下载

https://prometheus.io/download/
https://github.com/prometheus/prometheus/releases

2、安装配置

tar xvf prometheus-2.6.1.linux-amd64.tar.gz 
# 启动 Prometheus.
# 默认地, Prometheus 在 ./data 路径下存储其数据库 (flag --storage.tsdb.path).
# 在安装目录里面启动
./prometheus --config.file=prometheus.yml

3、配置参数

参数名称  含义 备注
--version 显示应用的版本信息
配置文件参数
--config.file="prometheus.yml" Prometheus配置文件路径
WEB服务参数
--web.listen-address="0.0.0.0:9090" UI、API、遥测(telemetry)监听地址
--web.read-timeout=5m 读取请求和关闭空闲连接的最大超时时间 默认值:5m
--web.max-connections=512 最大同时连接数 默认值:512
--web.external-url=<URL> 可从外部访问普罗米修斯的URL 如果Prometheus存在反向代理时使用,用于生成相对或者绝对链接,返回到
Prometheus本身,如果URL存在路径部分,它将用于给Prometheus服务的所有HTTP端点加前缀,如果省略,将自动派生相关的URL组件。
--web.route-prefix=<path> Web端点的内部路由 默认路径:--web.external-url
--web.user-assets=<path> 静态资产目录的路径 在/user路径下生效可用
--web.enable-lifecycle 通过HTTP请求启用关闭(shutdown)和重载(reload)
--web.enable-admin-api 启用管理员行为API端点
--web.console.templates="consoles" 总线模板目录路径 在/consoles路径下生效可用
--web.console.libraries="console_libraries" 总线库文件目录路径
--web.page-title="Prometheus Time Series Collection and Processing Server" Prometheus实例的文档标题
--web.cors.origin=".*" CORS来源的正则Regex,是完全锚定的 例如:'https?://(domain1|domain2).com'
数据存储参数
--storage.tsdb.path="data/" 指标存储的根路径
--storage.tsdb.retention=STORAGE.TSDB.RETENTION [DEPRECATED]样例存储时间 此标签已经丢弃,用"storage.tsdb.retention.time"替代
--storage.tsdb.retention.time=STORAGE.TSDB.RETENTION.TIME 存储时长,如果此参数设置了,会覆盖"storage.tsdb.retention"参
数;如果设置了"storage.tsdb.retention" 或者"storage.tsdb.retention.size"参数,存储时间默认是15d(天),单位:y, w,
d, h, m, s, ms
--storage.tsdb.retention.size=STORAGE.TSDB.RETENTION.SIZE [EXPERIMENTAL]试验性的。存储为块的最大字节数,需要使用一个
单位,支持:B, KB, MB, GB,TB, PB, EB 此标签处于试验中,未来版本会改变
--storage.tsdb.no-lockfile 不在data目录下创建锁文件
--storage.tsdb.allow-overlapping-blocks [EXPERIMENTAL]试验性的。允许重叠块,可以支持垂直压缩和垂直查询合并。
--storage.tsdb.wal-compression 压缩tsdb的WAL WAL(Write-ahead logging, 预写日志),WAL被分割成默认大小为128M的文件段
(segment),之前版本默认大小是256M,文件段以数字命名,长度为8位的整形。WAL的写入单位是页(page),每页的大小为32KB,所
以每个段大小必须是页的大小的整数倍。如果WAL一次性写入的页数超过一个段的空闲页数,就会创建一个新的文件段来保存这些页,从
而确保一次性写入的页不会跨段存储。
--storage.remote.flush-deadline=<duration> 关闭或者配置重载时刷新示例的等待时长
--storage.remote.read-sample-limit=5e7 在单个查询中通过远程读取接口返回的最大样本总数。0表示无限制。对于流式响应类型,将忽
略此限制。
--storage.remote.read-concurrent-limit=10 最大并发远程读取调用数。0表示无限制。
--storage.remote.read-max-bytes-in-frame=1048576 在封送处理之前,用于流式传输远程读取响应类型的单个帧中的最大字节数。
请注意,客户机可能对帧大小也有限制。 默认情况下,protobuf建议使用1MB。
告警规则相关参数
--rules.alert.for-outage-tolerance=1h 允许prometheus中断以恢复“for”警报状态的最长时间。
--rules.alert.for-grace-period=10m 警报和恢复的“for”状态之间的最短持续时间。这仅对配置的“for”时间大于宽限期的警报进行维护。
--rules.alert.resend-delay=1m 向Alertmanager重新发送警报之前等待的最短时间。
告警管理中心相关参数
--alertmanager.notification-queue-capacity=10000 挂起的Alertmanager通知的队列容量。 默认值:10000
--alertmanager.timeout=10s 发送告警到Alertmanager的超时时间 默认值:10s
数据查询参数
--query.lookback-delta=5m 通过表达式解析和联合检索指标的最大反馈时间 默认值:5m
--query.timeout=2m 查询中止前可能需要的最长时间。 默认值:2m
--query.max-concurrency=20 并发(concurrently)执行查询的最大值
--query.max-samples=50000000 单个查询可以加载到内存中的最大样本数。注意,如果查询试图将更多的样本加载到内存中,则会失败,
因此这也限制了查询可以返回的样本数。 数量级:5千万
日志信息参数
--log.level=info 仅记录给定的日志级别及以上的信息 可选参数值:[debug, info, warn, error],其中之一
--log.format=logfmt 日志信息输出格式 可选参数值:[logfmt, json],其中之一

常用的几个参数
--config.file=/usr/local/prometheus/prometheus.yml #Prometheus配置文件路径
--storage.tsdb.path=/usr/local/prometheus/data #指定数据落地的目录,默认为家目录的data目录
--storage.tsdb.retention.time=90d #设置数据保留时间为90天,默认为15天
--web.enable-lifecycle #在修改了prometheus.yml之后 可以不重启,直接热加载更新配置
--web.enable-admin-api #启用api,可以进行数据清理功能
--web.max-connections=512 #设置最大同时连接数
--web.external-url "http://192.168.1.4:9090" #注意:指定ip,此ip用于报警消息中超链接跳转所用
--web.listen-address=0.0.0.0:9090 #prometheus默认监控端口

4、加入systemd管理

1、配置文件
cat /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus server daemon
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path="/usr/local/prometheus/data" \
--storage.tsdb.retention=15d \
--web.console.templates="/usr/local/prometheus/consoles" \
--web.console.libraries="/usr/local/prometheus/console_libraries" \
--web.max-connections=512 \
--web.external-url "http://192.168.1.4:9090" \
--web.listen-address=0.0.0.0:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target

2、启动程序介绍
ExecStart=/usr/local/prometheus/prometheus #启动运行prometheus程序所在的路径
--config.file=/usr/local/prometheus/prometheus.yml #指定prometheus.yml配置文件路径
--storage.tsdb.path="/usr/local/prometheus/data" #指定监控指标数据存储的路径.默认在prometheus解压包目录下data目录
--storage.tsdb.retention=15d #历史数据最大保留时间,默认15天
--web.console.templates="/usr/local/prometheus/consoles" #指定控制台模板目录路径
--web.console.libraries="/usr/local/prometheus/console_libraries" #指定控制台库目录路径
--web.max-connections=512 #设置最大同时连接数
--web.external-url "http://192.168.1.4:9090" #用于生产返回prometheus相对的绝对链接地址,可以在后续告警通知内容中直接
点击链接地址访问prometheus Web UI。其格式为:http://{ip或者域名}:9090
--web.listen-address=0.0.0.0:9090 #prometheus默认监控端口

3、systemctl命令介绍:
systemctl daemon-reload #通知systemctl重新加载配置文件
systemctl enable prometheus.service #设置为开机自启动
systemctl disable prometheus.service #如果不想设置为开机启动,可以关闭。关闭开机自启动
systemctl start prometheus.service #开启服务
systemctl status prometheus.service #查看服务状态
systemctl restart prometheus.service #重启服务
systemctl stop prometheus.service #关闭服务