Prometheus简介
Prometheus是一款由Google研发的开源系统监控和警报系统。其后端由Golang语言研发,通过HTTP协议周期性地拉取被监控的组件信息,任意组件只要提供对应的HTTP接口就可以接入到监控中。
Prometheus架构图如下所示(来自Promethrus官网):
Prometheus首先从job中抓取metrics(直接抓取或通过pushgateway),然后将所有抓取的数据本地化,通过运行规则,从现有数据聚合和记录新的时间序列或生成警报。使用Grafana或其他API,用户可以将收集的数据可视化。
Prometheus的主要特点如下:
- 多维数据模型:Prometheus采用多维数据模型,时序由metric名和key/value的labels构成,可以更灵活地存储和查询数据。
- 灵活的查询语句:Prometheus支持PromQL,这是一种灵活的查询语句,可以方便地分析和查询监控数据。
- 无依赖存储:Prometheus支持本地和远程不同的存储模型,无需依赖其他存储系统。
- 采用http协议:Prometheus使用http协议,采用pull模式拉取数据,简单易懂。
此外,Prometheus适合监控Docker容器,广泛应用于各种监控场景,如无人驾驶车辆的运行数据、证券行业的实时交易数据、实时运维监控数据等。
Ubuntu搭建Prometheus + Pushgateway环境
运行Prohetheus
通过docker拉取prometheus镜像
docker pull prom/prometheus
编写prometheus配置文件,让prometheus监控自身数据:
prometheus配置文件保存至/opt/prometheus下:
vim /opt/prometheus/prometheus.yml
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
运行prometheus容器,这里设置docker网络模式为host模式以和主机共享网络:
docker run -d -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml --net=host prom/prometheus
这将在后台运行prometheus容器,同时将容器内的/prometheus目录挂载到主机的/opt/prometheus目录下,以便在主机上访问prometheus的配置文件和数据文件。
成功运行后,浏览器访问localhost:9090,即可进入Prometheus图形界面,如下图所示:
运行Pushgateway
安装push gateway
docker pull prom/pushgateway
启动PushGateway
docker run -d --name=pgw --net=host prom/pushgateway
成功运行后,在浏览器访问localhost:9091,即可进入Pushgateway图形界面,如下图所示:
在Prometheus中配置Pushgateway对应的job
要使Prometheus能从Pushgateway中拉取信息,需要在/opt/prometheus/prometheus.yml中进行编辑,添加对应的job:
vim /opt/prometheus/prometheus.yml
添加如下内容:
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9091']
labels:
instance: pushgateway
重启Prometheus,查看运行状况
通过docker restart <container-id>命令重启Prometheus的容器。
访问localhost:9090/targets
可以看到,Prometheus已能够监控Pushgeteway。