Prometheus支持多种操作系统,例如Linux、Windows和Max OSX等。在产品官网上提供了独立的二进制文件进行下载,可下载对应的tar包并在相应系统的服务器上进行安装部署。当然,做为与容器有着紧密联系的监控系统,Promethesu也可以很方便的通过docker、kubernetes等容器平台进行部署。
Prometheus的部署安装非常简单,本文将演示通过二进制文件以及Docker的部署方式,部署环境操作系统为Centos7.8,其他环境的安装方式可自行参考官网的安装文档。
一.二进制安装1. 下载安装包
$ cd /opt $ wget https://github.com/prometheus/prometheus/releases/download/v2.20.0/prometheus-2.20.0.linux-386.tar.gz
2. 解压tar包,拷贝二进制文件到bin目录
$ tar -xvf prometheus-2.20.0.linux-386.tar.gz $ cd prometheus-2.20.0.linux-386 $ sudo cp prometheus /usr/local/bin/ $ sudo cp promtool /usr/local/bin/
3. 运行--versoin 检查版本
$ prometheus --version prometheus, version 2.20.0 (branch: HEAD, revision: e5a06b483527d4fe0704b8fa3a2b475b661c526f) build user: root@ac954b6d5c6e build date: 20200722-18:56:15 go version: go1.14.6
4. 启动
在本例中我们使用默认的配置文件来启动prometheus。
创建/etc/prometheus目录,并移动安装包的配置文件到此路径
$ sudo mkdir /etc/prometheus $ sudo cp prometheus.yml /etc/prometheus/
通过promtool工具,检测配置文件是否正确。
$ promtool check config /etc/prometheus/prometheus.yml Checking /etc/prometheus/prometheus.yml SUCCESS: 0 rule files found
启动Prometheus,并指定配置文件。
$ prometheus --config.file /etc/prometheus/prometheus.yml &
说明:Prometheus默认只保留15天的监控数据,可通过--storage.tsdb.retention选项控制时间序列的保留时间;--storage.tsdb.path选项可用于控制时间序列数据库位置,默认数据目录位于运行Prometheus的目录中。
启动完成后,打开浏览器,访问http://$IP:9090 可看到系统界面。
二. Docker安装
docker的安装方式很简单,只需要一条命令即可
$ docker run --name prometheus -d -p 9090:9090 prom/prometheus
如果要将配置文件与容器分离,可将prometheus.yml文件保存在本地目录 ,在启动时通过 -v的参数挂载到容器上面
$ mkdir /etc/prometheus
$ vi /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
$ docker run --name prometheus -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus三. 配置介绍
Prometheus使用prometheus.yml进行设置,通过在启动prometheus时指定相关的配置文件,进行配置文件的加载。
在上面下载的安装包中,可看到已有默认的promehteus.yml文件,其内容如下 :
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 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=` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
该配置文件分为四个模块:global、alerting、rule_files、scrape_configs,它们分别具有以下用途:
global
用于定义prometheus的全局配置。
scrape_interval :用来指定Prometheus从监控端抓取数据的时间间隔(默认为15s),如果在特定的job指定时间间隔指标,则该job的配置会覆盖全局设置。
evaluation_interval:用于指定检测告警规则的时间间隔,Prometheus每15s重新检测告警规则,并对变更进行更新。
alerting
用于设置Prometheus与alertmanager的通信,在Prometheus的整体架构中,Prometheus会根据配置的告警规则触发警报,并由独立的alertmanager组件进行告警管理。
static_configs代表使用静态配置,当前默认配置为注释的alertmanager:9093,此处可以先不理会,关于alertmanager后续的文档会有详细的介绍
rule_files
用于指定告警规则的文件路径,文件格式为yml。
Prometheus的告警规则都是通过yml文件进行配置,对于用惯了zabbix完善图形界面的人来说,一开始可能不会太习惯。但这也是Promthesu的特点之一,这种方式提供非常开放性的定制化功能,可以根据自己需要进行各类规则的定制化配置。
scrape_configs
用于指定Prometheus抓取的目标信息
Prometheus对于监控数据的抓取,通过配置job的方式进行操作。在job里面指定了一组目标抓取所必须的信息,例如目标地址、端口、标签和验证信息等。抓取的时间间隔使用上面global模块配置的时间,也可在该job中单独指定。
在实际环境中,通常会根据抓取目标的类型不同,如Mysql、mongodb、kafka等,分成多个job来进行。
默认配置只有一个监控目标,即prometheus server本身,端口为9090,如果不指定路径,默认会从/metrics路径抓取。
专注于内容,只做有质量的输出,欢迎关注个人公众号“运维老兵”。



