Check logs for a kubernetes resource CronJob

5/4/2020

I created a CronJob resource in kubernetes.

I want to check the logs to validate that my crons are run. But not able to find any way to do that. I have gone through the commands but looks like all are for pod resource type.

Also tried following

$ kubectl logs cronjob/<resource_name>
error: cannot get the logs from *v1beta1.CronJob: selector for *v1beta1.CronJob not implemented

Question 1) How to check logs of CronJob Resource type?

2) If i want this resource to be in specific namespace, how to implement that same?

Thanks in advance

-- codeaprendiz
kubernetes

1 Answer

5/4/2020

You need to check the logs of the pods which are created by the cronjob. The pods will be in completed state but you can check logs.

# here  you can get the pod_name from the stdout of the cmd `kubectl get pods`
$ kubectl logs -f -n default <pod_name>

For creating a cronjob in a namespace just add namespace in metadata section. The pods will created in that namespace.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
  namespace: default
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

Ideally you should be sending the logs to a log aggregator system such as ELK or Splunk.

-- Arghya Sadhu
Source: StackOverflow