使用Prometheus监控Docker[macOS Mojave]

Prometheus是一个开源的系统监控报警工具。本文示例将Docker作为其监控目标的配置方法。

配置Docker


在daemon.json加入如下配置后重启docker。

//指定metrics采集端口
{
  "metrics-addr" : "127.0.0.1:9323",
  "experimental" : true
}

配置和启动Prometheus

  1. 创建docker swarm环境
    Prometheus在docker swarm环境中以service的方式运行,所以我们需要使用docker swarm init初始化docker swarm环境。(此处笔者是单节点,对于多节点集群请继续执行docker swarm join完成swarm环境搭建)

  2. 创建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=` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ['docker.for.mac.localhost:9090']

  - job_name: 'docker'
         # metrics_path defaults to '/metrics'
         # scheme defaults to 'http'.

    static_configs:
      - targets: ['docker.for.mac.host.internal:9323']
  1. 启动Prometheus
    执行如下命令,以service的方式启动Prometheus
docker service create --replicas 1 --name my-prometheus \
    --mount type=bind,source=/tmp/prometheus.yml,destination=/etc/prometheus/prometheus.yml \
    --publish published=9090,target=9090,protocol=tcp \
    prom/prometheus

访问http://localhost:9090/targets/查看结果

参考:Collect Docker metrics with Prometheus

你可能感兴趣的:(开发工具,运维)