使用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即可导入仪表盘

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

相关推荐
一次旅行21 小时前
【效率秘籍】Shell 函数+别名组合拳,让 80% 的重复命令变 1 个字母
大数据·elasticsearch·搜索引擎
暖和_白开水21 小时前
数据分析agent(九):es_client_manager.py 和分词器ik_max_word问题
elasticsearch·数据分析·c#
码农学院1 天前
Elasticsearch构建汽车零配件智能搜索与匹配系统
大数据·elasticsearch·汽车
行者-全栈开发2 天前
TIG 监控体系搭建:Telegraf + InfluxDB + Grafana 全链路实战
grafana·influxdb·服务器监控·tig·docker部署·监控系统·telegraf
Elastic 中国社区官方博客2 天前
不到 5 分钟完成本地部署:Jina embedding 模型现已支持本地部署
大数据·人工智能·elasticsearch·搜索引擎·embedding·jina
二进制流水搬运工2 天前
入职第一天拉了23个仓库,我写了个脚本解放双手
大数据·elasticsearch·搜索引擎
Elasticsearch2 天前
在不到一小时内将 Datadog Kubernetes 仪表板迁移到 Elastic Observability
elasticsearch
.柒宇.2 天前
Elasticsearch 核心概念与系统架构详解
elasticsearch·系统架构
风行無痕2 天前
单机Elasticsearch 8.19.18安全升级到9.4.3的过程
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客2 天前
使用重新设计的 AutoOps 更快地进行 Elasticsearch 问题排查
大数据·运维·前端·人工智能·elasticsearch·搜索引擎·全文检索