介绍
Prometheus是一个开源的系统监控和警报工具包,最初由 SoundCloud 开发并开源,现已成为云原生计算基金会(CNCF)的毕业项目。它广泛应用于监控基础设施、应用程序和服务的性能,并提供强大的数据查询和警报功能。许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发者和用户社区。它现在是一个独立的开源项目,社区版免费使用,独立于任何公司进行维护。
Prometheus 通过定期从目标(如服务器、应用程序、服务)中拉取(pull)指标数据,并将其存储在本地时间序列数据库中。
多种数据收集方式
- HTTP 端点:目标暴露一个 HTTP 端点,Prometheus 定期访问该端点以获取指标。
- Pushgateway:对于短生命周期的任务,可以通过 Pushgateway 将指标推送到 Prometheus。
- 服务发现:支持多种服务发现机制(如 Kubernetes、Consul、DNS),自动发现监控目标。
核心功能
- Prometheus 将其指标收集并存储为时间序列数据,即指标信息与记录时的时间戳一起存储,同时存储可选的键值对(称为标签)。
- Prometheus 提供了强大的查询语言 PromQL(Prometheus Query Language),用于查询和分析时间序列数据
- Prometheus 自带一个简单的 Web UI,可以用于查询和可视化数据。此外,Prometheus 通常与 Grafana 集成,提供更强大的数据可视化功能。
- Prometheus 提供了灵活的警报机制,用户可以通过 PromQL 定义警报规则。当某个指标满足特定条件时,Prometheus 会触发警报并通过 Alertmanager 发送通知。
应用场景
Docker安装
mkdir -p ~/prometheus_datadocker run --name prometheus \-c 2 -m 1g --memory-swap=1536m \-p 9090:9090 \-v ~/prometheus_data/prometheus.yml:/etc/prometheus/prometheus.yml \-v ~/prometheus_data:/prometheus \prom/prometheus:latest
Portainer安装
通过Portainer安装包含prometheus服务docker镜像容器极其简单方便,在Pull image》Image中输入: prom/prometheus:latest,点击"Pull the image"拉取仓库镜像文件;

稍等片刻拉取完毕后(镜像文件下载时长受网络环境影响,有时官方hub.docker.com平台会偶尔断连),Images列表中将会显示镜像信息;

在Image中输入: prom/prometheus:latest
设置容器映射端口Prot mapping: 9090>9090

在Volumes中设置容器卷映射目录,采用Bind模式,提前在docker服务主机任意路径下创建docker容器内的卷映射目录/prometheus_data,将prometheus运行过程中和收集到的数据存储在docker服务主机物理目录中,以防docker容器故障数据丢失不可用;
默认通过docker安装prometheus服务,会使用打包镜像中内置的prometheus.yml配置文件,但为了对prometheus进行自定义配置与维护管理,将prometheus默认加载的/etc/prometheus/prometheus.yml配置文件通过卷映射到docker服务主机物理文件/data2/docker/sysdata/prometheus_data/prometheus.yml上,从而避免在docker容器停机或故障的情况下,保证prometheus.yml配置不会留失或重置;
在prometheus_data目录下创建prometheus.yml自定义配置文件
# 创建自定义配置文件
touch prometheus.yml# 编辑自定义配置文件
vim prometheus.yml
将以下内容写入到prometheus.yml 中
# my global config
global:scrape_interval: 30s # Set the scrape interval to every 30 seconds. Default is every 1 minute.evaluation_interval: 60s # Evaluate rules every 15 seconds. The default is every 1 minute.body_size_limit: 5MB # An uncompressed response body larger than this many bytes will cause the scrape to fail. 0 means no limit.sample_limit: 1000 # If more than this number of samples are present after metric relabeling. the entire scrape will be treated as failed. 0 means no limit.# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: "prometheus"# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:9090"]
完整配置教程,参考官方文档: Configuration | Prometheus
注意:需要对prometheus_data物理目录进行授权,否则prometheus服务没有对该目录的读写权限,会导致运行出错,比如:err="open /prometheus/queries.active: permission denied",prometheus启动过程中要向该绑定卷物理目录内写入数据;


待上述配置完成后,点击Deploy the container按钮发布docker容器并会同时启动Prometheus服务,稍等片刻没有错误消息提示,即容器运行正常服务启动成功,同时通过docker服务主机下的prometheus_data也可查看到已有文件写入,如有错误提示,可在Containers列表中点击容器日志进行排查;

访问Prometheus
包含prometheus服务的docker容器启动后,通过浏览器访问:http://docker_ip:9090,进入到prometheus控制台默认页面,通过Query菜单即可查询已收集到prometheus服务存储数据库中的指标,比如,查询从prometheus自身端点采集指标:process_cpu_seconds_total,点击Graph选择卡,设置1小时数据区间,并选择30s样本频率,展示图表如下:

参考:
Installation | Prometheus