2. Set up Elasticsearch

本贴最后更新于 2328 天前,其中的信息可能已经事过境迁

设置 Elasticsearch

本节包括有关如何设置 Elasticsearch 并使其运行的信息,包括 :

  • 下载
  • 安装
  • 启动
  • 配置

支持的平台

正式支持的操作系统和 JVM 的矩阵可以在这里查阅 : Support Matrix(支持矩阵)。 Elasticsearch 在列出的平台上测试,但它也可能在其他平台上工作。

Java(JVM) 版本

Elasticsearch 是使用 Java 构建的,并且至少需要 Java 8 才能运行。 只支持 Oracle 的 Java 和 OpenJDK。在所有 Elasticsearch 节点和客户端上应使用相同的 JVM 版本。

我们建议安装 Java 版本为 1.8.0_73 或更高版本。 如果使用已知的有问题的 Java 版本,Elasticsearch 将拒绝启动。

Elasticsearch 将使用的 Java 版本可以通过设置 JAVA_HOME 环境变量进行配置。

Installing Elasticsearch(安装)

Elasticsearch 提供以下安装包的格式 :

格式 说明
zip/tar.gz zip 和 tar.gz 的安装包适合在任何系统上安装,是针对 Elasticsearch 入门指南 的最佳选择。Install Elasticsearch with.zip or .tar.gz or Install Elasticsearch on Windows
deb deb 包适用于 Debian,Ubuntu 和其他基于 Debian 的系统。Debian 软件包可以从 Elasticsearch 网站或我们的 Debian 仓库下载。
rpm rpm 包适合在 Red Hat,Centos,SLES,OpenSuSE 和其他基于 RPM 的系统上安装。 RPM 可以从 Elasticsearch 网站或从我们的 RPM 仓库下载。Install Elasticsearch with RPM
docker 使用镜像可将 Elasticsearch 在 Docker 容器运行。它预装了 X-Pack,可以从 Elastic Docker Registry 下载。Install Elasticsearch with Docker

Configuration Management Tools(配置管理工具)

我们还提供以下配置管理工具来帮助大型部署 :

工具 地址
Puppet puppet-elasticsearch
Chef cookbook-elasticsearch
Ansible ansible-elasticsearch

zip 或 tar.gz 安装

zip 或 tar.gz 安装

Elasticsearch 提供了.zip 与作为.tar.gz 包。这些包可用于任何系统上安装 Elasticsearch 并且使用简单的 Elasticsearch 的包格式。

Elasticsearch 的最新稳定版本可以在 Elasticsearch 页面下载。其它版本可以在上 之前的下载也面找到。

Elasticsearch 需要 Java 8 或更高版本。使用 Oracle 官方发布或作为开放源代码的分布的 OpenJDK。

下载.zip 安装包

在.zip 为 Elasticsearch V5.0.1 压缩文件可以下载并安装如下:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.zip
sha1sum elasticsearch-6.0.0.zip  1
unzip elasticsearch-6.0.0.zip
cd elasticsearch-6.0.0/  2
  1. 通过比较产生的 SHA sha1sum 或 shasum 与 published SHA.
  2. 该目录被称为 $ES_HOME。

下载.tar.gz 安装包

在.tar.gz 为 Elasticsearch V5.0.1 压缩文件可以下载并安装如下:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.1.tar.gz
sha1sum elasticsearch-5.0.1.tar.gz  1
tar -xzf elasticsearch-5.0.1.tar.gz 2
cd elasticsearch-5.0.1 / 
  1. Compare the SHA produced by sha1sum or shasum with the published SHA.
  2. This directory is known as $ES_HOME.

在命令行中运行 Elasticsearch

Elasticsearch 可以从命令行启动,如下所示:

./bin/elasticsearch

默认情况下,Elasticsearch 在前台运行,其打印日志到标准输出(stdout),并且可以通过按下停止 Ctrl-C。

检查 Elasticsearch 运行

您可以测试你的 Elasticsearch 节点通过发送一个 HTTP 请求到端口上运行 9200 的 localhost:

GET /

返回的消息应该是这样的:

{
  "name" : "Cp8oag6",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "AT69_T_DTp-1qgIJlatQqA",
  "version" : {
    "number" : "5.0.1",
    "build_hash" : "f27399d",
    "build_date" : "2016-03-30T09:51:41.449Z",
    "build_snapshot" : false,
    "lucene_version" : "6.2.1"
  },
  "tagline" : "You Know, for Search"
}

要退出打印,在命令行输入-q 或 --quiet 选项在命令行上

作为守护程序运行

要运行 Elasticsearch 作为一个守护进程,指定-d 在命令行上,并使用记录进程 ID 文件中的-p 选项:

./bin/elasticsearch -d -p PID

日志信息可以在找到 $ES_HOME/logs/的目录。

要关闭 Elasticsearch,杀记录的过程中 ID pid 文件:

kill `cat pid`

在提供的启动脚本 RPM 和 Debian 的 包带启动和停止 Elasticsearch 过程。

在命令行配置 Elasticsearch

Elasticsearch 加载从它的配置 $ES_HOME/config/elasticsearch.yml 默认文件。这个配置文件的格式说明配置 Elasticsearch。

可以在配置文件中指定的任何设置,也可以在命令行上指定,使用-E 的语法如下:

./bin/elasticsearch -d -Ecluster.name = my_cluster -Enode.name = node_1

通常情况下,任何集群范围的设置(例如 cluster.name)应该被加入到 elasticsearch.yml 配置文件,而任何特定于节点的设置,例如 node.name 可以在命令行上被指定。

目录结构.zip 和.tar.gz 的文档

在.zip 和.tar.gz 包是完全独立的。所有的文件和目录,默认情况下,包含在 $ES_HOME -解压缩存档时创建的目录。

这是非常方便,因为你没有创建任何目录开始使用 Elasticsearch 和卸载 Elasticsearch 是去掉了容易 $ES_HOME 目录。不过,最好是改变 config 目录,数据目录和日志目录的默认位置,这样你就不会删除以后重要的数据。

类型 描述 默认位置 设置
主目录 Elasticsearch 主目录或 $ES_HOME 目录由解包归档创建
执行文件 二进制脚本包括 elasticsearch 启动一个节点,并 elasticsearch-plugin 安装插件 $ES_HOME/bin
配置文件 配置文件,包括 elasticsearch.yml $ES_HOME/config path.conf
数据 节点上分配的各索引/碎片的数据文件的位置。可以容纳多个位置。 $ES_HOME/data path.data
日志 日志文件的位置。 $ES_HOME/logs path.logs
插件 插件文件的位置。每一个插件将被包含在一个子目录。 $ES_HOME/plugins path.conf 配置文件
仓库 共享文件系统存储库位置。可以容纳多个位置。文件系统存储库可以放在这里指定的任意目录中的任何子目录。 未配置 path.repo
脚本 脚本文件位置。 $ES_HOME/config path.conf 配置文件

Debian 软件包安装 Elasticsearch

Debian 软件包安装 Elasticsearch

Debian 的软件包 Elasticsearch 可以从我们的网站下载 或从我们的 APT 仓库。它可以用于任何基于 Debian 的系统,如 Debian 和 Ubuntu 上安装 Elasticsearch。

Elasticsearch 的最新稳定版本可以在上找到 下载 Elasticsearch 页面。其它版本可以在上找到过去的版本页面。

Elasticsearch 需要 Java 8 或更高版本。使用 Oracle 官方分发 或作为开放源代码的分布,OpenJDK 的。

导入 Elasticsearch PGP 密钥

我们签署所有我们与 Elasticsearch 签名密钥包(PGP 密钥 D88E42B4,可从 https://pgp.mit.edu)与指纹识别:

4609 5ACC 8548 582C 1A26 99A9 D27D 666C D88E 42B4

下载并安装该公用签名密钥:

wget的- QO - HTTPS :// 文物。弹性。合/ GPG - KEY - elasticsearch | sudo易于- 键添加-

从 APT 库中安装

您可能需要安装 apt-transport-https 程序之前,Debian 软件包:

sudo apt-get install apt-transport-https

库定义保存到 /etc/apt/sources.list.d/elastic-5.x.list:

echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list

不要使用 add-apt-repository,因为它会增加一个 deb-src 条目,但是我们不提供源代码包。如果您已经添加了 deb-src 条目,你会看到如下的错误:
无法找到预期的条目发行文件的主要/源极/源“
(错误的 sources.list 条目或格式错误的文件)
删除 deb-src 从入门/etc/apt/sources.list 文件,按预期的方式安装应该工作。

您可以通过安装 Elasticsearch Debian 软件包:

sudo apt-get update && sudo apt-get install elasticsearch

如果同一 Elasticsearch 版本库中两个条目,你会看到在这样的错误 apt-get update:
重复的 sources.list 进入 https://artifacts.elastic.co/packages/5.x/apt/ ...`
检查/etc/apt/sources.list.d/elasticsearch-5.x.list 的重复条目,或找到文件之间的重复条目/etc/apt/sources.list.d/和/etc/apt/sources.list 文件。

下载并安装 Debian 软件包手动

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.0.1.deb
sha1sum elasticsearch-5.0.1.deb
sudo dpkg -i elasticsearch-5.0.1.deb

配置 Elasticsearch

Elasticsearch 本身具有适宜的默认值,且需要的配置很少。 使用 Cluster Update Settings(更新群集设置)API 可以更改运行中集群里的大多数设置。

配置文件应包含特定于节点的设置(例如 node.name 和路径)或节点为了能够加入群集而需要的设置,例如 cluster.name 和 network.host。

配置文件位置

Elasticsearch 有两个配置文件:

  • 用于配置 Elasticsearch 的 elasticsearch.yml;
  • log4j2.properties 用于配置 Elasticsearch 日志记录。

这些文件位于 config 目录中,其位置默认为 $ES_HOME/config/。Debian 和 RPM 软件包将 config 目录位置设置为/etc/elasticsearch/。

可以使用 path.conf 设置更改 config 目录的位置,如下所示:

./bin/elasticsearch -Epath.conf=/path/to/my/config/

配置文件格式

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

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

设置也可以展平开如下:

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

环境变量替换

配置文件中可以使用 $ {...}符号引用环境变量的值,例如:

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

设置的提示

对于您不希望在配置文件中存储的设置,您可以使用值{prompt.text}或{prompt.secret}并在控制台启动 Elasticsearch。{prompt.secret} 禁用了echo,输入的值不会在终端显示; {prompt.text}将允许您在键入时看到值。例如:

node:
  name: ${prompt.text}

当启动 Elasticsearch 时,将提示您进行输入,如下所示:

Enter value for [node.name]:

如果在设置中使用了{prompt.text}或{prompt.secret},Elasticsearch 进程将不能作为服务启动或不能后台运行。

设置默认设置

可以在命令行中设置新的默认设置。这将设置一个默认值,除非在配置文件中指定另一个值。

例如,如果 Elasticsearch 按如下方式启动:

./bin/elasticsearch -Edefault.node.name=My_Node

node.name 的值将为 My_Node,但可以在命令行中使用 es.node.name 或在包含 node.name 的配置文件中覆盖。

日志配置

Elasticsearch 使用 Log4j 2 进行日志记录。 Log4j 2 可以使用 log4j2.properties 文件进行配置。 Elasticsearch 公开了一个属性 ${sys:es.logs} ,可以在配置文件中引用该属性以确定日志文件的位置; 这将在运行时解析为 Elasticsearch 日志文件的前缀。

例如,如果您的日志目录(path.logs)是/var/log/elasticsearch 并且您的集群名为 production,那么 $ {sys:es.logs}将解析为/var/log/elasticsearch/production。

appender.rolling.type = RollingFile   ①
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs}.log   ②
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %.10000m%n
appender.rolling.filePattern = ${sys:es.logs}-%d{yyyy-MM-dd}.log   ③
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy   ④
appender.rolling.policies.time.interval = 1    ⑤
appender.rolling.policies.time.modulate = true    ⑥
  1. 配置 RollingFile appender
  2. 记录到/var/log/elasticsearch/production.log
  3. 将日志记录到/var/log/elasticsearch/production-yyyy-MM-dd.log
  4. 使用基于时间的滚动策略
  5. 每日滚动日志
  6. 在日边界对齐卷(而不是每二十四小时滚动一次)

Log4j 的配置解析会被任何无关的空格符所干扰;如果你复制、粘贴当前页的 Log4j 配置或自己输入 Log4j 配置时是要确认头尾的空格符号是否修正。

如果将.gz 或.zip 附加到 appender.rolling.filePattern 后,那么日志将在滚动时压缩。

如果要保留特定的时间段内的日志文件,则可以使用带有删除操作的滚动策略。

appender.rolling.strategy.type = DefaultRolloverStrategy ①
appender.rolling.strategy.action.type = Delete ②
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path} ③
appender.rolling.strategy.action.condition.type = IfLastModified ④
appender.rolling.strategy.action.condition.age = 7D ⑤
appender.rolling.strategy.action.PathConditions.type = IfFileName ⑥
appender.rolling.strategy.action.PathConditions.glob = ${sys:es.logs.cluster_name}-* ⑦
  1. 配置 DefaultRolloverStrategy(默认滚动策略)
  2. 配置 Delete(删除)操作处理策略
  3. Elasticsearch 日志路径
  4. 处理策略的条件
  5. 保留 7 日日志
  6. 仅删除与指定通配符匹配的文件名或目录中超出七日以上的文件
  7. 删除日志路径下与 ${sys:es.logs.cluster_name}-*通配符匹配的文件;但这只是仅仅删除匹配到的 Elasticsearch 日志,它不能同时删除 deprecation logs 和 slow logs。

可以加载多个配置文件(在这种情况下,它们将被合并),只要它们命名为 log4j2.properties 并将 Elasticsearch config 目录作为祖先; 这对于暴露其他日志记录器的插件很有用。 日志部分包含 java 包及其对应的日志级别。 追加器部分包含日志的目标。 有关如何自定义日志记录和所有支持的追加器的详细信息可以在 Log4j 文档中找到。

弃用日志记录

除了常规日志记录之外,Elasticsearch 还允许您启用已弃用操作的日志记录。 例如,这允许您早期确定,如果您需要在将来迁移某些功能。 默认情况下,将在 WARN 级别启用弃用日志记录,这是将发出所有弃用日志消息的级别。

logger.deprecation.level = warn

这将在日志目录中创建每日滚动 deprecation 日志文件。 定期检查此文件,特别是当您打算升级到新的主版本。

默认的日志配置已为弃用日志设置了滚动策略,以在 1GB 之后滚动和压缩,并且最多保留五个日志文件(四个滚动日志和活动日志)。

您可以通过将 deprecation 日志级别设置为 error,在 config / log4j2.properties 文件中禁用它。

安全配置

有一些设置是敏感的,通过文件系统的权限来保护是不足够的。基于这种场景,elasticsearch 提供了一个 keystore,可以通过密码保护。elasticsearch-keystore 就是用来管理与设置 keystore 的工具。

所有的指令都必须使用运行 elasticsearch 的用户来执行。

只有部分设置被设计为从 keystore 读取。请一一参阅设置项文档,以查看其是否为 keystore 支持的那部分设置。

创建 keystore

使用 create 指令来创建 elasticsearch.keystore:

bin/elasticsearch-keystore create
elasticsearch.keystore文件将被创建在elasticsearch.yml文件的旁边。

列出 keystore 的配置

使用 list 指令来列出 elasticsearch.keystore 的设置:

bin/elasticsearch-keystore list

添加一个 String 设置

一些敏感的字符串,像云平台插件的一个认真参数,可以通过 add 指令来添加:

bin/elasticsearch-keystore add the.setting.name.to.set

工具将提示这个值得设置。如果要通过控制台展示,使用--stdin 参数:

cat /file/containing/setting/value | bin/elasticsearch-keystore add --stdin the.setting.name.to.set

删除设置
使
用 remove 指令来从 keystore 中删除配置:

bin/elasticsearch-keystore remove the.setting.name.to.remove

Logging configurationedit

Elasticsearch uses Log4j 2 for logging. Log4j 2 can be configured using the log4j2.properties file. Elasticsearch exposes three properties, {sys:es.logs.base_path}, {sys:es.logs.cluster_name}, and {sys:es.logs.node_name} (if the node name is explicitly set via node.name) that can be referenced in the configuration file to determine the location of the log files. The property {sys:es.logs.base_path} will resolve to the log directory, {sys:es.logs.cluster_name} will resolve to the cluster name (used as the prefix of log filenames in the default configuration), and {sys:es.logs.node_name} will resolve to the node name (if the node name is explicitly set).

For example, if your log directory (path.logs) is /var/log/elasticsearch and your cluster is named production then {sys:es.logs.base_path} will resolve to /var/log/elasticsearch and {sys:es.logs.base_path}{sys:file.separator}{sys:es.logs.cluster_name}.log will resolve to /var/log/elasticsearch/production.log.

appender.rolling.type = RollingFile 
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log 
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz 
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy 
appender.rolling.policies.time.interval = 1 
appender.rolling.policies.time.modulate = true 
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy 
appender.rolling.policies.size.size = 256MB 
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
appender.rolling.strategy.action.type = Delete 
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName 
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* 
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize 
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB 
  1. Configure the RollingFile appender
  2. Log to /var/log/elasticsearch/production.log
  3. Roll logs to /var/log/elasticsearch/production-yyyy-MM-dd-i.log; logs will be compressed on each roll and i will be incremented
  4. Use a time-based roll policy
  5. Roll logs on a daily basis
  6. Align rolls on the day boundary (as opposed to rolling every twenty-four hours)
  7. Using a size-based roll policy
  8. Roll logs after 256 MB
  9. Use a delete action when rolling logs
  10. Only delete logs matching a file pattern
  11. The pattern is to only delete the main logs
  12. Only delete if we have accumulated too many compressed logs
  13. The size condition on the compressed logs is 2 GB


Log4j’s configuration parsing gets confused by any extraneous whitespace; if you copy and paste any Log4j settings on this page, or enter any Log4j configuration in general, be sure to trim any leading and trailing whitespace.

Note than you can replace .gz by .zip in appender.rolling.filePattern to compress the rolled logs using the zip format. If you remove the .gz extension then logs will not be compressed as they are rolled.

If you want to retain log files for a specified period of time, you can use a rollover strategy with a delete action.

appender.rolling.strategy.type = DefaultRolloverStrategy 
appender.rolling.strategy.action.type = Delete 
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path} 
appender.rolling.strategy.action.condition.type = IfFileName 
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* 
appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified 
appender.rolling.strategy.action.condition.nested_condition.age = 7D 
  1. Configure the DefaultRolloverStrategy
  2. Configure the Delete action for handling rollovers
  3. The base path to the Elasticsearch logs
  4. The condition to apply when handling rollovers
  5. Delete files from the base path matching the glob ${sys:es.logs.cluster_name}-*; this is the glob that log files are rolled to; this is needed to only delete the rolled Elasticsearch logs but not also delete the deprecation and slow logs
  6. A nested condition to apply to files matching the glob
  7. Retain logs for seven days

Multiple configuration files can be loaded (in which case they will get merged) as long as they are named log4j2.properties and have the Elasticsearch config directory as an ancestor; this is useful for plugins that expose additional loggers. The logger section contains the java packages and their corresponding log level. The appender section contains the destinations for the logs. Extensive information on how to customize logging and all the supported appenders can be found on the Log4j documentation.

Configuring logging levelsedit

There are four ways to configuring logging levels, each having situations in which they are appropriate to use.

  1. ia the command-line: -E = (e.g., -E logger.org.elasticsearch.transport=trace). This is most appropriate when you are temporarily debugging a problem on a single node (for example, a problem with startup, or during development).
  2. Via elasticsearch.yml: : (e.g., logger.org.elasticsearch.transport: trace). This is most appropriate when you are temporarily debugging a problem but are not starting Elasticsearch via the command-line (e.g., via a service) or you want a logging level adjusted on a more permanent basis.
  3. Via cluster settings:
PUT /_cluster/settings
{
  "transient": {
    "<name of logging hierarchy>": "<level>"
  }
}

For example:

PUT /_cluster/settings
{
  "transient": {
    "logger.org.elasticsearch.transport": "trace"
  }
}

This is most appropriate when you need to dynamically need to adjust a logging level on an actively-running cluster.
4. Via the log4j2.properties:

logger.<unique_identifier>.name = <name of logging hierarchy>
logger.<unique_identifier>.level = <level>

For example:

logger.transport.name = org.elasticsearch.transport
logger.transport.level = trace

This is most appropriate when you need fine-grained control over the logger (for example, you want to send the logger to another file, or manage the logger differently; this is a rare use-case).

Deprecation loggingedit

In addition to regular logging, Elasticsearch allows you to enable logging of deprecated actions. For example this allows you to determine early, if you need to migrate certain functionality in the future. By default, deprecation logging is enabled at the WARN level, the level at which all deprecation log messages will be emitted.

logger.deprecation.level = warn
This will create a daily rolling deprecation log file in your log directory. Check this file regularly, especially when you intend to upgrade to a new major version.

The default logging configuration has set the roll policy for the deprecation logs to roll and compress after 1 GB, and to preserve a maximum of five log files (four rolled logs, and the active log).

You can disable it in the config/log4j2.properties file by setting the deprecation log level to error.

重要 Elasticsearch 配置

虽然 Elasticsearch 仅需要很少的配置,但有许多设置需要手动配置,并且在进入生产之前绝对必须进行配置。

  • path.data 和 path.logs
  • cluster.name
  • node.name
  • bootstrap.memory_lock
  • network.host
  • discovery.zen.ping.unicast.hosts
  • discovery.zen.minimum_master_nodes

path.data 和 path.logs

如果使用.zip 或.tar.gz 归档,则数据和日志目录是 $ES_HOME 的子文件夹。 如果这些重要的文件夹保留在其默认位置,则存在将 Elasticsearch 升级到新版本时被删除的高风险。

在生产使用中,肯定得更改数据和日志文件夹的位置:

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

RPM 和 Debian 发行版已经使用数据和日志的自定义路径。

path.data 设置可以设置为多个路径,在这种情况下,所有路径将用于存储数据(属于单个分片的文件将全部存储在同一数据路径上):

path:
  data:
    - /mnt/elasticsearch_1
    - /mnt/elasticsearch_2
    - /mnt/elasticsearch_3

cluster.name

节点只能在群集与群集中的所有其他节点共享其 cluster.name 时才能加入群集。 默认名称为 elasticsearch,但您应将其更改为描述集群用途的适当名称。

cluster.name: logging-prod

确保不要在不同的环境中重复使用相同的集群名称,否则可能会导致加入错误集群的节点。

node.name

默认情况下,Elasticsearch 将使用随机生成的 uuid 的第一个字符作为节点 id。 请注意,节点 ID 是持久化的,并且在节点重新启动时不会更改,因此默认节点名称也不会更改。

配置一个更有意义的名称是值得的,这是重启节点后也能一直保持的优势:

node.name: ${HOSTNAME}

bootstrap.memory_lock

没有 JVM 被交换到磁盘上这事对于节点的健康来说是至关重要的。一种实现方法是将 bootstrap.memory_lock 设置为 true。
要使此设置生效,需要首先配置其他系统设置。 有关如何正确设置内存锁定的更多详细信息,请参阅启用 bootstrap.memory_lock。

network.host

默认情况下,Elasticsearch 仅仅绑定在本地回路地址——如:127.0.0.1 与[::1]。这在一台服务器上跑一个开发节点是足够的。

事实上,多个节点可以在单个节点上相同的 $ES_HOME 位置一同运行。这可以用于测试 Elasticsearch 形成集群的能力,但这种配置方式不推荐用于生产环境。

为了将其它服务器上的节点形成一个可以相互通讯的集群,你的节点将不能绑定在一个回路地址上。 这里有更多的网路配置,通常你只需要配置 network.host:

network.host: 192.168.1.10

network.host 也可以配置成一些能识别的特殊的值,譬如:local、_site、global,它们可以结合指定:ip4 与 ip6 来使用。更多相信信息请参见:网路配置

一旦你自定义了 network.host 的配置,Elasticsearch 将假设你已经从开发模式转到了生产模式,并将升级系统检测的警告信息为异常信息。更多信息请参见:开发模式 vs 生产模式

discovery.zen.ping.unicast.hosts(单播发现)

开箱即用,无需任何网络配置,Elasticsearch 将绑定到可用的回路地址,并扫描 9300 年到 9305 的端口去连接同一机器上的其他节点,试图连接到相同的服务器上运行的其他节点。它提供了不需要任何配置就能自动组建集群的体验。

当与其它机器上的节点要形成一个集群时,你需要提供一个在线且可访问的节点列表。像如下来配置:

discovery.zen.ping.unicast.hosts:
   - 192.168.1.10:9300
   - 192.168.1.11 #①
   - seeds.mydomain.com #②
  1. 未指定端口时,将使用默认的 transport.profiles.default.port 值,如果此值也为设置则使用 transport.tcp.port
  2. 主机名将被尝试解析成能解析的多个 IP

discovery.zen.minimum_master_nodes

为防止数据丢失,配置 discovery-zen-minimum_master_nodes 将非常重要,他规定了必须至少要有多少个 master 节点才能形成一个集群。

没有此设置时,一个集群在发生网络问题是可能会分裂成多个集群——脑裂——这将导致数据丢失。更多详细信息请参见:通过 minimum_master_nodes 避免脑裂

为避免脑裂,你需要根据 master 节点数来设置法定人数

(master_eligible_nodes / 2) + 1

换句话说,如果你有三个 master 节点,最小的主节点数因被设置为(3/2)+1 或者是 2

discovery.zen.minimum_master_nodes: 2

重要的系统配置

重要的系统配置

理想情况下,Elasticsearch 应该单独在一个服务器上运行,并使用所有可用的资源。为了做到这一点,您需要配置您的操作系统允许用户运行 Elasticsearch 比默认允许访问更多的资源。

以下设置必须在生产使用时配置:

  • 设置 JVM 堆大小
  • 禁用交换
  • 增加文件描述符
  • 确保足够的虚拟内存
  • 确保足够的线程

开发模式 vs 生产模式

默认情况下,Elasticsearch 假设你在开发环境中工作。任何没有设置正确的配置,都会以警告的方式记录在日志文件中,但是你能启动与运行 Elasticsearch 节点。

一旦你后面配置了网络设置如 network.host,Elasticsearch 将会切换到生产模式并将警告升级为异常。这些异常将会阻止 Elasticsearch 节点的启动。这是一个非常重要的安全措施,以确保你不会因为错误的配置丢失数据。

配置系统设置

系统设置

在哪里配置系统设置取决于你使用哪个包安装 Elasticsearch,以及您正在使用的操作系统。

当你使用.zip 或.tar.gz 的安装包是,系统设置可以按如下方式配置:

  • 通过 ulimit 临时限制
  • 通过/etc/security/limits.conf 永久限制

当你使用 RPM 或 Debian 安装包时,更多的系统设置可以通过(系统配置文件](#sysconfig)。但是,使用 systemd 的系统设置系统限制需要在 systemd 配置文件中设置。

ulimit

在 Linux 系统上,可以使用 ulimit 临时改变资源限制。限制通常需要切换到 root 用户下进行设置。例如,设置打开文件的句柄数限制(ulimit -n)为 65536,可以按如下做:

sudo su  #①
ulimit -n 65536 #②
su elasticsearch #③
  1. 切换到 root
  2. 修改最大打开文件句柄数
  3. 切换回启动 Elasticsearch 的 elasticsearch 用户

新的现在将被应用到当前会话。

你可以通过 ulimit -a 查询当前的所有限制参数。

/etc/security/limits.conf

在 Linux 系统,可以通过制定的用户编辑/etc/security/limits.conf 文件来持久化限制设置。若要设置 elasticsearch 用户打开文件句柄数最大值为 65536,可以将如下行添加到 limit.conf 文件中:

elasticsearch  -  nofile  65536

这种变化知会在用户下次打开一个新的会话时生效。

Ubuntu 与 limits.conf Ubuntu 在 init.d 启动时忽略了 limits.conf 文件。要开启 limits.conf 文件,需要编辑/etc/pam.d/su,删除下面行的注释:

# session required pam_limits.so

Sysconfig file

在使用 RPM 或 Debian 系统时,系统设置与环境变量可以通过系统配置文件制定,他们位于:

RPM /etc/sysconfig/elasticsearch

Debian /etc/default/elasticsearch

然而,对于使用 systemd 的系统,系统限制通过 systemd 指定。

systemd file

当使用 RPM 或 Debian 软件包是使用 systemd 的系统,系统限制必须通过 systemd 指定。

systemd 服务文件(/usr/lib/systemd/system/elasticsearch.service)包含的限制将被用作默认值。

如果需要覆盖它,需要添加一个叫/etc/systemd/system/elasticsearch.service.d/elasticsearch.conf 的文件并在文件中指定任意的变化,就像:

[Service]
LimitMEMLOCK=infinity

Once finished, run the following command to reload units:

sudo systemctl daemon-reload

jvm.options

设置 Java 虚拟机参数(包括系统属性和 JVM 参数)的首选方式是通过 jvm.options 文件配置。这个文件在 tar 和 zip 包方式安装时在 config/jvm.options,在 RPM 或 Debian 方式安装时在/etc/elasticsearch/jvm.options。这个文件包含了一个用行风格的 JVM 参数列表,必须要用-开头。你可以添加自定义的 JVM 参数到这个文件或在你的控制系统版本中检查这些配置

另一种方式是通过设置环境变量 ES_JAVA_OPTS 的方式来添加 JAVA 虚拟机参数,例如:

export ES_JAVA_OPTS="$ES_JAVA_OPTS -Djava.io.tmpdir=/path/to/temp/dir"
./bin/elasticsearch

在使用 RPM 或 Debian 安装包时,ES_JAVA_OPTS 可以通过系统配置文件设置。

在 jvm.options 中设置 JVM 堆大小

在 jvm.options 中设置 JVM 堆大小

-默认情况下,Elasticsearch 告诉 JVM 使用堆的最小值和最大值的 2GB。切换到生产时,保证 Elasticsearch 有足够的可用堆是非常重要的。

Elasticsearch 将通过 jvm.options 中的 Xms(堆的最小值)与 Xmx(堆的最大值)设置来分配堆的大小。

这个值依赖于服务器上可用的 RAM 数量,好的设置规则如下:

  • 堆的最小值(Xms)与堆的最大值(Xmx)设置成相同的。
  • Elasticsearch 的可用堆越大,它能在内存中缓存的数据越多。但是需要注意堆越大在垃圾回收时造成的暂停会越长。
  • 设置 Xmx 不要大于物理内存的 50%。用来确保有足够多的物理内存预留给操作系统缓存。
    不要设置 Xmx 超过 JVM 用来压缩对象指针的 cutoff(compressed oops);精确的 cutoff 可能不同,但接近于 32GB。你可以通过在日志中查找一条类似于下面的这条信息来确定这个 cutoff 限制。
    heap size [1.9gb], compressed ordinary object pointers [true]
  • 最好尽量保持低于 zero-based compressed oop 的阈值;精确的 cutoff 可能不同,但大多数系统 26GB 是安全的,但是在某些系统可能多达 30GB。你可以通过 JVM 的 XX:+UnlockDiagnosticVMOptions -XX:+PrintCompressedOopsMode 参数来验证限制,并通过类似如下的行来确定:
    heap address: 0x000000011be00000, size: 27648 MB, zero based Compressed Oops
    如果是开启了 zero-based compressed oop 则
    heap address: 0x0000000118400000, size: 28672 MB, Compressed Oops with base: 0x00000001183ff000

下面演示了如何通过 jvm.options 文件来配置堆大小:

-Xms2g #①
-Xmx2g #②
  1. 置堆的最小值为 2g。
  2. 设置堆的最大值为 2g。

他们同样也能通过环境变量来设置。先需要在 jvm.options 文件中注释掉 Xms 与 Xmx 设置,然后通过 ES_JAVA_OPTS 来设置:

ES_JAVA_OPTS="-Xms2g -Xmx2g" ./bin/elasticsearch #①
ES_JAVA_OPTS="-Xms4000m -Xmx4000m" ./bin/elasticsearch #②
  1. 设置堆的最小值与最大值为 2GB。
  2. 设置堆的最小值与最大值为 4000MB。

Windows 服务配置堆的大小与上面方式不同。初始值可以在安装 Windows 服务时配置,但是安装完之后也可以调整。查阅 Windows 服务文档来获取更多信息。

禁用 swapping

禁用 swapping

大多数操作系统尝试使用尽可能多的内存文件系统缓存和热切换出未使用的应用程序内存。这可能导致部分 JVM 堆被交换到磁盘上。

对于性能和节点的稳定性交换是非常糟糕的,应该不惜一切代价避免。它可能导致垃圾收集持续几分钟而不是毫秒,这可能导致节点响应缓慢,甚至脱离集群。

有三种方式禁用 swapping:

开启 bootstrap.memory_lock

第一步,在 Linux 与 Unix 系统中使用 mlockall 或者在 Windows 系统中使用 VirtuallLock 来尝试在 RAM 中锁定进程的地址空间,来阻止 Elasticsearch 内存被交换出去。

如果这些设置完成,请在 config/elasticsearch.yml 中添加如下行:

bootstrap.memory_lock: true
  • B3log

    B3log 是一个开源组织,名字来源于“Bulletin Board Blog”缩写,目标是将独立博客与论坛结合,形成一种新的网络社区体验,详细请看 B3log 构思。目前 B3log 已经开源了多款产品:SymSoloVditor思源笔记

    1083 引用 • 3461 回帖 • 285 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...