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/cjob

But as of Kubernetes 1.18, I get below Error

error: cannot get the logs from *v1beta1.CronJob: selector for *v1beta1.CronJob not implemented

let’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 cluster

Now 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 60s

In 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 cluster

But 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 cj2

Then 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 52s

Now 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.creationTimestamp

This 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"
https://medium.com/@pranay.shah/how-to-get-logs-from-cron-job-in-kubernetes-last-completed-job-7957327c7e76

댓글

이 블로그의 인기 게시물

Using the MinIO API via curl

How to split a list into chunks of 100 items in JavaScript, 자바스크립트 리스트 쪼개기

HTML Inline divisions at one row by Tailwind

Boilerplate for typescript server programing

가속도 & 속도

Gradle multi-module project

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

CDPEvents in puppeteer

Sparse encoder

Reactjs datetime range picker