<p>I want to overwrite the file on the pod container. Right now I have <code>elasticsearch.yml</code> at location <code>/usr/share/elasticsearch/config</code>.</p>
<p>I was trying to achieve that with <code>initContainer</code> at kubernetes deployment file, so I added something like:</p>
<pre class="lang-yaml prettyprint-override"><code>        - name: disabled-the-xpack-security
          image: busybox
          command: 
          - /bin/sh 
          - -c 
          - |
            sleep 20
            rm /usr/share/elasticsearch/config/elasticsearch.yml 
            cp /home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml /usr/share/elasticsearch/config/
          securityContext:
            privileged: true
</code></pre>
<p>But this doesn't work, error looks like:</p>
<pre><code>rm: can't remove '/usr/share/elasticsearch/config/elasticsearch.yml': No such file or directory
cp: can't stat '/home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml': No such file or directory
</code></pre>
<p>I was trying to use some <code>echo &quot;some yaml config&quot; &gt;&gt; elasticsearch.yml</code>, but this kind of workarounds doesn't work, because I was able to keep proper yaml formatting.</p>
<p>Do you have any suggestions, how can I do this?</p>

<p>As stated by Arman in the comments, you can create a ConfigMap with the contents of <code>/home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml</code>and mount it as a volume in the deployment.</p>
<p>To create the config map from your file you can run:</p>
<pre><code>kubectl create configmap my-es-config --from-file=/home/x/IdeaProjects/BD/infra/istio/kube/elasticsearch.yml
</code></pre>
<p>This will create a ConfigMap inside your kubernetes cluster with the yaml file.</p>
<p>You can then use that and add the volume mount to your deployment as:</p>
<pre><code>  containers:
    - name: elasticsearch
      image: k8s.gcr.io/busybox
      .
      .
      .
      volumeMounts:
      - name: config-volume
        mountPath: /usr/share/elasticsearch/config/
  volumes:
    - name: config-volume
      configMap:
        name: my-es-config
</code></pre>
<h3>Notes</h3>
<ul>
<li>It is recommended to create your ConfigMap as yaml as well. More information <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume" rel="noreferrer">here</a></li>
<li>Mounting a configmap directly on <code>/usr/share/elasticsearch/config/</code>, will replace everything inside that path and place the config file from the configmap. If that causes an issue, you might want to mount it at another location and then copy it.</li>
</ul>
