在Mac上使用docker-compose启动Prometheus和Grafana

普罗米修斯很受欢迎啊。它采用了拉模型,可以通过HTTP收集度量数据,所以可以在本地工作机上启动监视服务器进行测试和确认操作,这也是一个很好的优点。

Prometheus本身和exporter都提供了Docker镜像,这使得在Docker上进行环境配置变得很容易,这也符合现代化的开源软件风格。

我打算使用docker-compose在本地Mac上尝试启动。

如何在本地Mac上安装Prometheus和Grafana

Docker用于开发、部署和运行应用程序的开源平台。

请从上述链接中下载并安装Mac版本的dmg文件。

Docker Compose => Docker组合

$ sudo pip install docker-compose

配置文件

prometheus.yml — 普罗米修斯配置文件

这是 Prometheus 的配置文件。
在下面的设置中,我们只启用了 Prometheus 自身的监控(job_name: ‘prometheus’)。

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 
  external_labels:
      monitor: 'codelab-monitor'

rule_files:
#   - "/var/app/prometheus/alert.rules"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: 
        - 'localhost:9090'

grafana环境配置文件

这是一个整理了要传递给Grafana的环境变量的配置文件。
当通过Docker启动Grafana时,我们采用传递环境变量的方式来覆盖现有的设置。我们将通过Docker Compose传递这个文件。
实际上,在本文中我们不会使用这个文件,所以没有这个文件也可以启动,但是我们将它准备作为设置文件的模板。

# [server]
# GF_SERVER_DOMAIN=localhost
# GF_SERVER_HTTP_PORT=3000
# GF_SERVER_PROTOCOL=http

docker-compose.yml 的含义是 “Docker 组合文件”。

这是一个用于通过docker-compose启动prometheus和grafana的配置文件。
您可以将prometheus.yml和grafana.env放置在任意位置。

version: '3'
services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - /var/app/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - 3000:3000
    env_file:
      - /var/app/prometheus/grafana.env

开始

$ docker-compose -f docker-compose.yml up

Creating network "prometheus_default" with the default driver
Creating grafana
Creating prometheus
Attaching to prometheus, grafana
grafana       | t=2017-05-29T04:57:48+0000 lvl=info msg="Starting Grafana" logger=main version=4.3.0-beta1 commit=3a89272 compiled=2017-05-12T09:45:26+0000
grafana       | t=2017-05-29T04:57:48+0000 lvl=info msg="Config loaded from" logger=settings file=/usr/share/grafana/conf/defaults.ini
......

在启动后,连接到以下URL,您可以分别查看Web用户界面。

    • Prometheus : http://localhost:9090/

Grafana : http://localhost:3000/

在Grafana界面上输入任意的电子邮件地址并进行注册,即可生成一个账号。

将Prometheus仪表板添加到Grafana中。

作为 Prometheus 和 Grafana 的协同示例,在这里我们将把 Prometheus 预先准备好的仪表板添加到 Grafana 中。

1. 添加组织

image.png

2. 添加数据来源

image.png

点击左上角的菜单按钮,选择 “数据源”,然后在数据源页面点击 “+添加数据源” 按钮来添加 Prometheus 的配置。

image.png
    • Name : (任意)

 

    • Type: Prometheus

 

    • Url : http://localhost:9090

Access: direct

3. 添加仪表盘

点击 Prometheus 数据源的 “仪表盘” 选项卡,将显示如下界面。

image.png

点击此处的“导入”按钮后,Prometheus的仪表板将被添加到Grafana中。

image.png

新增节点导出器

为了监控本地 Mac 系统指标,需要进行 node-exporter 的配置,它是用于收集指标的导出器。

docker-compose.yml 文件

由于node-exporter还提供了docker镜像,因此我们将修改先前的docker-compose.yml配置如下。

version: '3'
services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    volumes:
      - /var/app/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    ports:
      - 9090:9090
  grafana:
    image: grafana/grafana
    container_name: grafana
    ports:
      - 3000:3000
    env_file:
      - /var/app/prometheus/grafana.env
  node-exporter:
    image: quay.io/prometheus/node-exporter
    container_name: node-exporter
    ports:
      - 9100:9100
    volumes:
      - /proc:/host/proc
      - /sys:/host/sys
      - /:/rootfs

这次,我们将在http://localhost:9100/上启动node-exporter。

prometheus.yml -> 普罗米修斯配置文件

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 
  external_labels:
      monitor: 'codelab-monitor'

rule_files:
#   - "/var/app/prometheus/alert.rules"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: 
        - 'localhost:9090'
  - job_name: 'node-exporter'
    static_configs:
      - targets:
        - '(host or ip address):9100'

将 node-exporter 的配置添加到 targets 中。
虽然你可能想要在这里写上 localhost:9100,但是由于 Prometheus 在 Docker 容器中运行,所以 localhost 地址会指向容器本身。
因此,你需要指定除了你的 Mac 分配的 localhost 之外的其他 IP 地址或主机名。

开始

$ docker-compose -f docker-compose.yml up

在启动后,打开以下 URL,您可以确认已添加的导出器包含在目标中。

image.png

如果指示 UP,那么说明 Prometheus-exporter 之间的通讯是正常的。

图表

在添加exporter后,您可以在Prometheus和Grafana的界面上获取已添加的exporter度量信息,并能够通过图形等方式进行可视化。

image.png

通过随时添加能够收集所需指标的导出器,并将其与Prometheus集成,以进行Prometheus和Grafana的配置。

bannerAds