prometheus數(shù)據(jù)遠(yuǎn)程寫入elasticsearch方式
prometheus數(shù)據(jù)遠(yuǎn)程寫入elasticsearch
一、部署elasticsearch
version: '3'
# 網(wǎng)橋es -> 方便相互通訊
networks:
es:
driver: bridge
services:
elasticsearch:
image: elasticsearch:7.14.1
container_name: elasticsearch # 容器名為'elasticsearch'
restart: unless-stopped # 指定容器退出后的重啟策略為始終重啟,但是不考慮在Docker守護(hù)進(jìn)程啟動時就已經(jīng)停止了的容器
volumes: # 數(shù)據(jù)卷掛載路徑設(shè)置,將本機(jī)目錄映射到容器目錄
- "./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: # 設(shè)置環(huán)境變量,相當(dāng)于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
TAKE_FILE_OWNERSHIP: "true" # 權(quán)限
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"
# 網(wǎng)橋 -> 方便相互通訊
networks:
prometheus:
ipam:
driver: default
config:
- subnet: "172.22.0.0/24"
services:
# 開源的系統(tǒng)監(jiān)控和報警系統(tǒng)
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
# 采集服務(wù)器層面的運行指標(biāo)
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的數(shù)據(jù)存儲到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相關(guān)配置文件查看地址:https://gitee.com/huanglei1111/docker-compose/tree/master/Linux/prometheus/prometheus-es/config
查看是否部署成功

查看監(jiān)控指標(biāo)是否健康
注意:狀態(tài)為down,注意修改prometheus.yml配置文件中targets的ip為服務(wù)器ip

三、通過prometheusbeat寫入數(shù)據(jù)到es
上面在部署docker-compose-prometheus.yml文件已經(jīng)部署啦
如果es有用戶密碼,需要在prometheusbeat.yml配置文件中添加配置
prometheusbeat: # prometheusbeat 服務(wù)的端口。默認(rèn)為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 來實現(xiàn)遠(yuǎn)端存儲,所以在 prometheus.yml 配置文件中增加 remote_write 參數(shù)
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
remote_write:
# 遠(yuǎn)程寫入到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監(jiān)控數(shù)據(jù),即linux
- job_name: linux
static_configs:
- targets: ['127.0.0.1:19100']
labels:
instance: localhost配置說明
- url:prometheusbeat 的服務(wù)地址
- write_relabel_configs:這個很有用,用于過濾數(shù)據(jù),可以設(shè)置多個過濾條件,同時滿足這些條件的數(shù)據(jù)才會被存到遠(yuǎn)端存儲!
- source_labels:根據(jù)哪個字段進(jìn)行過濾
- regex:值過濾的規(guī)則
- action:keep(保留)/drop(丟棄)
所以,上面配置文件中的配置意思就是:prometheus 收到數(shù)據(jù)后,遠(yuǎn)程發(fā)送給 prometheusbeat,并且只發(fā)送 name 為 go_gc_cycles_automatic_gc_cycles_total
然后重啟prometheus。之后當(dāng)收集到數(shù)據(jù)時,就能成功保存到 elasticsearch 了,首次保存時會自動創(chuàng)建索引:prometheusbeat-7.3.1,數(shù)據(jù)都存在此索引中。
四、elasticsearch head驗證
我們通過瀏覽器安裝elasticsearch head插件,發(fā)現(xiàn)已經(jīng)多了這個索引了


總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中finally和return的關(guān)系實例解析
這篇文章主要介紹了Java中finally和return的關(guān)系實例解析,總結(jié)了二者的關(guān)系,然后分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
基于SpringBoot實現(xiàn)一個方法級耗時監(jiān)控器
本文介紹基于SpringBoot實現(xiàn)的輕量級耗時監(jiān)控器,通過AOP攔截方法并分級存儲數(shù)據(jù),提供可視化統(tǒng)計界面,支持調(diào)用次數(shù)、耗時、失敗次數(shù)分析及多維排序,適合中小型項目快速集成,無需復(fù)雜配置,需要的朋友可以參考下2025-09-09
Springcloud Config配置中心使用與相關(guān)介紹
springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-09-09
SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法
本篇文章主要介紹了SpringMVC如何在生產(chǎn)環(huán)境禁用Swagger的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02

