--- title: "使用 INFINI Gateway 保护 Elasticsearch 集群之限制请求至协调节点" date: 2025-02-15 lastmod: 2025-02-15 description: "本文介绍如何通过 INFINI Gateway 限制请求仅转发至 Elasticsearch 集群的协调节点,提升集群稳定性,同时支持 Opensearch 和 Easysearch,提供节点角色过滤配置示例与测试方法。" tags: ["Easysearch", "Gateway"] summary: "本文将探讨如何使用 INFINI Gateway 限制仅向 Elasticsearch 集群的协调节点转发请求。此方法同样适用于 Opensearch 和 INFINI Easysearch 。 Elasticsearch 是天然分布式的系统,任何节点都能接收请求并进行处理。一个稍具规模,经过规划的 Elasticsearch 集群,会有不同角色的专属节点,比如专属的 master 节点、data 节点、协调节点等。 为了保障集群稳定,一般建议将查询发送到集群的协调节点,避免将请求发送 master 节点。使用 INFINI Gateway 可轻松做到将所有请求轮询发送到集群的所有协调节点。 Elasticsearch 资源 # INFINI Gateway 将 Elasticsearch 集群作为一种后端资源,可以定义多个 Elasticsearch 集群,支持不同的集群版本。并且可以通过参数 discovery 开启集群的节点拓扑自动发现和更新。 ##elasticsearch servers elasticsearch: - name: prod enabled: true endpoints: - $[[env.PROD_ES_ENDPOINT]] discovery: enabled: true refresh: enabled: true interval: 30s basic_auth: username: $[[env.PROD_ES_USER]] password: $[[env.PROD_ES_PASS]] traffic_control.max_bytes_per_node: 1010485760 metadata_cache_enabled: false # Whether to cache the cluster info in memory cache - name: logging-server enabled: true endpoints: - $[[env." --- 本文将探讨如何使用 INFINI Gateway 限制仅向 Elasticsearch 集群的协调节点转发请求。此方法同样适用于 Opensearch 和 INFINI Easysearch 。 Elasticsearch 是天然分布式的系统,任何节点都能接收请求并进行处理。一个稍具规模,经过规划的 Elasticsearch 集群,会有不同角色的专属节点,比如专属的 master 节点、data 节点、协调节点等。 为了保障集群稳定,一般建议将查询发送到集群的协调节点,避免将请求发送 master 节点。使用 INFINI Gateway 可轻松做到将所有请求轮询发送到集群的所有协调节点。 ## Elasticsearch 资源 INFINI Gateway 将 [Elasticsearch](https://infinilabs.cn/docs/latest/gateway/references/elasticsearch/) 集群作为一种后端资源,可以定义多个 Elasticsearch 集群,支持不同的集群版本。并且可以通过参数 discovery 开启集群的节点拓扑自动发现和更新。 ```plain ##elasticsearch servers elasticsearch: - name: prod enabled: true endpoints: - $[[env.PROD_ES_ENDPOINT]] discovery: enabled: true refresh: enabled: true interval: 30s basic_auth: username: $[[env.PROD_ES_USER]] password: $[[env.PROD_ES_PASS]] traffic_control.max_bytes_per_node: 1010485760 metadata_cache_enabled: false # Whether to cache the cluster info in memory cache - name: logging-server enabled: true endpoints: - $[[env.LOGGING_ES_ENDPOINT]] basic_auth: username: $[[env.LOGGING_ES_USER]] password: $[[env.LOGGING_ES_PASS]] discovery: enabled: false ``` 上面的例子定义了一个名为 prod 和一个名为 logging-server 的集群。其中 prod 集群开启了拓扑自动发现和更新,这使得虽然我们 endpoints 里只写了一个地址(变量),但 INFINI Gateway 可以发现集群的所有节点,并定时进行节点探活。这个探活信息可以通过 INFINI Gateway 的 API 获取到。 ```plain curl localhost:2900/elasticsearch/hosts ``` {{% load-img "/img/blog/2025/restricting-requests-to-coordinating-nodes-in-es-with-gateway/1.png" "" %}} 如上所示,INFINI Gateway 检测到 prod 集群有三个节点,并且都是可用的。 其中有 node-3 和 node-4 是专属协调节点: {{% load-img "/img/blog/2025/restricting-requests-to-coordinating-nodes-in-es-with-gateway/2.png" "" %}} ## Elasticsearch 过滤器 INFINI Gateway 通过 Elasticsearch 过滤器将请求转发给后端 Elasticsearch 集群。使用 Elasticsearch 过滤器之前,需要提前定义一个 Elasticsearch 资源。前面我们不仅定义了一个名为 prod 的 Elasticsearch 资源,还开启了拓扑自动发现和更新。 在使用 Elasticsearch 过滤器时也开启刷新,即可访问后端所有节点,且节点上下线也会自动更新。 ```plain - name: default_flow filter: - get_cache: pass_patterns: ["_cat","scroll", "scroll_id","_refresh","_cluster","_ccr","_count","_flush","_ilm","_ingest","_license","_migration","_ml" ,"_rollup","_data_stream","_open", "_close"] - elasticsearch: elasticsearch: prod max_connection_per_node: 1000 refresh: enabled: true interval: 30s ``` 为了达到只转发请求到协调节点的效果,我们开启节点角色过滤功能,将请求发送给特定的节点。完整的 Elasticsearch 过滤器配置如下。 ```plain - name: default_flow filter: - get_cache: pass_patterns: ["_cat","scroll", "scroll_id","_refresh","_cluster","_ccr","_count","_flush","_ilm","_ingest","_license","_migration","_ml" ,"_rollup","_data_stream","_open", "_close"] - elasticsearch: elasticsearch: prod max_connection_per_node: 1000 refresh: enabled: true interval: 20s filter: roles: exclude: - master - data - transform - remote_cluster_client - ml - data_content - data_hot - data_warm - data_cold - ingest ``` 通过排除所有节点角色,我们就限制了 INFINI Gateway 只能将请求转发到专属协调节点(无任何角色)。此外 INFINI Gateway 还支持按照节点的 IP、标签等来进行过滤,详情参考[官方文档](https://infinilabs.cn/docs/latest/gateway/references/filters/elasticsearch/)。 ## 测试 我们使用 _cat/nodes API 连续访问 INFINI Gateway 几次,看看后端服务的节点是否是专属协调节点。 ```plain curl localhost:8000/_cat/nodes ``` {{% load-img "/img/blog/2025/restricting-requests-to-coordinating-nodes-in-es-with-gateway/3.png" "" %}} 通过 [INFINI Console](https://docs.infinilabs.com/console/main/zh/) 查看访问记录,可以观察到请求被轮流转发到了后端专属协调节点:节点-3 (192.168.56.102:9202) 、节点-4 (192.168.56.102:9203) 。 有任何问题,欢迎加我微信沟通。 {{% load-img "/img/blog/banner/about_yangf.png" "" %}}