<p>When k8s job keeps recreating pods within 10 seconds, what is the best way to get the related logs and find out the root cause? I tried to use</p>
<pre><code>kubectl logs &lt;pod&gt; -n &lt;namespace&gt;
</code></pre>
<p>and always got <em><strong>Error from server (NotFound): pods &quot;XXXX&quot; not found</strong></em> as it already got deleted and a new pod got created in the meantime.</p>

<p>If the pod &quot;restarting/crashloop&quot; then you can get logs from the <code>previous</code> run.</p>
<pre><code>kubectl logs -n &lt;namespace&gt; &lt;pod&gt; --previous
</code></pre>
<p>This will show you the logs of the last run of the pod before it crashed. It is a handy feature in case you want to figure out why the pod crashed in the first place.</p>
<p>For the long term you should consider some logging solution as the pod should be treated as a disposable object and should not rely on this.</p>
<p>One of best tool that I used so far without any issue <a href="https://docs.datadoghq.com/containers/kubernetes/log/?tab=daemonset" rel="nofollow noreferrer">Kubernetes Log collection using datadog</a></p>
