Analyzing Kubernetes Job Logs
Being able to view and analyze the logs from your Kubernetes jobs is essential for debugging and understanding job behavior. In this step, we'll explore how to access and analyze logs from the jobs we've created.
Getting Pod Names for Our Jobs
Before we can view logs, we need to know the names of the pods created by our jobs. Each job creates one or more pods with names that include the job name and a random suffix.
Let's list all pods related to our jobs:
kubectl get pods --show-labels
This will show you all pods along with their labels. Look for pods with labels like job-name=hello-job
or job-name=long-job
.
Alternatively, you can filter pods by job name:
kubectl get pods -l job-name=hello-job
This will show only pods that belong to the hello-job
job.
Viewing Job Logs
Now that we know our pod names, we can view the logs for our jobs. Use the following command, replacing <pod-name>
with the actual name of your pod:
kubectl logs <pod-name>
For example, if your pod is named hello-job-abcd1
, you would run:
kubectl logs hello-job-abcd1
You should see the output:
Hello, Kubernetes!
This is the message that our job was programmed to output.
Let's also check the logs of our longer job. First, find the pod name:
kubectl get pods -l job-name=long-job
Then view its logs:
kubectl logs <long-job-pod-name>
You should see:
Starting long job...
Long job completed!
Viewing Logs of Completed Jobs
One of the advantages of Kubernetes jobs is that you can view the logs even after the job has completed. This is very useful for debugging and auditing purposes.
Let's create a job that will fail, so we can see how to debug it:
cd ~/project
nano failed-job.yaml
Copy and paste the following YAML content:
apiVersion: batch/v1
kind: Job
metadata:
name: failed-job
spec:
template:
spec:
containers:
- name: failed
image: busybox:1.28
command: ["sh", "-c", 'echo "Attempting task..." && exit 1']
restartPolicy: Never
backoffLimit: 2
This job will always exit with status code 1, which indicates failure. Save the file and exit nano, then create the job:
kubectl apply -f failed-job.yaml
Wait a few moments for the job to attempt and fail a few times (up to the backoff limit). Then check its status:
kubectl get jobs failed-job
You should see that it shows 0/1
completions and has reached the completion deadline.
Now let's examine what went wrong by checking the logs of the failed pods:
kubectl get pods -l job-name=failed-job
You'll see several pods, all in the Error
state. Choose one and view its logs:
kubectl logs <failed-pod-name>
You should see:
Attempting task...
The pod logs show that the task started but then exited with an error code. This information is crucial for debugging job failures.
Following Logs in Real-Time
If you want to follow the logs of a running job in real-time, you can use the -f
flag:
kubectl logs -f <pod-name>
This is particularly useful for longer-running jobs where you want to see the output as it happens.
Let's create another long-running job to demonstrate:
cd ~/project
nano counter-job.yaml
Copy and paste the following YAML content:
apiVersion: batch/v1
kind: Job
metadata:
name: counter-job
spec:
template:
spec:
containers:
- name: counter
image: busybox:1.28
command:
[
"sh",
"-c",
'for i in $(seq 1 5); do echo "Count: $i"; sleep 5; done'
]
restartPolicy: Never
Save the file and exit nano, then create the job:
kubectl apply -f counter-job.yaml
Now let's follow its logs. First, find the pod name:
kubectl get pods -l job-name=counter-job
Then follow its logs:
kubectl logs -f <counter-job-pod-name>
You'll see the count incrementing every 5 seconds:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Press Ctrl+C
to stop following the logs.
By understanding how to access and analyze logs, you can effectively troubleshoot and debug your Kubernetes jobs.