使用Promethues+Grafana监控Elasticsearch

Promethues+Grafana监控Elasticsearch

监控选用说明

虽然用Kibana来监控ES,能展示一些关键指标,但ES本身收集的指标并不全面,还需要在ES配置文件中设置xpack.monitoring.collection.enabled: true来开启监控指标的收集,并占用额外的集群资源。重点是当集群出故障时,故障期间可能也收集不到监控指标。

所以需要使用第三方监控组件来实现对ES的监控。目前开源监控组件最受欢迎的就是Promethues+Grafana,再使用elasticsearch-exportor从ES中获取监控指标,最终就可以通过Grafana来展示各种监控图表了。

指标上报流程说明

promethues、elasticsearch-exportor和grafana完成上报的流程如下图所示:

  1. elasticsearch-exportor通过elasticsearch的Rest Api周期性获取ES集群的各项指标,组装成promethues支持解析的指标数据格式,并暴露http端口来支持通过http的方式获取这些指标数据;
  2. promethues通过elasticsearch-exportor提供的http接口来获取指标数据;
  3. grafana使用promethues上的指标数据绘图展示监控面板。

实现监控的步骤

使用环境:centos7.9

部署方式:通过docker compose

搭建elasticsearch-exporter服务

1、创建elasticsearch-exportor目录

bash 复制代码
mkdir /home/es/elasticsearch_exporter

2、编写docker-compose.yml

bash 复制代码
cd /home/es/elasticsearch_exporter
vim docker-compose.yml

docker-compose.yml文件内容

yaml 复制代码
version: '2.1'

services:  
  elasticsearch_exporter:
    image: quay.io/prometheuscommunity/elasticsearch-exporter:latest
    command:
      - '--es.uri=http://192.168.8.87:9200'  # 内网IP
    restart: unless-stopped   # 表示容器退出时总是重启,但是不考虑docker守护进程运行时就已经停止的容器
    ports:
      - 9114:9114     

3、启动服务

bash 复制代码
cd /home/prome
# 后台启动
docker compose up -d 

4、确认elasticsearch-exporter成功连上了es

通过http请求获取metric来验证elasticsearch-exporter是否连上了

例如:http://192.168.8.87:9114/metrics

如果返回了elasticsearch_开头的相关的监控指标,则说明elasticsearch-exporter成功连接了elasticsearch

搭建promethues和grafana服务

1、创建目录

bash 复制代码
mkdir /home/prome

2、编写docker-compose.yml

bash 复制代码
cd /home/prome
vim docker-compose.yml

docker-compose.yml文件内容

yaml 复制代码
version: '2.1'

networks:
  monitor-net:
    driver: bridge

volumes:
    prometheus_data: {}
    grafana_data: {}

services:
  prometheus:
    image: prom/prometheus:v2.37.0
    container_name: prometheus
    volumes:
      - ./prometheus:/etc/prometheus
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    restart: unless-stopped   # 表示容器退出时总是重启,但是不考虑docker守护进程运行时就已经停止的容器
    ports:
      - 9090:9090
    networks:
      - monitor-net
    labels:
      org.label-schema.group: "monitoring"

  grafana:
    image: grafana/grafana:9.0.3
    container_name: grafana
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards
      - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
    environment:
      - GF_SECURITY_ADMIN_USER=${ADMIN_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin123}
      - GF_USERS_ALLOW_SIGN_UP=false
    restart: unless-stopped
    ports:
      - 3000:3000
    networks:
      - monitor-net
    labels:
      org.label-schema.group: "monitoring"

3、启动服务

bash 复制代码
cd /home/prome
# 后台启动
docker compose up -d 

4、配置拉取 elasticsearch-exporter的监控数据

修改配置文件 ./prometheus/prometheus.yml,在其末尾增加elasticsearch-exporter的地址配置,如下:

yaml 复制代码
- job_name: 'es_test'      
    static_configs:
      - targets: ['192.168.1.86:9114']

如图(需要注意yml文件的层级关系):

5、确认prometheus成功连上了elasticsearch-exporte

浏览器访问 IP:9090/status 进入到prometheus提供的web界面

例如:http://192.168.8.87:9090/status

看到如下页面,表示prometheus已启动成功

点击菜单栏的Graph,在这个页面可以查询到prometheus获取到的指标

prometheus成功连上了elasticsearch-exporte的现象:

(1)输入"el"可以出来选项

(2)查询其中一个指标,有如下展示:

6、访问浏览器访问IP:3000/进入grafana登录页面

例如:http://192.168.8.87:3000/

7、输入管理员账号密码(docker-compose.yml里配置的)

账号:admin

密码:admin123

8、导入仪表盘

grafana官网提供了elasticsearch-exportor上报指标的仪表盘,包含了上报到elasticsearch相关指标的监控图,可直接到官网下载后导入即可使用

下载页面为:
https://grafana.com/grafana/dashboards

右边列表可自选仪表盘

选择某个进入详情后,点击右侧的Download JSON即可下载

ps. 需要尝试几个,可能会导致部分监控面板无法正常显示。

本次记录我选择的是:

https://grafana.com/grafana/dashboards/17724-elasticsearch/

下载完成后,回到部署的Grafana服务里,在左侧展开的菜单栏中点击Dashboards下的Import

上传下载的json文件

点击Import即可导入仪表盘

点击左侧搜索框也可进入导入的面板

相关推荐
失散131 小时前
分布式专题——46 ElasticSearch高级查询语法Query DSL实战
java·分布式·elasticsearch·架构
Elasticsearch2 小时前
Elasticsearch 推理 API 增加了开放的可定制服务
elasticsearch
麦麦麦造9 小时前
小小 Postgres,何以替代 Redis、MongoDB 甚至 ES?
redis·mongodb·elasticsearch·postgresql
Zzz 小生9 小时前
Claude Code学习笔记(四)-助你快速搭建首个Python项目
大数据·数据库·elasticsearch
斯普信专业组14 小时前
使用Reindex迁移Elasticsearch集群数据详解(下)
大数据·elasticsearch
Jabes.yang17 小时前
Java求职面试: 互联网医疗场景中的缓存技术与监控运维应用
java·redis·spring security·grafana·prometheus·oauth2·互联网医疗
K_i13421 小时前
负载均衡:运维高可用的核心技术
负载均衡·grafana·prometheus
JAVA学习通1 天前
OJ竞赛平台----C端题目列表
java·开发语言·jvm·vue.js·elasticsearch
Elasticsearch1 天前
根据用户行为数据中的判断列表在 Elasticsearch 中训练 LTR 模型
elasticsearch
失散131 天前
分布式专题——45 ElasticSearch基础数据管理详解
java·分布式·elasticsearch·架构