kubernetes cronjob logs
How to get logs from Cron Job in kubernetes (last completed job)
Question is how do I get logs from my Crone job? Ideally, I should be able to do this :
If I have Cron Job “cjob” in namespace “cj2” I can run a command like this
kubectl logs -n cj2 cronjob.batch/cjobBut as of Kubernetes 1.18, I get below Error
error: cannot get the logs from *v1beta1.CronJob: selector for *v1beta1.CronJob not implementedlet’s do the hard way
This is Cron job Yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: cj2
spec: {}
status: {}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
creationTimestamp: null
name: cjob
namespace: cj2
spec:
jobTemplate:
metadata:
creationTimestamp: null
name: cjob
spec:
template:
metadata:
spec:
containers:
- image: busybox
name: cjob
command:
resources: {}
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
schedule: "*/1 * * * *"
status: {}It basically prints timestamp and ‘Hello from kubernetes cluster’ every 1 minute
Thu Jul 9 20:39:12 UTC 2020
Hello from the Kubernetes clusterNow the hard way is to get a log from Cron job — for that first, we need to get all pods ran for this job and pick the pod that ran the last and get logs
$ kubectl get pods -n cj2
NAME READY STATUS RESTARTS AGE
cjob-1594327920-6n4fk 0/1 Completed 0 3m
cjob-1594327980-jt8xp 0/1 Completed 0 2m
cjob-1594328040-rd2mz 0/1 Completed 0 60sIn the above case, it is the latest pod is cjob-1594328040-rd2mz
$ kubectl logs -n cj2 cjob-1594328040-rd2mz
Thu Jul 9 20:54:03 UTC 2020
Hello from the Kubernetes clusterBut that is a really hard way (above all can’t be automated) and I need to run multiple commands to get that — what if I want to get this all one command?
Proposed (Easy way)
First, we add labels to each pod that Cron job creates like this :
apiVersion: batch/v1beta1
kind: CronJob
metadata:
creationTimestamp: null
name: cjob
namespace: cj2
spec:
jobTemplate:
metadata:
creationTimestamp: null
name: cjob
spec:
template:
metadata:
labels:
cj2job: cronjob
spec:
containers:
- image: busybox
name: cjob
command:
resources: {}
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
schedule: "*/1 * * * *"
status: {}If you feel adventitious you can use below patch command to patch existing Cron job — but best apporach will be to update origianl yaml file
kubectl patch cronjobs.batch cjob -p '{"spec":{"jobTemplate":{"spec": {"template":{"metadata":{"labels":{"cj2job": "cronjob"}}}}}}}}}' -n cj2Then now with the above modification, it is easy to get all pods that are created by this Cron job
$ kubectl get pods -n cj2 -l cj2job
NAME READY STATUS RESTARTS AGE
cjob-1594328160-btbzx 0/1 Completed 0 2m52s
cjob-1594328220-w8zv2 0/1 Completed 0 112s
cjob-1594328280-8xd9f 0/1 Completed 0 52sNow we just need last pod’s name that was completed we can do this via this JSON path trick
$ kubectl get pods -n cj2 -l cj2job --sort-by=.metadata.creationTimestamp -o 'jsonpath={.items[-1].metadata.name}'
cjob-1594328400-cwq5l
$There are two things to pay attention
--sort-by=.metadata.creationTimestampThis gives us pods sorted by timestamp — the latest pod to complete will be at the end of the list.
We need that pod’s name so we can use this JSON path trick
-o 'jsonpath={.items[-1].metadata.name}'This will give us the last in list Pod’s name
Now we can feed this value to ‘kubectl logs’ command get our latest logs of Cron Job
$ kubectl logs -n cj2 $(kubectl get pods -n cj2 -l cj2job --sort-by=.metadata.creationTimestamp -o 'jsonpath={.items[-1].metadata.name}')Thu Jul 9 21:03:05 UTC 2020
Hello from the Kubernetes cluster
An alternative way to run above command is to use Xargs so you can add a watch on it
watch "kubectl get pods -n cj2 -l cj2job --sort-by=.metadata.creationTimestamp -o 'jsonpath={.items[-1].metadata.name}' | xargs kubectl logs -n cj2"
댓글
댓글 쓰기