<p>I have a CronJob which runs every 10 minutes and executes some command.</p>
<p>It's failing for some reason and I want to <code>exec</code> into it to investigate and fix it (There's a 1 time command I need to run to fix a shared volume*).</p>
<p>The issue is when I try to run <code>exec</code> I get this error, which is expected:</p>
<pre><code>error: cannot exec into a container in a completed pod; current phase is Failed
</code></pre>
<p>I would like to create a new pod from the job definition and run a custom command on that (e.g. <code>tail -f</code>) so that it runs without crashing and I can <code>exec</code> into it to investigate and fix the issue.</p>
<p>I've been struggling to do this and have only found 2 solutions which both seem a bit hacky (I've used both and they do work, but since I'm still developing the feature I've had to reset a few times)</p>
<ol>
<li>I change the command on the k8s YAML file to <code>tail -f</code> then update the Helm repo and <code>exec</code> on the new container. Fix the issue and revert back.</li>
<li>Copy the job to a new <code>Pod</code> YAML file in a directory outside of the Helm repo, with <code>tail -f</code>. Create it with the <code>kubectl apply -f</code> command. Then I can <code>exec</code> on it, do what I need and delete the pod.</li>
</ol>
<p>The issue with the first is that I change the Helm repo. The second requires some duplication and adaptation of code, but it's not too bad.</p>
<p>What I would like is a <code>kubectl</code> command I can run to do this. Kind of like how you can create a job from a CronJob:</p>
<pre><code>kubectl create job --from=cronjob/myjob myjob-manual
</code></pre>
<p>If I could do this to create a pod, or to create a job with a command which never finishes (like <code>tail -f</code>) it would solve my problem.</p>
<p>*The command I need to run is to pass in some TOTP credentials as a 1 time task to login to a service. The cookies to stay logged in will then exist on the shared volume so I won't have to do this again. I don't want to pass in the TOTP master key as a secret and add logic to interpret it either. So the most simple solution is to set up this service and once in a while I <code>exec</code> into the pod and login using the TOTP value again.</p>
<p>One more note. This is for a personal project and a tool I use for my own use. It's not a critical service I am offering to someone else so I don't mind if something goes wrong once in a while and I need to intervene.</p>

<p>Looked into this question more, your <strong>option 2 is the most viable solution</strong>.</p>
<p>Adding a sidecar container - it's the same as option 1, but even more difficult/time consuming.</p>
<p>As mentioned in comments, there are no options for direct imperative pod creation from <code>job</code>/<code>cronjob</code>. Available options can be checked for <code>kubectl</code>:</p>
<ul>
<li><a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#create" rel="nofollow noreferrer"><code>kubectl create</code></a></li>
<li><a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run" rel="nofollow noreferrer"><code>kubectl run</code></a></li>
</ul>
<p>Also tried out of the interest (logic is to run the command from <code>cronjob</code> and then continue with specified command), but did not work out:</p>
<pre><code>$ kubectl create job --from=cronjob/test-cronjob manual-job -- tail -f
error: cannot specify --from and command
</code></pre>
