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

 更新时间:2021年03月29日 11:42:42   作者:Airship  
这篇文章主要介绍了docker启动ES内存溢出的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在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 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

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

相关文章

  • 教你如何通过 SSH 连接到 Docker 容器

    教你如何通过 SSH 连接到 Docker 容器

    如何通过 SSH 连接到正在运行的容器,以及为什么在这样做之前应该三思而后行,对如何通过SSH连接到Docker容器相关知识感兴趣的朋友跟随小编一起看看吧
    2022-01-01
  • 深入解析docker三种网络模式

    深入解析docker三种网络模式

    这篇文章主要介绍了docker三种网络模式,分别是bridge桥接模式,host主机模式,none无网络模式,每种模式给大家介绍的非常详细,需要的朋友可以参考下
    2022-04-04
  • Docker部署及使用压测神器sysbench的方法

    Docker部署及使用压测神器sysbench的方法

    sysbench 是一个开源跨平台的多线程性能测试工具,这篇文章主要介绍了Docker部署及使用压测神器sysbench的相关知识,需要的朋友可以参考下
    2022-08-08
  • k8s 与docker空间使用分析与清理方法

    k8s 与docker空间使用分析与清理方法

    使用Docker 运行业务一段时间后,可能会出现宿主节点的磁盘容量占用高,导致宿主机磁盘空间不足等异常,对业务造成影响,本文对 Docker 的空间占用进行汇总分析,需要的朋友可以参考下
    2022-12-12
  • docker启动镜像失败后如何用日志logs查找失败原因及解决

    docker启动镜像失败后如何用日志logs查找失败原因及解决

    在使用docker的时候,在某些未知的情况下可能启动了容器,但是过了没几秒容器自动退出了,这个时候如何排查问题呢?下面这篇文章主要给大家介绍了关于docker启动镜像失败后如何用日志logs查找失败原因及解决的相关资料,需要的朋友可以参考下
    2023-05-05
  • 详解docker pull 下来的镜像文件存放的位置

    详解docker pull 下来的镜像文件存放的位置

    本篇文章主要介绍了详解docker pull 下来的镜像文件存放的位置,具有一定的参考价值,有兴趣的可以了解一下。
    2017-04-04
  • 使用Docker将容器打成镜像的方法步骤

    使用Docker将容器打成镜像的方法步骤

    本文主要介绍了使用Docker将容器打成镜像的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • docker-maven-plugin 插件无法拉取对应jar包问题

    docker-maven-plugin 插件无法拉取对应jar包问题

    这篇文章主要介绍了docker-maven-plugin 插件无法拉取问题,总是报错,如何解决这个问题呢,下面小编给大家带来了解决方法,一起看看吧
    2021-09-09
  • dockerDesktop使用教程

    dockerDesktop使用教程

    本文给大家分享docker Desktop使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-11-11
  • docker环境搭建mgr集群的问题及原理

    docker环境搭建mgr集群的问题及原理

    这篇文章主要介绍了docker 搭建mgr集群,大家都知道MySQL推出MGR之前,传统复制模式分为异步复制和半同步复制,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论