<p>I am trying to load <code>elasticsearch.yml</code> file using <code>ConfigMap</code> while installing ElasticSearch using Kubernetes.</p>

<pre><code>kubectl create configmap elastic-config --from-file=./elasticsearch.yml
</code></pre>

<p>The <code>elasticsearch.yml</code> file is loaded in the container with <code>root</code> as its owner and read-only permission (<a href="https://github.com/kubernetes/kubernetes/issues/62099" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/62099</a>). Since, ElasticSearch will not start with <code>root</code> ownership, the pod crashes.</p>

<p>As a work-around, I tried to mount the <code>ConfigMap</code> to a different file and then copy it to the <code>config</code> directory using an <code>initContainer</code>. However, the file in the <code>config</code> directory does not seem to be updated. 
Is there anything that I am missing or is there any other way to accomplish this?</p>

<p><strong>ElasticSearch Kubernetes StatefulSet:</strong></p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata: 
  name: es-cluster
  labels:
    app: elasticservice
spec:
  serviceName: elasticsearch
  replicas: 1
  selector: 
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      containers:
      - name: elasticsearch
        image: docker.elastic.co/elasticsearch/elasticsearch:6.5.4
        resources:
          limits:
            cpu: 1000m
          requests: 
            cpu: 100m
        ports:
        - containerPort: 9200
          name: rest
          protocol: TCP
        - containerPort: 9300
          name: inter-node
          protocol: TCP
        volumeMounts:
        - name: elastic-config-vol
          mountPath: /tmp/elasticsearch
        - name:  elastic-storage
          mountPath: /usr/share/elasticsearch/data
        env:
          - name: cluster.name
            value: docker-elastic
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: discovery.zen.ping.unicast.hosts
            value: "elastic-service"
          - name: discovery.zen.minimum_master_nodes
            value: "1"
          - name: node.master
            value: "true"
          - name: node.data
            value: "true"
          - name: ES_JAVA_OPTS
            value: "-Xmx256m -Xms256m"
      volumes:
        - name: elastic-config-vol
          configMap:
           name: elastic-config
           items:
           - key: elasticsearch.yml
             path: elasticsearch.yml
        - name: elastic-config-dir
          emptyDir: {}
        - name: elastic-storage
          emptyDir: {}
      initContainers:
        # elasticsearch will not run as non-root user, fix permissions
      - name: fix-vol-permission
        image: busybox
        command:
          - sh
          - -c
          - chown -R 1000:1000 /usr/share/elasticsearch/data
        securityContext:
          privileged: true
        volumeMounts:
          - name: elastic-storage
            mountPath: /usr/share/elasticsearch/data
      - name: fix-config-vol-permission
        image: busybox
        command:
          - sh
          - -c
          - cp /tmp/elasticsearch/elasticsearch.yml /usr/share/elasticsearch/config/elasticsearch.yml
        securityContext:
          privileged: true
        volumeMounts:
          - name: elastic-config-dir
            mountPath: /usr/share/elasticsearch/config
          - name: elastic-config-vol
            mountPath: /tmp/elasticsearch
      # increase default vm.max_map_count to 262144
      - name: increase-vm-max-map-count
        image: busybox
        command:
          - sysctl
          - -w
          - vm.max_map_count=262144
        securityContext: 
          privileged: true
      - name: increase-the-ulimit
        image: busybox
        command:
          - sh
          - -c
          - ulimit -n 65536
        securityContext:
          privileged: true
</code></pre>

Daniccan
<p>I use:</p>

<pre><code>...
        volumeMounts:
        - name: config
          mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
          subPath: elasticsearch.yml
      volumes:
      - name : config
        configMap:
          name: es-configmap
</code></pre>

<p>without any permissions problem, but you can set permissions with <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#configmap-v1-core" rel="nofollow noreferrer">defaultMode</a></p>
