zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

(0.5)elasticsearch配置文件(基础)

2023-09-11 14:21:08 时间

【0】默认文件内容

#(1)cluster
cluster.name:集群名称 # 默认名称为 elasticsearch
cluster.initial_master_nodes:["node-1","node-2"]  #分布式集群,初始化节点
gateway.recover_after_nodes:3 #在整个集群重启后,阻止恢复访问,直到启动完 N 个节点位置(这里就是要3个节点及以上启动好之后才可以访问)
#(2)node
#node.name:node-1         #默认为计算机名,但应该为节点起一个描述性名称
#node.attr.rack:r1        #添加自定义的节点属性

#(3)path path.data:
/path/to/data #数据文件路径,可以有多个路径,多个路径以 , 逗号分割 path.logs:/path/to/logs
#(4)memory
bootstrap.memory_lock:true #在启动时锁定内存(锁定内存页),确保将堆大小设置为可用内存的一半,并且在操作系统层面解开使用限制
#(5)network
network.host:192.168.0.1 #设置绑定地址:本机IP network.host:0.0.0.0 #设置绑定地址:任意地址,建议设置这个,省事 http.port:9200 #设置绑定端口

#(6)discovery
#默认主机列表为 【127.0.0.1】,【::1】,也就是本机 discovery.seed_hosts:["host1","host2"]
#others
action.destructive_requires_name:true   #删除索引时,必须要提供具体的名称!

英文原文:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
elasticsearch.yml

 

【1】重要配置

参考官网:https://www.elastic.co/guide/en/elasticsearch/reference/7.11/important-settings.html

path.data and path.logs

数据库、日志存放目录

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

(1)如果您使用.zip.tar.gz归档,则datalogs目录是的子文件夹$ES_HOME

如果这些重要文件夹保留在默认位置,则在将Elasticsearch升级到新版本时,很有可能将其删除;

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

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

Cluster.name

(1)当cluster.name节点与集群中的所有其他节点共享其节点时,该节点只能加入集群。

(2)默认名称为elasticsearch,但您应将其更改为描述群集用途的适当名称。

cluster.name: logging-prod

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

Node.name

  Elasticsearchnode.name用作Elasticsearch特定实例的人类可读标识符,因此它被包含在许多API的响应中。

  它默认为计算机在Elasticsearch启动时具有的主机名,但可以elasticsearch.yml按以下方式显式配置

Network.host

  默认情况下,Elasticsearch仅绑定到环回地址(例如127.0.0.1和)[::1]这足以在服务器上运行单个开发节点。

  实际上,可以从$ES_HOME单个节点上的相同位置启动一个以上的节点。这对于测试Elasticsearch形成集群的能力很有用,但不是建议用于生产的配置。

为了与其他服务器上的节点形成集群,您的节点将需要绑定到非环回地址。虽然有很多网络设置,通常您需要配置的是

 

network.host: 192.168.1.10

network.host设置也了解一些特殊的值,比如_local__site__global_和喜欢修饰:ip4:ip6,详情可参见的特殊值 network.host.

一旦为提供了自定义设置network.host,Elasticsearch就会假设您正在从开发模式转换为生产模式,并将许多系统启动检查从警告升级为异常。开发模式与生产模式 欲获得更多信息。

Discovery and Cluster build

在投入生产之前,应该配置两个重要的发现和集群形成设置,以便集群中的节点可以彼此发现并选举一个主节点。

(1)discovery.seed_hosts

  开箱即用,无需任何网络配置,Elasticsearch将绑定到可用的环回地址,并将扫描本地端口9300至9305,以尝试连接到在同一服务器上运行的其他节点。

  此行为无需进行任何配置即可提供自动群集体验。

如果要与其他主机上的节点组成集群,请使用 静态 discovery.seed_hosts设置。此设置提供了群集中其他主机节点的列表,这些节点具有主机资格,并且可能处于活动状态并且可以联系以播种发现过程

此设置接受群集中所有主资格节点的YAML序列或地址数组。每个地址可以是IP地址,也可以是通过DNS解析为一个或多个IP地址的主机名。

discovery.seed_hosts:
   - 192.168.1.10:9300
   - 192.168.1.11 
   - seeds.mydomain.com 
   - [0:0:0:0:0:ffff:c0a8:10c]:9301 
cluster.initial_master_nodes: 
   - master-node-a
   - master-node-b
   - master-node-c
  • 该端口是可选的,通常默认为9300,但是次默认可以通过设置覆盖
  • 如果主机名解析为多个IP地址,则该节点将尝试在所有解析的地址处发现其他节点
  • IPV6地址不许放在[] 括号中

(2)cluster.initial_master_nodes

如果您的主节点资格没有固定的名称或地址,请使用 替代主机提供商 动态地找到他们的地址

《1》首次启动Elasticsearch集群时, 集群引导步骤将确定其主节点符合资格的节点集,该节点的票数将在第一次选举中进行计数。开发模式下(未配置发现设置),此步骤由节点自身自动执行。

《2》由于自动引导本质上不安全的,因此在生产模式下启动新集群时,必须明确列出符合主控条件的节点,这些节点的票数应在首次选举中进行计数。您可以使用cluster.initial_master_nodes设置来设置此列表 

Heap size settings

  默认情况下,Elasticsearch根据节点的角色和总内存自动调整JVM堆的大小 对于大多数生产环境,我们建议使用此默认大小。如果需要,您可以通过手动设置JVM堆大小来覆盖默认 大小

注意:

  • 自动堆大小调整需要捆绑的JDK,或者,如果使用自定义JRE位置,则需要Java 14或更高版本的JRE。
  • Docker等容器中运行时,总内存定义为该容器可见的内存量,而不是主机上的总系统内存。

【手动设置jvm堆大小】

默认选项值是文件 $ES_HOME/config/jvm.options  中定义的

方法(1):vim $ES_HOME/config/jvm.options.d/jvm_head.options

-Xms2g
-Xmx2g

方法(2):环境变量

ES_JAVA_OPTS="-Xms2g -Xmx2g" ./bin/elasticsearch 
ES_JAVA_OPTS="-Xms2000m -Xmx2000m" ./bin/elasticsearch 

  将最小堆大小设置为2g。

  将最大堆大小设置为2g。

 

 

JVM堆转储路径设置

就是内存不够用了,转存到文件中去

默认配置在 $ES_HOME/config/jvm.options(但一般不需要配置)

默认情况下,Elasticsearch将JVM配置为将内存不足异常中的堆转储到默认数据目录。

RPM和 Debian软件包中,数据目录为/var/lib/elasticsearch在 Linux,MacOSWindows发行版上,该data目录位于Elasticsearch安装目录的根目录下。

如果此路径不适合接收堆转储,请修改以下 -XX:HeapDumpPath=...jvm.options

  • 如果指定目录,那么JVM将基于正在运行的实例的PID为堆转储生成文件名。
  • 如果指定固定文件名而不是目录,则当JVM需要在内存不足异常时执行堆转储时,该文件必须不存在。否则,堆转储将失败。

GC记录

JVM的垃圾回收机制,操作记录,默认配置在 $ES_HOME/config/jvm.options(但一般不需要配置)

默认情况下,Elasticsearch启用GC日志。这些配置在jvm.options并输出到与Elasticsearch日志相同的默认位置。默认配置每64 MB轮换一次日志,最多可消耗2 GB磁盘空间。

您可以使用以下内容中描述的命令行选项重新配置JVM日志记录: JEP 158:统一JVM日志记录除非您jvm.options直接更改默认文件,否则除了您自己的设置外,还将应用Elasticsearch默认配置。要禁用默认配置,请首先通过提供-Xlog:disable选项来禁用日志记录,然后提供您自己的命令行选项。这将禁用所有JVM日志记录,因此请确保检查可用选项并启用所需的所有功能。

要查看原始JEP中未包含的其他选项,请参见 使用JVM统一日志记录框架启用日志记录.

  • /opt/my-app/gc.log通过创建$ES_HOME/config/jvm.options.d/gc.options一些示例选项,将默认GC日志输出位置更改为:
# Turn off all previous logging configuratons
-Xlog:disable
# Default settings from JEP 158, but with `utctime` instead of `uptime` to match the next line
-Xlog:all=warning:stderr:utctime,level,tags
# Enable GC logging to a custom location with a variety of options
-Xlog:gc*,gc+age=trace,safepoint:file=/opt/my-app/gc.log:utctime,pid,tags:filecount=32,filesize=64m
  • 配置Elasticsearch Docker容器将GC调试日志发送到标准错误(stderr)。这使容器协调器可以处理输出。如果使用ES_JAVA_OPTS环境变量,请指定:
MY_OPTS="-Xlog:disable -Xlog:all=warning:stderr:utctime,level,tags -Xlog:gc=debug:stderr:utctime" docker run -e ES_JAVA_OPTS="$MY_OPTS" # etc

Temp dir(很重要)

默认配置在 $ES_HOME/config/jvm.options(但一般不需要配置)

默认情况下,Elasticsearch使用启动脚本在系统临时目录下立即创建的私有临时目录。

(1)在某些Linux发行版中,/tmp如果最近未访问过文件和目录,系统实用程序将从中清除文件和目录

(2)如果长时间不使用需要使用临时目录的功能,则可能会导致在运行Elasticsearch时删除私有临时目录。如果随后使用需要临时目录的功能,则将导致问题。

(3)如果使用.deb.rpm软件包安装Elasticsearch并在其下运行,systemd则定期清理将排除Elasticsearch使用的私有临时目录。

(4)但是,如果打算.tar.gz长时间在Linux上运行发行版,则应考虑为Elasticsearch创建一个专用的临时目录,该目录不在将清除旧文件和目录的路径下。

(5)该目录应该设置权限,以便只有运行Elasticsearch的用户才能访问它。然后 $ES_TMPDIR 在启动Elasticsearch之前环境变量设置为指向它。

因为,写入文档到ES时,并不是实时写入;

NRT(Near Realtime)近实时:(1s)

    

 

甚至,当集群没有查询操作时,写入近乎30秒才从临时目录同步一次到索引中去;

JVM致命错误日志

默认情况下,Elasticsearch将JVM配置为将致命错误日志写入默认日志目录;

(1)rpm 和 yum

默认存放到 /var/log/elasticsearch

(2)linux windows macos

默认存放到 $ES_HOME/logs

这些是JVM遇到致命错误(例如,分段错误)时生成的日志。如果这个路径是不适合接收日志,您应该修改条目-XX:ErrorFile=...jvm.options 到替代路径。

Cluster backup

在灾难中,快照可以防止永久性数据丢失。 快照生命周期管理是对群集进行定期备份的最简单方法。有关更多信息,请参阅备份集群

【疑问】

开发模式与生产模式

  默认情况下,Elasticsearch假定您正在开发模式下工作。如果上述任何设置的配置不正确,都会在日志文件中写入警告,但是您将能够启动和运行Elasticsearch节点。

  一旦您配置了类似的网络设置 network.host,Elasticsearch就会假设您即将投入生产,并将上述警告升级为异常。

  这些异常将阻止您的Elasticsearch节点启动。这是一项重要的安全措施,可确保不会因服务器配置错误而丢失数据。