Prometheus监控Elasticsearch

文章描述了如何下载并使用ElasticsearchExporter进行监控,通过systemd管理服务,设置相关参数,并配置Prometheus进行动态发现和定时抓取指标。此外,还提到了使用Grafana的仪表板ID14191来展示监控数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 下载elasticsearch exporter

https://github.com/prometheus-community/elasticsearch_exporter

2 systemd 管理elasticsearch exporter 

service文件:  /usr/lib/systemd/system/es_exporter.service

[Unit]
Description=es_exporter
Documentation=https://prometheus.io
After=network.target

[Service]
ExecStart=/usr/local/elasticsearch_exporter/elasticsearch_exporter --es.all --es.cluster_settings --es.indices --es.indices_settings --es.shards --es.snapshots --es.timeout=10s --web.listen-address ":9114" --web.telemetry-path "/metrics" --es.uri "http://172.16.53.120:19200"
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure


[Install]
WantedBy=multi-user.target

[Unit]
Description=es_exporter
After=network.target

elasticsearch_exporter参数:

      --collector.cluster-info  Enable the cluster-info collector (default: enabled).
      --web.listen-address=":9114"  
                                Address to listen on for web interface and telemetry.
      --web.telemetry-path="/metrics"  
                                Path under which to expose metrics.
      --es.uri="http://localhost:9200"  
                                HTTP API address of an Elasticsearch node.
      --es.timeout=5s           Timeout for trying to get stats from Elasticsearch.
      --es.all                  Export stats for all nodes in the cluster. If used, this flag will override the flag es.node.
      --es.node="_local"        Node's name of which metrics should be exposed.
      --es.indices              Export stats for indices in the cluster.
      --es.indices_settings     Export stats for settings of all indices of the cluster.
      --es.indices_mappings     Export stats for mappings of all indices of the cluster.
      --es.aliases              Export informational alias metrics.
      --es.cluster_settings     Export stats for cluster settings.
      --es.shards               Export stats for shards in the cluster (implies --es.indices).
      --es.snapshots            Export stats for the cluster snapshots.
      --es.slm                  Export stats for SLM snapshots.
      --es.data_stream          Export stas for Data Streams.
      --es.clusterinfo.interval=5m  
                                Cluster info update interval for the cluster label
      --es.ca=""                Path to PEM file that contains trusted Certificate Authorities for the Elasticsearch connection.
      --es.client-private-key=""  
                                Path to PEM file that contains the private key for client auth when connecting to Elasticsearch.
      --es.client-cert=""       Path to PEM file that contains the corresponding cert for the private key to connect to Elasticsearch.
      --es.ssl-skip-verify      Skip SSL verification when connecting to Elasticsearch.
      --log.level="info"        Sets the loglevel. Valid levels are debug, info, warn, error
      --log.format="logfmt"     Sets the log format. Valid formats are json and logfmt
      --log.output="stdout"     Sets the log output. Valid outputs are stdout and stderr
      --aws.region=""           Region for AWS elasticsearch
      --version                 Show application version.

3 启动exporter

systemctl start es_exporter.service

systemctl stop es_exporter.service

4 prometheus动态发现

prometheus.yml

  - job_name: 'es monitor'
    scrape_interval: 1m
    file_sd_configs:
     - files:
        - /usr/local/src/prometheus/conf.d/elasticsearch.json

elasticsearch.json

[
  {
    "labels": {
      "desc": "elasticsearch",
      "group": "elasticsearch",
      "host_ip": "172.16.53.117"
    },
    "targets": [
      "172.16.53.117:9114"
    ]
  }
]

5 Grafana dashboard

ID:14191

### 配置 Prometheus 监控 Elasticsearch #### 安装与配置 Prometheus Elasticsearch Exporter 为了使 Prometheus 能够监控 Elasticsearch,需先安装并配置 Prometheus Elasticsearch Exporter。此工具可以用于监控 Elasticsearch 集群的健康状况、查询性能和资源使用情况[^2]。 ```bash # 下载适合操作系统的版本 wget https://github.com/prometheus/elasticsearch_exporter/releases/download/vX.X.X/elasticsearch_exporter-vX.X.X.linux-amd64.tar.gz tar xvzf elasticsearch_exporter*.tar.gz cd elasticsearch_exporter* ./elasticsearch_exporter --web.listen-address=":9108" ``` 上述命令会启动一个 HTTP 服务,默认监听 `9108` 端口,该端口可按实际需求修改。 #### 修改 Exporter 配置文件 根据具体的监控需求,在 exporter.cfg 文件中定义要抓取的数据项,确保只收集必要的关键指标来减少开销。例如: ```yaml es.uri: "http://localhost:9200/" metrics_path: "/_prometheus/metrics" cluster_health: level: cluster indices_stats: include_indices: ["index_name"] nodes_stats: metrics: ["fs", "jvm", "process", "thread_pool", "transport", "http", "breaker"] ``` 以上 YAML 片段展示了如何指定特定索引名称以及所需节点级别的度量标准。 #### 设置 Prometheus 抓取目标 编辑 Prometheus 的配置文件 prometheus.yml 添加新的 job 来指向已部署好的 Elasticsearch Exporter 实例: ```yaml scrape_configs: - job_name: 'elasticsearch' static_configs: - targets: ['localhost:9108'] ``` 这一步骤使得 Prometheus 开始定期从指定地址拉取数据[^1]。 #### 创建告警规则 利用 Prometheus 提供的强大表达式语言 PromQL 设定合理的阈值触发条件,当满足这些条件时即刻发出通知提醒管理员注意潜在问题的存在。比如检测某个节点是否离线或响应时间超过预期等情形。 ```yaml groups: - name: example rules: - alert: InstanceDown expr: up == 0 for: 5m labels: severity: page annotations: summary: "Instance {{ $labels.instance }} down" ``` 这段配置表示如果某实例连续五分钟未能成功上报状态,则触发名为 “InstanceDown”的警告,并附带简短描述信息给相关人员知晓。 通过遵循上述最佳实践方法论,能够有效地实现对 Elasticsearch 集群的有效监督,及时发现并解决可能出现的各种异常现象,从而保障整个搜索服务平台稳定可靠运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值