Prometheus是一个开源的系统监控报警工具。本文示例将Docker与Ruby应用作为其监控目标的配置方法。
配置Docker
在daemon.json加入如下配置后重启docker。
//指定metrics采集端口, Prometheus会定时从该端口拉取数据
{
"metrics-addr" : "127.0.0.1:9323",
"experimental" : true
}
配置和启动Ruby应用
- 安装Prometheus ruby客户端
https://github.com/prometheus/client_ruby
# Gemfile
gem 'prometheus-client'
将prometheus-client
添加到Gemfile后bundle install
安装客户端.
- 以中间件的形式加入rack
require 'rack'
require 'prometheus/middleware/collector'
require 'prometheus/middleware/exporter'
use Rack::Deflater
use Prometheus::Middleware::Collector
use Prometheus::Middleware::Exporter
run ->(_) { [200, {'Content-Type' => 'text/html'}, ['OK']] }
并以9100端口启动应用。
配置和启动Prometheus
将Prometheus以Docker的形式运行在本地。
- 创建Prometheus配置文件
将以下内容保存为/tmp/prometheus.yml
,作为Prometheus的配置文件
# 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).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first.rules"
# - "second.rules"
# 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.
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
# Prometheus服务
- job_name: 'prometheus'
static_configs:
- targets: ['docker.for.mac.localhost:9090']
# Docker
- job_name: 'docker'
static_configs:
- targets: ['docker.for.mac.host.internal:9323']
# Ruby应用,笔者Ruby应用的端口是9100
- job_name: "ruby-example"
static_configs:
- targets: ['docker.for.mac.localhost:9100']
- 启动Prometheus
执行如下命令,启动Prometheus
docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
访问http://localhost:9090/targets/查看结果
参考:
Collect Docker metrics with Prometheus
prometheus/client_ruby