如何配置 Elasticsearch ?

Viewed 4

配置 Elasticsearch

  • 配置文件位置
  • 配置文件格式
  • 环境变量替换
  • 集群和节点设置类型
1 Answers

配置 Elasticsearch

Elasticsearch 附带了良好的默认设置,几乎不需要配置。大多数设置都可以在运行的集群上使用集群更新设置 API 进行更改。

配置文件应包含特定于节点的设置(例如 node.name 和路径),或节点加入集群所需的设置,例如 cluster.namenetwork.host

配置文件位置

Elasticsearch 有三个配置文件:

  • elasticsearch.yml 用于配置 Elasticsearch
  • jvm.options 用于配置 Elasticsearch JVM 设置
  • log4j2.properties 用于配置 Elasticsearch 日志记录

这些文件位于 config 目录中,其默认位置取决于安装是来自存档分发版(tar.gz 或 zip)还是软件包分发版(Debian 或 RPM 软件包)。

对于存档分发版,config 目录位置默认为 $ES_HOME/config。可以通过 ES_PATH_CONF 环境变量更改 config 目录的位置,如下所示:

ES_PATH_CONF=/path/to/my/config ./bin/elasticsearch

或者,您可以通过命令行或 shell 配置文件导出 ES_PATH_CONF 环境变量。

对于软件包分发版,config 目录位置默认为 /etc/elasticsearchconfig 目录的位置也可以通过 ES_PATH_CONF 环境变量更改,但请注意,在 shell 中设置此变量是不够的。相反,此变量来源于 /etc/default/elasticsearch(对于 Debian 软件包)和 /etc/sysconfig/elasticsearch(对于 RPM 软件包)。您需要相应地编辑这些文件之一中的 ES_PATH_CONF=/etc/elasticsearch 条目以更改 config 目录位置。

配置文件格式

配置格式为 YAML。以下是更改数据和日志目录路径的示例:

path:
    data: /var/lib/elasticsearch
    logs: /var/log/elasticsearch

设置也可以扁平化,如下所示:

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch

在 YAML 中,您可以将非标量值格式化为序列:

discovery.seed_hosts:
   - 192.168.1.10:9300
   - 192.168.1.11
   - seeds.mydomain.com

虽然不太常见,但您也可以将非标量值格式化为数组:

discovery.seed_hosts: ["192.168.1.10:9300", "192.168.1.11", "seeds.mydomain.com"]

环境变量替换

配置文件中使用 ${...} 符号引用的环境变量将替换为环境变量的值。例如:

node.name:    ${HOSTNAME}
network.host: ${ES_NETWORK_HOST}

环境变量的值必须是简单的字符串。使用逗号分隔的字符串来提供 Elasticsearch 将解析为列表的值。例如,Elasticsearch 会将以下字符串拆分为 ${HOSTNAME} 环境变量的值列表:

export HOSTNAME="host1,host2"

集群和节点设置类型

集群和节点设置可以根据其配置方式进行分类:

动态

您可以使用集群更新设置 API 在运行的集群上配置和更新动态设置。您也可以使用 elasticsearch.yml 在未启动或关闭的节点上本地配置动态设置。

使用集群更新设置 API 进行的更新可以是持久性的,即在集群重新启动后仍然有效,也可以是瞬态的,即在集群重新启动后重置。您还可以通过使用 API 为瞬态或持久性设置分配 null 值来重置它们。

如果您使用多种方法配置相同的设置,Elasticsearch 将按以下优先顺序应用设置:

  1. 瞬态设置
  2. 持久性设置
  3. elasticsearch.yml 设置
  4. 默认设置值

例如,您可以应用瞬态设置来覆盖持久性设置或 elasticsearch.yml 设置。但是,更改 elasticsearch.yml 设置不会覆盖已定义的瞬态或持久性设置。

如果您使用 Elasticsearch Service,请使用用户设置功能配置所有集群设置。此方法可让 Elasticsearch Service 自动拒绝可能破坏集群的不安全设置。

如果您在自己的硬件上运行 Elasticsearch,请使用集群更新设置 API 配置动态集群设置。仅将 elasticsearch.yml 用于静态集群设置和节点设置。API 不需要重新启动,并确保所有节点上的设置值相同。

我们不再建议使用瞬态集群设置。请改用持久性集群设置。如果集群变得不稳定,瞬态设置可能会意外清除,从而导致潜在的意外集群配置。请参阅瞬态设置迁移指南。

静态

静态设置只能在未启动或关闭的节点上使用 elasticsearch.yml 进行配置。

必须在集群中的每个相关节点上设置静态设置。

原文:

Configuring Elasticsearch(右键打开原文链接)

Elasticsearch ships with good defaults and requires very little configuration. Most settings can be changed on a running cluster using the Cluster update settings API.

The configuration files should contain settings which are node-specific (such as node.name and paths), or settings which a node requires in order to be able to join a cluster, such as cluster.name and network.host.

Config files location

Elasticsearch has three configuration files:

  • elasticsearch.yml for configuring Elasticsearch
  • jvm.options for configuring Elasticsearch JVM settings
  • log4j2.properties for configuring Elasticsearch logging

These files are located in the config directory, whose default location depends on whether the installation is from an archive distribution (.tar.gz or .zip) or a package distribution (Debian or RPM packages).

For archive distributions, the config directory defaults to $ES_HOME/config. The location can be changed via the ES_PATH_CONF environment variable:

ES_PATH_CONF=/path/to/my/config ./bin/elasticsearch

Alternatively, export the ES_PATH_CONF environment variable in your shell profile.

For package distributions, the config directory defaults to /etc/elasticsearch. The location can also be changed via the ES_PATH_CONF environment variable. However, setting this in your shell is insufficient. This variable is sourced from /etc/default/elasticsearch (Debian) and /etc/sysconfig/elasticsearch (RPM). Edit the ES_PATH_CONF=/etc/elasticsearch entry in the appropriate file.

Config file format

The configuration format is YAML. Here's an example of changing the data and log directory paths:

path:
    data: /var/lib/elasticsearch
    logs: /var/log/elasticsearch

Settings can also be flattened:

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch

In YAML, format non-scalar values as sequences:

discovery.seed_hosts:
   - 192.168.1.10:9300
   - 192.168.1.11
   - seeds.mydomain.com

Less commonly, format non-scalar values as arrays:

discovery.seed_hosts: ["192.168.1.10:9300", "192.168.1.11", "seeds.mydomain.com"]

Environment variable substitution

Environment variables referenced with ${...} within the configuration file are replaced with their values. For example:

node.name:    ${HOSTNAME}
network.host: ${ES_NETWORK_HOST}

Environment variable values must be simple strings. Use a comma-separated string for values that Elasticsearch will parse as a list:

export HOSTNAME="host1,host2"

Cluster and node setting types

Cluster and node settings are categorized by how they are configured:

Dynamic

Configure and update dynamic settings on a running cluster using the cluster update settings API. Configure dynamic settings locally on an unstarted or shut down node using elasticsearch.yml.

API updates can be persistent (across restarts) or transient (reset after restart). Reset transient or persistent settings by assigning them a null value using the API.

When configuring the same setting using multiple methods, Elasticsearch applies the following precedence:

  1. Transient setting
  2. Persistent setting
  3. elasticsearch.yml setting
  4. Default setting value

For example, a transient setting overrides a persistent or elasticsearch.yml setting. However, an elasticsearch.yml change won't override a defined transient or persistent setting.

If using Elasticsearch Service, use user settings to configure all cluster settings. This allows Elasticsearch Service to reject unsafe settings.

If running Elasticsearch on your own hardware, use the cluster update settings API for dynamic cluster settings. Use elasticsearch.yml only for static cluster settings and node settings. The API avoids restarts and ensures consistent setting values across nodes.

Transient cluster settings are no longer recommended. Use persistent settings instead. Transient settings can clear unexpectedly if a cluster becomes unstable, leading to an undesirable configuration. See the Transient settings migration guide.

Static

Static settings are configured only on an unstarted or shut down node using elasticsearch.yml. Static settings must be set on every relevant node.

Related Questions

Powered by Answer - the open-source software that powers Q&A communities.
Made with love © 2025 亘古问答.