62

I've created a Kubernetes job that has now failed. Where can I find the logs to this job?

I'm not sure how to find the associated pod (I assume once the job fails it deletes the pod)?

Running kubectl describe job does not seem to show any relevant information:

Name:           app-raiden-migration-12-19-58-21-11-2018
Namespace:      localdev
Selector:       controller-uid=c2fd06be-ed87-11e8-8782-080027eeb8a0
Labels:         jobType=database-migration
Annotations:    kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"batch/v1","kind":"Job","metadata":{"annotations":{},"labels":{"jobType":"database-migration"},"name":"app-raiden-migration-12-19-58-21-1...
Parallelism:    1
Completions:    1
Start Time:     Wed, 21 Nov 2018 12:19:58 +0000
Pods Statuses:  0 Running / 0 Succeeded / 1 Failed
Pod Template:
  Labels:  controller-uid=c2fd06be-ed87-11e8-8782-080027eeb8a0
           job-name=app-raiden-migration-12-19-58-21-11-2018
  Containers:
   app:
    Image:  pp3-raiden-app:latest
    Port:   <none>
    Command:
      php
      artisan
      migrate
    Environment:
      DB_HOST:        local-mysql
      DB_PORT:        3306
      DB_DATABASE:    raiden
      DB_USERNAME:    <set to the key 'username' in secret 'cloudsql-db-credentials'>  Optional: false
      DB_PASSWORD:    <set to the key 'password' in secret 'cloudsql-db-credentials'>  Optional: false
      LOG_CHANNEL:    stderr
      APP_NAME:       Laravel
      APP_KEY:        ABCDEF123ERD456EABCDEF123ERD456E
      APP_URL:        http://192.168.99.100
      OAUTH_PRIVATE:  <set to the key 'oauth_private.key' in secret 'laravel-oauth'>  Optional: false
      OAUTH_PUBLIC:   <set to the key 'oauth_public.key' in secret 'laravel-oauth'>   Optional: false
    Mounts:           <none>
  Volumes:            <none>
Events:
  Type     Reason                Age   From            Message
  ----     ------                ----  ----            -------
  Normal   SuccessfulCreate      2m    job-controller  Created pod: app-raiden-migration-12-19-58-21-11-2018-pwnjn
  Warning  BackoffLimitExceeded  2m    job-controller  Job has reach the specified backoff limit
2
  • It seems your pod hasn't started yet. It's failing to start. Nov 21, 2018 at 13:07
  • 1
    From Kubernetes documentation: "Once .spec.backoffLimit has been reached the Job (restartPolicy=OnFailure) will be marked as failed and any running Pods will be terminated.". The pods would have been deleted in this case.
    – Hem
    Jul 14, 2020 at 15:07

4 Answers 4

63

One other approach:

  • kubectl describe job $JOB
  • Pod name is shown under "Events"
  • kubectl logs $POD
2
  • 25
    If activeDeadlineSeconds is exceeded, then the pod is deleted and I could find no way to look at the logs. Events will say "<none>"
    – Balazs Rau
    Mar 25, 2020 at 15:14
  • 1
    If you cannot find the pod name under "Events" it's probably because of successfulJobsHistoryLimit: <num> configuration within CronJob Jul 23, 2020 at 11:42
37

You can also execute

kubectl logs job.batch/your-job-name

In case you job has more than one pod, you will see a message similar to:

Found X pods, using pod/your-job-name-xxxxx

(Tested with kubectl version = 1.21.2)

3
  • By far the easiest answer for a batch job, since we do not have to look up the pods for a job Aug 3, 2021 at 19:26
  • For me it returned error: timed out waiting for the condition Sep 20 at 23:50
  • This should be the correct answer unless you have an older version of kubectl. Sep 23 at 12:12
16

Use this command to show all pods, even failed ones:

kubectl get pods -A

And then a pod will be shown like below:

app-raiden-migration-12-19-58-21-11-2018-pwnjn   0/1       Error     0          6m

Then use:

kubectl logs lighthouse-timer-1553800620-pwssv
2
  • 4
    Flag --show-all has been deprecated, will be removed in an upcoming release. Any idea what will be used to replace it? Apr 29, 2019 at 11:08
  • 4
    -A just means all namespaces. It's not going to show the pods related to a failed job.
    – spinkus
    Sep 10, 2021 at 23:25
3

You can get the pods of this job by running:

kubectl get pods --selector=job-name=app-raiden-migration-12-19-58-21-11-2018

but in this case i think you won't find any pods because no pod is created, and as mentioned in this link: Job Termination and Cleanup, pods are not deleted after jobs completion. But still i don't know how you can find out why that pod failed to run (but if there is a pod and you could find it, you can find the logs of that pod). I had the same mistake and i ran the job again and job worked successfully.

EDIT:

I just see the events of your job, your job has made pod and you can see the logs of your pod by your pod name as I mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.