因电脑内存只有16G,部署的虚拟机内存不够,导致k8s的elasticsearch集群提示OOMKilled。由于ES是运行在JVM上,JVM本身除了分配的heap内存以外,还会用到一些堆外(off heap)内存。 在小内存的机器上跑ES,如果heap划分过多,累加上堆外内存后,总的JVM使用内存量可能超过物理内存限制。 如果swap又是关闭的情况下,就会被操作系统oom killer杀掉。

[root@master1 default]# kubectl -n component get pod 
NAME                READY   STATUS      RESTARTS   AGE
es-data-default-0   0/1     OOMKilled   1          58s
es-data-default-1   0/1     OOMKilled   1          58s
es-data-default-2   0/1     OOMKilled   1          58s

可以修改elasticsearch的配置文件/elasticsearch/config/jvm.option解决

vim es-config-jv.yaml        #创建configmap的yaml文件
apiVersion: v1
kind: ConfigMap
metadata:
  name: elasticsearch-config-jv
  namespace: component
data:
  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:
    ###
    -Xms512m                             #原为### -Xms4g,去掉#号,修改为512m
    -Xmx512m                               #原为### -Xmx4g,去掉#号,修改为512m
    ###
    ### 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
    #
    ## Commented out since these are supplied as env vars in the command-line
    ## for the service.
    #
    -Xms512m
    -Xmx512m
    #
    #################################################################
    ### 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
    #
    ## disable calls to System#gc
    #-XX:+DisableExplicitGC
    #
    ## 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 keep Netty from being unsafe
    #-Dio.netty.noUnsafe=true
    #-Dio.netty.noKeySetOptimization=true
    #
    ## 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}
    #
    ## 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

修改statefulset的配置,增加configmap的文件挂载

  - name: elasticsearch-config-jv
          mountPath: /elasticsearch/config/jvm.options
          subPath: jvm.options
      volumes:
      - name: elasticsearch-config-jv
        configMap:
          name: elasticsearch-config-jv
sysctl -w vm.max_map_count=262144       #es需要vm.max_map_count为262144
sysctl -a|grep vm.max_map_count
vim /etc/sysctl.conf              #文件最后添加一行,持久配置

vm.max_map_count=262144
kubectl -n component get pod         #已经开启正常了
NAME                READY   STATUS    RESTARTS   AGE
es-data-default-0   1/1     Running   0          10m
es-data-default-1   1/1     Running   0          10m
es-data-default-2   1/1     Running   0          10m