如何监控容器或K8s中的OpenSearch

概述

当前 OpenSearch 使用的越来越多, 但是 OpenSearch 生态还不尽完善.

针对如下情况:

  • 监控容器化或运行在 K8s 中的 OpenSearch

我查了下, 官方还没有提供完备的方案.

这里如何监控 K8s 中的 OpenSearch, 包括安装 exporter 插件、采集、展示全环节。

OpenSearch 简介

  • OpenSearch 是一款开源的分布式搜索引擎(从 ElasticSearch 特定版本分叉而来),可以执行快速、可扩展的全文搜索、应用程序和基础设施监控、安全和事件信息管理、运营健康跟踪等用例。
  • OpenSearch 具有多种功能和插件,可以帮助索引、保护、监控和分析数据。
  • OpenSearch 包含一个演示配置,以便您可以快速启动和运行,但在生产环境中使用 OpenSearch 之前,您必须使用自己的证书、身份验证方法、用户和密码手动配置安全插件。
  • OpenSearch 由 AWS 支持,所有组件均可在 GitHub 上获得 Apache 许可证版本 2.0。

Prometheus Exporter Plugin for OpenSearch 简介

  • Prometheus Exporter 插件用于将 OpenSearch 指标暴露为 Prometheus 格式。
  • 插件版本必须与 OpenSearch 版本完全匹配,因此需要保持 prometheus-exporter-plugin-for-opensearch 版本与 OpenSearch 版本同步。
  • 可以通过在每个要由 Prometheus 抓取的 OpenSearch 节点上安装插件来安装插件。
  • 可以通过在 config/opensearch.yml 中配置静态设置和动态设置来配置插件。
  • 指标可以直接在 http(s)://<opensearch-host>:9200/_prometheus/metrics 获得。

📚️相关参考资料

本文会使用到 2 个资源:

具体实现

两种方案:

  1. 自己制作包含 prometheus-exporter 插件的镜像
  2. 通过 OpenSearch Helm Chart 安装prometheus-exporter 插件

(方案一)制作包含 prometheus-exporter 插件的镜像并使用

📝Notes:

这里以 opensearch:2.12 版本为例

Dockerfile 内容如下:

FROM opensearchproject/opensearch:2.12.0
LABEL maintainer="cuikaidong@foxmail.com"
ARG EXPORTER_PLUGIN_URL="https://github.com/Aiven-Open/prometheus-exporter-plugin-for-opensearch/releases/download/2.12.0.0/prometheus-exporter-2.12.0.0.zip"
RUN opensearch-plugin install -b ${EXPORTER_PLUGIN_URL}

📝Notes

如果 docker build 过程下载超时, 可以将对应EXPORTER_PLUGIN_URL行替换为相关代理的 URL(这里不详述).
或者, 下载后, 通过 COPY 复制进去后再执行:
opensearch-plugin install -b file:///path/to/prometheus-exporter-2.12.0.0.zip

构建并推送镜像:

docker build -t xxxxx/opensearch:2.12.0-prometheus-exporter -f ./Dockerfile
docker push xxxx/opensearch:2.12.0-prometheus-exporter

📝Notes

您可以通过 CICD Pipeline, 随着 OpenSearch 和 prometheus-exporter-plugin-for-opensearch 的更新, 自动构建新的镜像.
我相信, 随着 OpenSearch 生态的完善, 应该会有已经包含 exporter 的 OpenSearch 镜像.

对于容器化或 K8s 运行的 OpenSearch, 只需要将镜像改为构建后的, 带 prometheus-exporter 的镜像即可.

如:

原来是:

image: opensearchproject/opensearch:2.12.0

修改为:

image: xxxx/opensearch:2.12.0-prometheus-exporter

(方案二)使用 OpenSearch Helm Chart

如果你是在 K8s 中运行 OpenSearch, 也可以考虑使用 OpenSearch 的 Helm Chart, 它包含了安装第三方插件的功能, 具体 values.yaml 如下:

## Enable to add 3rd Party / Custom plugins not offered in the default OpenSearch image.
plugins:
  enabled: true
  installList:
    - https://github.com/Aiven-Open/prometheus-exporter-plugin-for-opensearch/releases/download/2.12.0.0/prometheus-exporter-2.12.0.0.zip

📚️参考文档:

OpenSearch Helm Chart

修改 pometheus-exporter 的配置

另外, 可以按需修改prometheus-exporter 的配置, 详细配置说明见:

示例配置如下:

config/opensearch.yml, 追加如下内容:

plugins.security.disabled: true
prometheus.indices_filter.selected_indices: "log-*,*log,*log*,log*-test"
prometheus.indices_filter.selected_option: "STRICT_EXPAND_OPEN_FORBID_CLOSED"

📝声明

plugins.security.disabled: true 可选项, 允许通过 http 协议访问插件 url. 生产不建议使用. 建议只在快速验证时采用

prometheus.indices_filter.selected_indices 仅供参考. 请按需调整.

prometheus.indices_filter.selected_option 使用默认配置. 请阅读细节后按需调整.

修改完配置后, 重启容器正常生效.

验证插件已启用

指标可直接在以下位置获取:

http(s)://opensearch-host:9200/_prometheus/metrics

作为示例结果,你将得到如下内容:

# HELP opensearch_process_mem_total_virtual_bytes Memory used by ES process
# TYPE opensearch_process_mem_total_virtual_bytes gauge
opensearch_process_mem_total_virtual_bytes{cluster="develop",node="develop01",} 3.626733568E9
# HELP opensearch_indices_indexing_is_throttled_bool Is indexing throttling ?
# TYPE opensearch_indices_indexing_is_throttled_bool gauge
opensearch_indices_indexing_is_throttled_bool{cluster="develop",node="develop01",} 0.0
# HELP opensearch_jvm_gc_collection_time_seconds Time spent for GC collections
# TYPE opensearch_jvm_gc_collection_time_seconds counter
opensearch_jvm_gc_collection_time_seconds{cluster="develop",node="develop01",gc="old",} 0.0
opensearch_jvm_gc_collection_time_seconds{cluster="develop",node="develop01",gc="young",} 0.0
# HELP opensearch_indices_requestcache_memory_size_bytes Memory used for request cache
# TYPE opensearch_indices_requestcache_memory_size_bytes gauge
opensearch_indices_requestcache_memory_size_bytes{cluster="develop",node="develop01",} 0.0
# HELP opensearch_indices_search_open_contexts_number Number of search open contexts
# TYPE opensearch_indices_search_open_contexts_number gauge
opensearch_indices_search_open_contexts_number{cluster="develop",node="develop01",} 0.0
# HELP opensearch_jvm_mem_nonheap_used_bytes Memory used apart from heap
# TYPE opensearch_jvm_mem_nonheap_used_bytes gauge
opensearch_jvm_mem_nonheap_used_bytes{cluster="develop",node="develop01",} 5.5302736E7
...

使用 Prometheus 采集指标

(仅作为参考示例, 请按需调整), 在 Prometheus 的 scrape 下面, 追加如下内容:

    - job_name: opensearch
      metrics_path: /_prometheus/metrics
      relabel_configs:
        - replacement: '<your-instance-name>'
          target_label: node
      static_configs:
        - targets: ['<your-host-name>:9200']

配置 Prometheus Rules 和 Alerts

这里随便举一个简单例子, 现在使用 OpenSearch 的, 之前应该有完备的 ES 相关的 rules 和 alerts. 略作修改即可.

alert: OpenSearchYellowCluster
for: 5m
annotations:
  summary: At least one of the clusters is reporting a yellow status.
  description: '{{$labels.cluster}} health status is yellow over the last 5 minutes'
  runbook_url: ''
labels:
  severity: warning
  '': ''
expr: |
  opensearch_cluster_status == 1

使用 Grafana 查看

可以使用如下 Grafana Dashboard 进行查看:

效果如下:

OpenSearch Dashboard

更多 OpenSearch Dashboard 可以在 https://grafana.com/grafana/dashboards/ 中搜索关键词 "OpenSearch".

总结

如何监控容器或 K8s 中的 OpenSearch?

  1. 先安装 OpenSearch Prometheus Exporter 插件, 有 2 种办法:
    1. 自己制作包含 OpenSearch Prometheus Exporter 插件的镜像
    2. 使用 OpenSearch Helm Chart 安装
  2. 配置 Prometheus scrape config
  3. 配置 Prometheus Rules 和 Alerts
  4. 使用 Grafana 查看

以上.

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 175,490评论 5 419
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 74,060评论 2 335
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 124,407评论 0 291
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 47,741评论 0 248
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 56,543评论 3 329
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 43,040评论 1 246
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 34,107评论 3 358
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 32,646评论 0 229
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 36,694评论 1 271
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 32,398评论 2 279
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 33,987评论 1 288
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 30,097评论 3 285
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 35,298评论 3 282
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 27,278评论 0 14
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 28,413评论 1 232
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 38,397评论 2 309
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 38,099评论 2 314

推荐阅读更多精彩内容