<p>I was trying to make file before application gets up in kubernetes cluster with initcontainers,</p>
<p>But when i am setting up the pod.yaml and trying to apply it with &quot;kubectl apply -f pod.yaml&quot; it throws below error
<a href="https://i.stack.imgur.com/KV1mO.png" rel="nofollow noreferrer">error-image</a></p>

Jayesh Desai
<p>Like the error says, you cannot update a Pod adding or removing containers. To quote the documentation ( <a href="https://kubernetes.io/docs/concepts/workloads/pods/#pod-update-and-replacement" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/#pod-update-and-replacement</a> )</p>
<blockquote>
<p>Kubernetes doesn't prevent you from managing Pods directly. It is
possible to update some fields of a running Pod, in place. However,
Pod update operations like patch, and replace have some limitations</p>
</blockquote>
<p>This is because usually, you don't create Pods directly, instead you use Deployments, Jobs, StatefulSets (and more) which are high-level resources that defines Pods templates. When you modify the template, Kubernetes simply delete the old Pod and then schedule the new version.</p>
<p>In your case:</p>
<ul>
<li>you could delete the pod first, then create it again with the new specs you defined. But take into consideration that the Pod <strong>may be scheduled on a different node</strong> of the cluster (if you have more than one) and that <strong>may have a different IP Address</strong> as Pods are disposable entities.</li>
<li>Change your definition with a slightly more complex one, a Deployment ( <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/deployment/</a> ) which can be changed as desired and, each time you'll make a change to its definition, the old Pod will be removed and a new one will be scheduled.</li>
</ul>
<hr />
<p>From the spec of your Pod, I see that you are using a volume to share data between the init container and the main container. This is the optimal way but you don't necessarily need to use a hostPath. If the only needs for the volume is to share data between init container and other containers, you can simply use <code>emptyDir</code> type, which acts as a temporary volume that can be shared between containers and that will be cleaned up when the Pod is removed from the cluster for any reason.</p>
<p>You can check the documentation here: <a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/volumes/#emptydir</a></p>
