Elasticsearch简单入门--elasticsearch重要的系统配置(一)

Important System Configuration 重要的系统配置

理想情况下,Elasticsearch应该单独在服务器上运行,并使用所有可用资源,为了做到这一点,您需要将操作系统配置为允许运行Elasticsearch的用户访问比默认情况下允许的更多的资源(you need to configure your operating system to allow the user running Elasticsearch to access more resources than allowed by default)

在投入生产之前,必须解决一下设置

  • Set JVM heap size
  • Disable swapping
  • Increase file descriptors
  • Ensure sufficient virtual memory
  • Ensure sufficient threads

 Development mode vs production mode 开发模式 vs 生产模式

默认情况,Elasticsearch假设你在开发模式下工作,如果上面的任意配置项没有被正确配置,一个警告信息将被写入到日志文件,但是你能够启动并且运行你的Elasticsearch节点。(虽然有警告,但是不影响Elasticsearch的正常启动和运行)

只要你配置了像network.host这样的网络设置,Elasticsearch就假设你正在转向生产环境,并将上述警告升级为异常,这些异常将会阻止你的Elasticsearch节点的启动,这是一个重要的安全措施,可以确保您不会因为服务器配置不当而丢失数据(As soon as you configure a network setting like network.host, Elasticsearch assumes that you are moving to production and will upgrade the above warnings to exceptions. These exceptions will prevent your Elasticsearch node from starting. This is an important safety measure to ensure that you will not lose data because of a malconfigured server)

1. Configuring system settings 配置系统设置

在哪里配置系统设置取决于你使用了哪个包来安装Elasticsearch,以及你正在使用哪个操作系统。(Where to configure systems settings depends on which package you have used to install Elasticsearch, and which operating system you are using.)

使用.zip或.tar时。gz包,系统设置可配置:

  • 暂时地修改使用 ulimit
  • 永久地修改 在 /etc/security/limits.conf 文件修改 

当使用RPM或者Debain包,大多数系统设置是在system configuration file中被设置的, 但是,使用systemd的系统要求在systemd配置文件中指定系统限制。

参考:https://www.elastic.co/guide/en/elasticsearch/reference/5.6/setting-system-settings.html#systemd

ulimit

在Linux系统中,ulimit可用于临时更改资源限制,在切换到将运行Elasticsearch的用户之前, Limits通常需要使用Root用户设置

例如,要将打开文件句柄的数量设置为65536,可以执行如下操作(For example, to set the number of open file handles (ulimit -n) to 65,536, you can do the following)

sudo su   # 1 
ulimit -n 65536  # 2
su elasticsearch # 3

Become root. 切换到root用户

Change the max number of open files.

更改打开文件的最大数量

Become the elasticsearch user in order to start Elasticsearch.

切换到elasticsearch用户 以启动Elasticsearch

新限制只适用于当前会话。 (The new limit is only applied during the current session.)

您可以使用ulimit -a查询当前应用的所有限制。(You can consult all currently applied limits with ulimit -a.)

/etc/security/limits.conf 

在Linux系统上,可以通过编辑/etc/security/limit .来为特定用户设置持久性限制(On Linux systems, persistent limits can be set for a particular user by editing the /etc/security/limits.conf file)

若要将elasticsearch用户的最大打开文件数设置为65,536,请将以下行添加到limits.conf文件中 (To set the maximum number of open files for the elasticsearch user to 65,536, add the following line to the limits.conf file:)

elasticsearch  -  nofile  65536

此更改仅在下一次elasticsearch用户打开新会话时生效。(This change will only take effect the next time the elasticsearch user opens a new session.)

2. Setting JVM options 设置jvm选项

设置java虚拟机选项(包括系统属性和JVM标记)的首选方法是通过jvm.options配置文件,这个文件的默认位置是$ES_HOME/config/jvm.options(当通过tar或者zip安装的时候)和/etc/elasticsearch/jvm.options(当通过Debian或RPM包安装的时候),这个文件包含了一个JVM参数的必须以-开始的行分界符,您可以向该文件添加自定义JVM标志,并将此配置签入版本控制系统。

设置Java虚拟机选项的另一种机制是通过ES_JAVA_OPTS环境变量,例如

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

3. Set JVM heap size via jvm.options 设置JVM堆大小通过jvm.options

默认情况下,Elasticsearch告诉JVM其使用的最小和最大的堆为2GB, 在转移到生成环境时,配置堆大小以确保Elasticsearch拥有足够的堆空间可用是很重要的。

Elasticsearch将会分配在jvm.options中通过Xms(最小堆大小)和Xmx(最大堆大小)的设置的整个堆。

这些设置项的值取决于在你服务器上的可用RAM的数量,好的经验法则是:

Set the minimum heap size (Xms) and maximum heap size (Xmx) to be equal to each other.

设置最小堆大小和最大堆大小彼此相等

The more heap available to Elasticsearch, the more memory it can use for caching. But note that too much heap can subject you to long garbage collection pauses.

Elasticsearch可用堆越多,用于缓存的内存就越多,但是请注意,过多的堆会导致长时间的垃圾收集暂停。

Set Xmx to no more than 50% of your physical RAM, to ensure that there is enough physical RAM left for kernel file system caches.

将Xmx设置为不超过物理RAM的50%,以确保有足够的物理RAM留给内核文件系统缓存。

Don’t set Xmx to above the cutoff that the JVM uses for compressed object pointers (compressed oops); the exact cutoff varies but is near 32 GB. You can verify that you are under the limit by looking for a line in the logs like the following:

不要将Xmx设置在JVM用于压缩对象指针的界限之上(被压缩了);确切的界限是不相同,但接近32gb。您可以通过在日志中查找如下所示的行来验证您是否处于限制之下:

heap size [1.9gb], compressed ordinary object pointers [true]

Even better, try to stay below the threshold for zero-based compressed oops; the exact cutoff varies but 26 GB is safe on most systems, but can be as large as 30 GB on some systems. You can verify that you are under the limit by starting Elasticsearch with the JVM options -XX:+UnlockDiagnosticVMOptions -XX:+PrintCompressedOopsMode and looking for a line like the following:

heap address: 0x000000011be00000, size: 27648 MB, zero based Compressed Oops

showing that zero-based compressed oops are enabled instead of

heap address: 0x0000000118400000, size: 28672 MB, Compressed Oops with base: 0x00000001183ff000

如何通过jvm.options文件来设置堆大小,例如

-Xms2g # 设置最小堆大小为2GB
-Xmx2g # 设置最大堆大小为2GB

还可以通过环境变量设置堆大小,这可以通过注释掉jvm.options文件中的Xms和Xmx设置来实现,并通过ES_JAVA_OPTS设置这些值(It is also possible to set the heap size via an environment variable. This can be done by commenting out the Xms and Xmx settings in the jvm.options file and setting these values via ES_JAVA_OPTS:)

ES_JAVA_OPTS="-Xms2g -Xmx2g" ./bin/elasticsearch # 设置最小堆、最大堆大小为2GB
ES_JAVA_OPTS="-Xms4000m -Xmx4000m" ./bin/elasticsearch  #设置最小堆、最大堆大小为4000MB

 4. Disable swapping 禁止交换

大多数操作系统试图为文件系统缓存使用尽可能多的内存,并急切地交换未使用的应用程序内存,这可能导致JVM堆的某些部分,甚至其可执行页面被交换到磁盘。

交换对性能非常不利,为了节点的稳定性,应不惜一切代价避免,它可能导致垃圾收集持续分钟而不是毫秒,并且会导致节点响应缓慢,甚至与集群断开连接。(Swapping is very bad for performance, for node stability, and should be avoided at all costs. It can cause garbage collections to last for minutes instead of milliseconds and can cause nodes to respond slowly or even to disconnect from the cluster)

有三种方法可以禁用交换。首选选项是完全禁用交换,如果这不是一个选项,那么是否选择最小化swappiness而不是内存锁定取决于您的环境。

Disable all swap files

通常,Elasticsearch是唯一运行在盒子上的服务,它的内存使用由JVM options控制。不需要启用交换。

在Linux系统上,您可以通过运行以下命令暂时禁用交换:

sudo swapoff -a

要永久禁用交换,你需要编辑/etc/fstab文件并且注释掉包含单词swap的任何行. (To disable it permanently, you will need to edit the /etc/fstab file and comment out any lines that contain the word swap)

Configure swappiness

在Linux系统上可用的另一个选项是确保sysctl的值vm.swappiness设置为1,这减少了内核交换的倾向,在正常情况下不应该导致交换,同时还允许整个系统在紧急情况下进行交换.

Enable bootstrap.memory_lock

另一个选项是在Linux/Unix系统上使用mlockall,试图将进程地址空间锁定到RAM中,防止任何Elasticsearch内存被交换出去,

这可以通过在config/elasticsearch.yml文件中添加这一行来实现:

bootstrap.memory_lock: true

注意:如果mlockall试图分配比可用内存更多的内存,那么mlockall可能会导致JVM或shell会话退出!

在启动Elasticsearch之后,通过检查如下请求输出中的mlockall的值,可以看到这个设置是否成功应用:

http://192.168.101.118:9200/_nodes?filter_path=**.mlockall 

{
	"nodes": {
		"rIb6AA0sQ3aGnyt8uiM-Vg": {
			"process": {
				"mlockall": true
			}
		},
		"831BjGQLQXeCwTzz956b-g": {
			"process": {
				"mlockall": true
			}
		},
		"s4ZsIA2PQfy84hX1pedZ9A": {
			"process": {
				"mlockall": false
			}
		}
	}
}

 以上是我的Elasticsearch集群中3个节点,其中2个设置了禁止交换,如果你看到mlockall is false,那么这意味着mlockall请求已经失败,通过单词Unable to lock JVM Memory,你将在日志中看到一行包含更多信息的行。

在Linux/Unix系统中,最可能的原因是运行Elasticsearch的用户没有lock memory的权限,可以通过如下语句授权:

.zip and .tar.gz  # 通过zip 或者 tar.gz 安装Elasticsearch

Set ulimit -l unlimited as root before starting Elasticsearch, # 在启动Elasticsearch之前,使用root用户 设置ulimit -l unlimited 

or set memlock to unlimited in/etc/security/limits.conf.

在/etc/security/limits.conf 添加如下,然后重启Elasticsearch,再请求http://192.168.101.118:9200/_nodes?filter_path=**.mlockall 

*   -   memlock   unlimited 

mlockall失败的另一个可能原因是临时目录(通常是/tmp)是用noexec选项挂载的(Another possible reason why mlockall can fail is that the temporary directory (usually /tmp) is mounted with the noexec option),可以通过使用ES_JAVA_OPTS环境变量指定一个新的临时目录来解决这个问题

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

或者在jvm.options配置文件中设置JVM flag

猜你喜欢

转载自blog.csdn.net/zhen_6137/article/details/86137865