title: How to disable swapping in Elasticsearch on Kubernetes?
body: <p>As per the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-configuration-memory.html#setup-configuration-memory" rel="nofollow noreferrer">official es docs</a>, disabling swapping is one of the best performance boosts available to Elasticsearch.</p>
  
  <p>However, it's proving to be difficult to configure. I've spent a number of hours researching and attempting different methods to disable swapping using the official ES docker image on Kubernetes.</p>
  
  <p>When setting <code>bootstrap.memory_lock: true</code> as an env variable, the image fails to boot up with the error: <code>Unable to lock JVM Memory: error=12, reason=Cannot allocate memory. This can result in part of the JVM being swapped out. Increase RLIMIT_MEMLOCK, soft limit: 65536, hard limit: 65536</code>. As the docs point out, this is kind of expected. I've even mounted a custom <code>/etc/security/limits.conf</code> with the settings, but that's failed.</p>
  
  <p>What is the recommended way to disable swapping when using the official es image on k8s?</p>
  
  <p>And, here are the relevant sections of my yaml</p>
  
  <pre><code>apiVersion: apps/v1beta1
  kind: StatefulSet
  metadata:
    name: elastic-data
  spec:
    serviceName: elastic-data
    replicas: 1
    template:
      spec:
        securityContext:
          runAsUser: 0
          fsGroup: 0
        containers:
        - name: elastic-data
          image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.4.0
          env:
          - name: ES_JAVA_OPTS
            value: "-Xms2g -Xmx2g"
          - name: cluster.name
            value: "elastic-devs"
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.zen.ping.unicast.hosts
            value: "elastic-master.default.svc.cluster.local"
          - name: node.master
            value: "false"
          - name: node.ingest
            value: "false"
          - name: node.data
            value: "true"
          - name: network.host
            value: "0.0.0.0"
          - name: path.data
            value: /usr/share/elasticsearch/data
          - name: indices.memory.index_buffer_size
            value: "512MB"
          - name: bootstrap.memory_lock
            value: "true"
          resources:
            requests:
              memory: "3Gi"
            limits:
              memory: "3Gi"
          ports:
          - containerPort: 9300
            name: transport
          - containerPort: 9200
            name: http
          volumeMounts:
          - name: data-volume
            mountPath: /usr/share/elasticsearch/data
          - name: swappiness-config
            mountPath: /etc/security/limits.conf
            subPath: limits.conf
        volumes:
        - name: data-volume
          persistentVolumeClaim:
            claimName: pvc-es
       - name: swappiness-config
         configMap:
           name: swappiness-config
           items:
            - key: limits.conf
               path: limits.conf
  </code></pre>
  
  <p>limits.conf</p>
  
  <pre><code>elasticsearch soft memlock unlimited
  elasticsearch hard memlock unlimited
  elasticsearch hard nofile 65536
  elasticsearch soft nofile 65536
  </code></pre>