prometheus数据远程写入elasticsearch方式

 更新时间:2025年06月23日 09:47:42   作者:yololee_  
这篇文章主要介绍了prometheus数据远程写入elasticsearch方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

prometheus数据远程写入elasticsearch

一、部署elasticsearch

version: '3'

# 网桥es -> 方便相互通讯
networks:
  es:
    driver: bridge

services:
  elasticsearch:
    image: elasticsearch:7.14.1
    container_name: elasticsearch             # 容器名为'elasticsearch'
    restart: unless-stopped                           # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
    volumes:                                  # 数据卷挂载路径设置,将本机目录映射到容器目录
      - "./elasticsearch/data:/usr/share/elasticsearch/data"
      - "./elasticsearch/logs:/usr/share/elasticsearch/logs"
      - "./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml"
#      - "./elasticsearch/config/jvm.options:/usr/share/elasticsearch/config/jvm.options"
      - "./elasticsearch/plugins/ik:/usr/share/elasticsearch/plugins/ik" # IK中文分词插件
    environment:                              # 设置环境变量,相当于docker run命令中的-e
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      TAKE_FILE_OWNERSHIP: "true"  # 权限
      discovery.type: single-node
      ES_JAVA_OPTS: "-Xmx512m -Xms512m"
      ELASTIC_PASSWORD: "123456" # elastic账号密码
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - es

  kibana:
    image: kibana:7.14.1
    container_name: kibana
    restart: unless-stopped
    volumes:
      - ./elasticsearch/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
    ports:
      - "5601:5601"
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    networks:
      - es

其中的配置文件和插件查看地址:https://gitee.com/huanglei1111/docker-compose/tree/master/Linux/elasticsearch

查看是否运行成功

访问elasticsearch:ip:端口

访问kibana:ip:端口

  • 用户名:elastic
  • 密码:123456

二、部署prometheus

version: "3"

# 网桥 -> 方便相互通讯
networks:
  prometheus:
    ipam:
      driver: default
      config:
        - subnet: "172.22.0.0/24"
services:
  # 开源的系统监控和报警系统
  prometheus:
    image: prom/prometheus:v2.34.0
    container_name: hl-prometheus
    restart: unless-stopped
    volumes:
      - ./config/prometheus.yml:/etc/prometheus/prometheus.yml
      # - "./web-config.yml:/etc/prometheus/web-config.yml"
    command: 
      --config.file=/etc/prometheus/prometheus.yml
      --web.enable-lifecycle
      # --web.config.file=/etc/prometheus/web-config.yml
    ports:
      - "19090:9090"
    depends_on:
      - hl-node-exporter
    networks:
      prometheus:
        ipv4_address: 172.22.0.11

  # 采集服务器层面的运行指标
  node-exporter:
    image: prom/node-exporter:v1.3.1
    container_name: hl-node-exporter
    restart: unless-stopped
    volumes:
      - "./node-exporter/proc:/host/proc:ro"
      - "./node-exporter/sys:/host/sys:ro"
      - "./node-exporter:/rootfs:ro"
    ports:
      - "19100:9100"
    networks:
      prometheus:
        ipv4_address: 172.22.0.22
  # 使用prometheusbeat 把prometheus的数据存储到elasticsearch中
  beat:
    image: infonova/prometheusbeat
    container_name: hl-prometheusbeat
    ports:
      - 18081:8080
    depends_on:
      - hl-prometheus
    volumes:
      - "./config/prometheusbeat.yml:/prometheusbeat.yml"
      - "/etc/localtime:/etc/localtime"
    networks:
      prometheus:
         ipv4_address: 172.22.0.33

相关配置文件查看地址:https://gitee.com/huanglei1111/docker-compose/tree/master/Linux/prometheus/prometheus-es/config

查看是否部署成功

查看监控指标是否健康

注意:状态为down,注意修改prometheus.yml配置文件中targets的ip为服务器ip

三、通过prometheusbeat写入数据到es

上面在部署docker-compose-prometheus.yml文件已经部署啦

如果es有用户密码,需要在prometheusbeat.yml配置文件中添加配置

prometheusbeat:
  # prometheusbeat 服务的端口。默认为8080
  listen: ":8080"
  # Context path. Defaults to /prometheus
  context: "/prometheus"
output.elasticsearch:
  # elasticsearch 的地址
  hosts: ["127.0.0.1:9200"]
  username: "elastic"
  password: "123456"

其次修改prometheus.yml配置文件

prometheus 通过 remote_write 来实现远端存储,所以在 prometheus.yml 配置文件中增加 remote_write 参数

global:
  scrape_interval: 10s
  scrape_timeout: 10s
  evaluation_interval: 10m
remote_write:
# 远程写入到prometheusbeat中
- url: "http://127.0.0.1:18081/prometheus"
  write_relabel_configs:
  - source_labels: [__name__]
    action: keep
    regex: go_gc_cycles_automatic_gc_cycles_total
  remote_timeout: 30s
scrape_configs:
  # prometheus
  - job_name: prometheus
    static_configs:
      - targets: ['127.0.0.1:19090']
        labels:
          instance: prometheus

  # 采集node exporter监控数据,即linux
  - job_name: linux
    static_configs:
      - targets: ['127.0.0.1:19100']
        labels:
          instance: localhost

配置说明

  • url:prometheusbeat 的服务地址
  • write_relabel_configs:这个很有用,用于过滤数据,可以设置多个过滤条件,同时满足这些条件的数据才会被存到远端存储!
  • source_labels:根据哪个字段进行过滤
  • regex:值过滤的规则
  • action:keep(保留)/drop(丢弃)

所以,上面配置文件中的配置意思就是:prometheus 收到数据后,远程发送给 prometheusbeat,并且只发送 name 为 go_gc_cycles_automatic_gc_cycles_total

然后重启prometheus。之后当收集到数据时,就能成功保存到 elasticsearch 了,首次保存时会自动创建索引:prometheusbeat-7.3.1,数据都存在此索引中。

四、elasticsearch head验证

我们通过浏览器安装elasticsearch head插件,发现已经多了这个索引了

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java 高并发二:多线程基础详细介绍

    Java 高并发二:多线程基础详细介绍

    本文主要介绍Java 高并发多线程的知识,这里整理详细的资料来解释线程的知识,有需要的学习高并发的朋友可以参考下
    2016-09-09
  • IDEA如何切换数据库版本mysql5或mysql8

    IDEA如何切换数据库版本mysql5或mysql8

    本文介绍了如何将IntelliJ IDEA从MySQL5切换到MySQL8的详细步骤,包括下载MySQL8、安装、配置、停止旧服务、启动新服务以及更改密码等
    2025-01-01
  • spring boot tomcat jdbc pool的属性绑定

    spring boot tomcat jdbc pool的属性绑定

    这篇文章主要介绍了spring boot tomcat jdbc pool的属性绑定的相关资料,非常不错,具有参考借鉴价值,需要的朋友参考下
    2018-01-01
  • jdbc实现连接和增删改查功能

    jdbc实现连接和增删改查功能

    这篇文章主要为大家详细介绍了jdbc实现连接和基本的增删改查功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • IntelliJ IDEA连接MySQL数据库详细图解

    IntelliJ IDEA连接MySQL数据库详细图解

    今天小编就为大家分享一篇关于intellij idea连接mysql数据库详细图解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • 完美解决request请求流只能读取一次的问题

    完美解决request请求流只能读取一次的问题

    这篇文章主要介绍了完美解决request请求流只能读取一次的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 简单介绍java中equals以及==的用法

    简单介绍java中equals以及==的用法

    这篇文章主要介绍了简单介绍java中equals以及==的用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Java网络通信中ServerSocket的设计优化方案

    Java网络通信中ServerSocket的设计优化方案

    今天小编就为大家分享一篇关于Java网络通信中ServerSocket的设计优化方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • Spring MVC 请求参数绑定实现方式

    Spring MVC 请求参数绑定实现方式

    Spring MVC 是一个用于构建 Web 应用程序的框架,它提供了一种方便的方式来处理 HTTP 请求和响应,Spring MVC 提供了多种方式来实现请求参数绑定,本文结合实例代码给大家介绍的非常详细,需要的朋友跟随小编一起看看吧
    2023-09-09
  • Selenium Webdriver实现截图功能的示例

    Selenium Webdriver实现截图功能的示例

    今天小编就为大家分享一篇Selenium Webdriver实现截图功能的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05

最新评论