【Prometheus】docker-compose方式搭建服务端

一、概述

简单来说,就是下面四个特性:

  • 多维度数据模型

  • 方便的部署和维护

  • 灵活的数据采集

  • 强大的查询语言

实际上,多维度数据模型和强大的查询语言这两个特性,正是时序数据库所要求的,所以 Prometheus 不仅仅是一个监控系统,同时也是一个时序数据库。那为什么 Prometheus 不直接使用现有的时序数据库作为后端存储呢?这是因为 SoundCloud 不仅希望他们的监控系统有着时序数据库的特点,而且还需要部署和维护非常方便。纵观比较流行的时序数据库(参见下面的附录),他们要么组件太多,要么外部依赖繁重,比如:Druid 有 Historical、MiddleManager、Broker、Coordinator、Overlord、Router 一堆的组件,而且还依赖于 ZooKeeper、Deep storage(HDFS 或 S3 等),Metadata store(PostgreSQL 或 MySQL),部署和维护起来成本非常高。而 Prometheus 采用去中心化架构,可以独立部署,不依赖于外部的分布式存储,你可以在几分钟的时间里就可以搭建出一套监控系统。

此外,Prometheus 数据采集方式也非常灵活。要采集目标的监控数据,首先需要在目标处安装数据采集组件,这被称之为 Exporter,它会在目标处收集监控数据,并暴露出一个 HTTP 接口供 Prometheus 查询,Prometheus 通过 Pull 的方式来采集数据,这和传统的 Push 模式不同。不过 Prometheus 也提供了一种方式来支持 Push 模式,你可以将你的数据推送到 Push Gateway,Prometheus 通过 Pull 的方式从 Push Gateway 获取数据。目前的 Exporter 已经可以采集绝大多数的第三方数据,比如 Docker、HAProxy、StatsD、JMX 等等,官网有一份 Exporter 的列表。

除了这四大特性,随着 Prometheus 的不断发展,开始支持越来越多的高级特性,比如:服务发现,更丰富的图表展示,使用外部存储,强大的告警规则和多样的通知方式。下图是 Prometheus 的整体架构图:

 从上图可以看出,Prometheus 生态系统包含了几个关键的组件:Prometheus server、Pushgateway、Alertmanager、Web UI 等,但是大多数组件都不是必需的,其中最核心的组件当然是 Prometheus server,它负责收集和存储指标数据,支持表达式查询,和告警的生成。接下来我们就来安装 Prometheus server。

二、安装 Prometheus server

安装方式有二进制包方式安装,docker容器方式安装。我这里采用docker-compose的脚本启动docker容器,和alertmanager一块运行了。

[root@idc-zabbix ~]# cd /home/work/prometheus/
[root@idc-zabbix prometheus]# cat docker-compose.yml 
version: "3.8"

services:

  alertmanager:
    image: prom/alertmanager:latest
    container_name: alertmanager
    volumes:
      - ./config/alertmanager.yml:/etc/alertmanager/alertmanager.yml
      - ./templates:/etc/alertmanager/templates
    ports:
      - 9093:9093
      - 9094:9094
    networks:
      - prom

  prometheus:
    depends_on:
      - alertmanager
    image: prom/prometheus:latest
    container_name: prometheus
    volumes:
      - ./config/prometheus.yml:/etc/prometheus/prometheus.yml
      - ./config/alert:/etc/prometheus/alert
    ports:
      - 9090:9090
    networks:
      - prom
    command:
      - "--config.file=/etc/prometheus/prometheus.yml"

  dingdingtalk:
    depends_on:
      - alertmanager
    image: timonwong/prometheus-webhook-dingtalk:v1.4.0
    container_name: dingdingtalk
    ports:
      - 8060:8060
    networks:
      - prom
    entrypoint: /bin/prometheus-webhook-dingtalk --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=***********" 

  grafana:
    depends_on:
      - prometheus
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - 3000:3000
    networks:
      - prom

networks:
  prom:
    name: prometheus
    driver: bridge

运行前需要创建挂载所需目录和配置文件

[root@idc-zabbix prometheus]# mkdir -p config/alert
[root@idc-zabbix prometheus]# mkdir templates
[root@idc-zabbix prometheus]# ll
total 4
drwxr-xr-x 3 root root  65 May 17 15:00 config
-rw-r--r-- 1 root root 810 May  7 16:44 docker-compose.yml
drwxr-xr-x 2 root root  25 May 13 15:46 templates

[root@idc-zabbix config]# pwd
/home/work/prometheus/config
[root@idc-zabbix config]# ll
total 8
drwxr-xr-x 2 root root   71 May 14 17:20 alert
-rw-r--r-- 1 root root 1131 May 13 15:39 alertmanager.yml
-rw-r--r-- 1 root root 2051 May 17 14:56 prometheus.yml

三、配置 Prometheus

Prometheus 有一个配置文件,通过参数 --config.file 来指定,配置文件格式为 YAML。我们可以打开默认的配置文件 prometheus.yml 看下里面的内容:

etc/prometheus $ cat prometheus.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=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
 
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
 
    static_configs:
    - targets: ['localhost:9090']

Prometheus 默认的配置文件分为四大块:

  • global 块:Prometheus 的全局配置,比如 scrape_interval 表示 Prometheus 多久抓取一次数据,evaluation_interval 表示多久检测一次告警规则;

  • alerting 块:关于 Alertmanager 的配置,这个我们后面再看;

  • rule_files 块:告警规则,这个我们后面再看;

  • scrape_config 块:这里定义了 Prometheus 要抓取的目标,我们可以看到默认已经配置了一个名称为 prometheus 的 job,这是因为 Prometheus 在启动的时候也会通过 HTTP 接口暴露自身的指标数据,这就相当于 Prometheus 自己监控自己,虽然这在真正使用 Prometheus 时没啥用处,但是我们可以通过这个例子来学习如何使用 Prometheus;可以访问 http://localhost:9090/metrics 查看 Prometheus 暴露了哪些指标;

四、访问prometheus

浏览器打开prometheus-server的IP加端口。

http://192.168.1.180:9090

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值