<p>I'm using a Kubernetes job to run a test; a shell script runs the job and has setup/teardown logic. The job has</p>
<ul>
<li><code>restartPolicy: Never</code></li>
<li><code>backoffLimit: 2</code></li>
</ul>
<p>which means that if the job fails, it'll make a new pod and try again once.</p>
<p>After the job completes, I'd like to dump the logs from all pods in the job. But when I do</p>
<pre><code>kubectl logs job/my-test
</code></pre>
<p>I only get logs from one of the pods, prefixed with something like <code>Found 2 pods, using pod/my-test-ntb4w</code>.</p>
<p>The <code>--all-containers=true</code> flag doesn't give me logs from all pods.</p>
<p>How can I get logs from all pods in a job, in a shell script?</p>

<p>As an reference please take a look at <code>kubectl logs</code> --help:</p>
<pre><code>   -- kubectl logs job/my-job
    # Return snapshot logs from first container of a job named hello
      kubectl logs job/hello -- will provide output only for one pod/container
    
    -- while using (either label or selector) it gives you more flexible way to deal with your resources
    # -l, --selector='': Selector (label query) to filter on.
</code></pre>
<p>Alternatively you can add custom <em><strong>labels</strong></em> or use an _<em><strong>label/selector</strong></em> from job description:</p>
<pre><code>labels:
      controller-uid: 55d965d0-0016-42ba-b4f5-120c1a78798b
      job-name: pi
</code></pre>
<hr />
<p>You can find fairly similar case in the docs: <a href="https://kubernetes.io/docs/tasks/job/parallel-processing-expansion/#create-jobs-from-the-manifests" rel="nofollow noreferrer">checking on the output of all jobs at once</a> and <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="nofollow noreferrer">Running an example Job</a>.</p>
<p>While using bash you can try also:</p>
<pre><code>pods=$(kubectl get pods --selector=job-name=pi --output=jsonpath='{.items[*].metadata.name}')

for pod in $pods ; do kubectl logs $pod ; done 
</code></pre>
<hr />
<p>Very useful command for working with k8s objects:</p>
<pre><code>kubectl get pods,jobs --show-labels

NAME           READY   STATUS      RESTARTS   AGE   LABELS
pod/pi-25vcd   0/1     Completed   0          97s   controller-uid=55d965d0-0016-42ba-b4f5-120c1a78798b,job-name=pi
</code></pre>
