<p>I have a deployment YAML file and I have 4 different containers in a single pod.
I want to do this when one of the containers is crashes then I want to restart the pod.</p>
<p>Right now when the container is crashed, only that container restart but I want to restart all containers</p>

Kufu
<p>A Deployment can't do this but you could abuse a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#job-v1-batch" rel="nofollow noreferrer">Job</a> with a <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy" rel="nofollow noreferrer">restartPolicy</a> of Never to achieve it. But it's a hack and definitely <strong>violates best practices</strong>. It would require each container to artificially fail if any other container failed:</p>
<ol>
<li>Use a Job with a restartPolicy of Never instead of a Deployment</li>
<li>Make each container regularly write some &quot;I'm alive&quot; message to some shared location, e.g. an emptyDir volume that's shared between all the containers in the Pod</li>
<li>Make each container monitor the &quot;I'm alive&quot; messages of all other containers, and when one is missing (which means that this container crashed), then crash this container on purpose (e.g. <code>exit 1</code>)</li>
</ol>
<p>The effect of this is that when one container crashes, then all containers crash. When all containers crashed, the Pod is declared as <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase" rel="nofollow noreferrer">Failed</a> and the Job Controller restarts the entire Pod.</p>
<p>Note however that each restart counts against the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#jobspec-v1-batch" rel="nofollow noreferrer">backoffLimit</a> of the Job, so when this limit is reached, the Job is declared as failed and Pods won't be restarted anymore. Also note that this works only if the Pod template in the Job has a restartPolicy of Never, because with OnFailure (see <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy" rel="nofollow noreferrer">docs</a>), failed containers are restarted immediately and each container restart counts against the Job's backoffLimit (see <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/#job-termination-and-cleanup" rel="nofollow noreferrer">docs</a>).</p>
<p>As mentioned, it's an abuse of what a Job is supposed to do and therefore I would <strong>not recommend</strong> to use this in production or for any serious workloads. But it would probably allow you to do what you want for playing around.</p>
