欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

docker启动ES内存溢出的解决方案

程序员文章站 2022-08-17 20:02:44
在elasticsearch的config中加jvm.options文件,修改堆栈大小,默认是2gb,直接启动es即可,保证之前已经映射了配置文件。-xms5g-xmx5g完整jvm.options文...

在elasticsearch的config中加jvm.options文件,修改堆栈大小,默认是2gb,直接启动es即可,保证之前已经映射了配置文件。

-xms5g
-xmx5g

完整jvm.options文件如下:

## jvm configuration
################################################################
## important: jvm heap size
################################################################
##
## you should always set the min and max jvm heap
## size to the same value. for example, to set
## the heap to 4 gb, set:
##
## -xms4g
## -xmx4g
##
## see https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################
# xms represents the initial size of total heap space
# xmx represents the maximum size of total heap space
-xms5g
-xmx5g
################################################################
## expert settings
################################################################
##
## all settings below this section are considered
## expert settings. don't tamper with them unless
## you understand what you are doing
##
################################################################
## gc configuration
-xx:+useconcmarksweepgc
-xx:cmsinitiatingoccupancyfraction=75
-xx:+usecmsinitiatingoccupancyonly
## optimizations
# pre-touch memory pages used by the jvm during initialization
-xx:+alwayspretouch
## basic
# force the server vm (remove on 32-bit client jvms)
-server
# explicitly set the stack size (reduce to 320k on 32-bit client jvms)
-xss1m
# set to headless, just in case
-djava.awt.headless=true
# ensure utf-8 encoding by default (e.g. filenames)
-dfile.encoding=utf-8
# use our provided jna always versus the system one
-djna.nosys=true
# use old-style file permissions on jdk9
-djdk.io.permissionsusecanonicalpath=true
# flags to configure netty
-dio.netty.nounsafe=true
-dio.netty.nokeysetoptimization=true
-dio.netty.recycler.maxcapacityperthread=0
# log4j 2
-dlog4j.shutdownhookenabled=false
-dlog4j2.disable.jmx=true
-dlog4j.skipjansi=true
## heap dumps
# generate a heap dump when an allocation from the java heap fails
# heap dumps are created in the working directory of the jvm
-xx:+heapdumponoutofmemoryerror
# specify an alternative path for heap dumps
# ensure the directory exists and has sufficient space
#-xx:heapdumppath=${heap.dump.path}
## gc logging
#-xx:+printgcdetails
#-xx:+printgctimestamps
#-xx:+printgcdatestamps
#-xx:+printclasshistogram
#-xx:+printtenuringdistribution
#-xx:+printgcapplicationstoppedtime
# log gc status to a file with time stamps
# ensure the directory exists
#-xloggc:${loggc}
# by default, the gc log file will not rotate.
# by uncommenting the lines below, the gc log file
# will be rotated every 128mb at most 32 times.
#-xx:+usegclogfilerotation
#-xx:numberofgclogfiles=32
#-xx:gclogfilesize=128m
# elasticsearch 5.0.0 will throw an exception on unquoted field names in json.
# if documents were already indexed with unquoted fields in a previous version
# of elasticsearch, some operations may throw errors.
#
# warning: this option will be removed in elasticsearch 6.0.0 and is provided
# only for migration purposes.
#-delasticsearch.json.allow_unquoted_field_names=true

补充:docker 容器内存限制

docker 内存限制

docker run -d -i -t -m 256m --memory-swap 512m --name centos2.12 centos /bin/bash

查看容器实例 内存限制:

docker启动ES内存溢出的解决方案

限制容器内存大小;

docker run -d -i -t -m 256m --memory-swap 512m --name centos centos /bin/bash

-m, --memory 
# 内存限制大小,单位可以为 b,k,m,g;最小为4m
--memory-swap
# 内存+交换分区大小总限制
--memory-reservation # 预留内存大小;容器在宿主机最小占用内存;
--oom-kill-disable
# out-of-memory 内存溢出;限制kill容器进程,默认没设置
--oom-score-adj
# 容器被 oom killer 杀死的优先级,范围是[-1000, 1000],默认为 0
--memory-swappiness
# 用于设置容器的虚拟内存控制行为。值为 0~100 之间的整数
--kernel-memory
核心内存限制,最小为 4m。

1、memory 设置容器内存大小;

--memory-swap 不是交换分区,而是 memory + swap 的大小;
容器的交换分区 swap = memory-swap - memory

2、docker 默认容器交换分区的大小和内存相同

memory-swap 不设置 或者设置为 0 ;
容器的交换分区 swap 大小就是 memory 的小大;
容器的进程使用最大内存 = memory + swap

3、memory-swap 设置

当 memory-swap 设置为 -1 时;
容器内存大小为 memory 设置的大小;
交换分区大小为宿主机 swap 大小;
容器进程能使用的最大内存 = memory + 宿主机 swap 大小;

4、内存溢出

--oom-kill-disable
限制 kill 容器进程; (必须设置在 memory 之后才有限;)
docker run -d -i -t -m 256m --oom-kill-disable --name centos-1 centos /bin/bash

5、核心内存 & 用户内存

核心内存和用户内存不同的地方在于核心内存不能被交换出。

不能交换出去的特性使得容器可以通过消耗太多内存来堵塞一些系统服务。

核心内存包括:
stack pages(栈页面)
slab pages
socket memory pressure
tcp memory pressure

可以通过设置核心内存限制来约束这些内存。

每个进程都要消耗一些栈页面,通过限制核心内存,可以在核心内存使用过多时阻止新进程被创建。

docker run -d -i -t -m 500m --kernel-memory 128m --name centos-2 centos /bin/bash
限制容器内存 256m;限制核心内存 128m 。
docker run -d -i -t --kernel-memory 128m --name centos-3 centos /bin/bash
内存为宿主机memory大小, 限制核心内存 128m

6、swappiness 内存回收页

容器的内核可以交换出一定比例的匿名页。

--memory-swappiness就是用来设置这个比例的。
--memory-swappiness可以设置为从 0 到 100。
# 0 表示关闭匿名页面交换。
# 100 表示所有的匿名页都可以交换。默认情况下,如果不适用--memory-swappiness,则该值从父进程继承而来。
docker run -d -i -t --memory-swappiness=0 --name centos-4 centos /bin/bash
将--memory-swappiness设置为 0 可以保持容器的工作集,避免交换代理的性能损失。

swappiness 的值越大,表示越积极使用swap分区,越小表示越积极使用物理内存。默认值swappiness=60

sysctl vm.swappiness = 100 
# cat /proc/sys/vm/swappiness

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。