Sub topic 11
Inspecting and Troubleshooting Runs
This chapter assumes you can apply YAML manifests with kubectl, read Pod logs, and use labels to select objects. It also assumes you know the Job and CronJob fields from earlier chapters: backoffLimit, activeDeadlineSeconds, concurrencyPolicy, startingDeadlineSeconds, and suspend.
The ownership chain
A CronJob creates Jobs, and a Job creates Pods. When something goes wrong, you walk this chain from the top down and stop at the first object that looks wrong.
Inspect a Job
Start with the Job's status and events.
kubectl get job hello
kubectl describe job hello
kubectl get pods --selector=job-name=hello
kubectl logs job/hellokubectl get job shows how many completions have succeeded, such as 0/1. kubectl describe job shows the conditions and the Events section at the bottom. The Pods carry a job-name label, so the selector finds every Pod the Job has created. kubectl logs job/hello prints the logs of one Pod from that Job. If a Job has several Pods, use kubectl logs <pod-name> for a specific one.
To wait for a result in a script, use kubectl wait:
kubectl wait --for=condition=complete job/hello --timeout=60sCommon failures
| Symptom | Likely cause | What to check |
|---|---|---|
| Job shows `BackoffLimitExceeded` | Pods failed more than `backoffLimit` times | `kubectl logs` on a failed Pod, then the exit code in `kubectl describe pod` |
| Job shows `DeadlineExceeded` | The Job ran longer than `activeDeadlineSeconds` | Raise the limit or speed up the task |
| Pod is `Pending` | No node fits the requests, or a volume cannot mount | Events in `kubectl describe pod` |
| Pod is `ImagePullBackOff` | Wrong image name or missing pull credentials | Image name and tag, `imagePullSecrets` |
| Pods keep appearing and failing | The retry loop is working as designed | Fix the cause, since each retry creates a new Pod |
| Job seems stuck with no Pods | The Job is suspended | `spec.suspend` in `kubectl get job -o yaml` |
| Pods are gone after a Job finishes | `ttlSecondsAfterFinished` removed them | Set a longer TTL, or ship logs elsewhere |
With restartPolicy: Never, each failed attempt leaves a separate Pod, so you can read the logs of every attempt. With OnFailure, the same Pod restarts its container, and you read earlier output with kubectl logs --previous.
Inspect a CronJob
kubectl get cronjob nightly-report
kubectl describe cronjob nightly-report
kubectl get jobsIn kubectl get cronjob, check three columns. SUSPEND should be False. ACTIVE shows how many Jobs are running now. LAST SCHEDULE shows when the controller last created a Job. The Events section of describe records each Job creation and any problem.
To test the Job template without waiting for the schedule, start a one-off run:
kubectl create job nightly-report-manual --from=cronjob/nightly-reportIf the manual run works but scheduled runs do not, the problem is in the schedule, not in the task.
Why a CronJob did not run
- It is suspended. Check
SUSPENDinkubectl get cronjob. - The schedule or time zone is not what you think. Compare
LAST SCHEDULEwith the cron expression andtimeZone. concurrencyPolicy: Forbidskipped the run. A previous Job was still active. Look atACTIVEand at older Jobs that never finished.- The run missed
startingDeadlineSeconds. The controller was down or the cluster was busy, and the window passed. A CronJob also stops scheduling if it misses too many runs in a row, anddescribeshows an event explaining this. - History limits removed the evidence. Old Jobs are deleted according to
successfulJobsHistoryLimitandfailedJobsHistoryLimit. Keep at least one failed Job so you can inspect it.
Read events in order
Events expire after about an hour by default. List them for a namespace in time order:
kubectl get events --sort-by=.metadata.creationTimestampExercise
A nightly CronJob has not produced a new Job for two days. kubectl get cronjob shows SUSPEND as False, and a Job from three days ago is still listed under ACTIVE. The policy is concurrencyPolicy: Forbid. What is the most likely cause, and what do you check next?
Show answer
The old Job never finished, so Forbid skips every new run. Run kubectl describe job and kubectl get pods for that Job to see whether its Pod is hung or Pending. Fix the cause, or delete the stuck Job. To prevent a repeat, set activeDeadlineSeconds on the Job template so a hung run fails instead of blocking the schedule.