codeaprendiz
codeaprendiz

Reputation: 3205

Check logs for a Kubernetes resource CronJob

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

Questions:

  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?

Upvotes: 2

Views: 5718

Answers (2)

Eduard Florinescu
Eduard Florinescu

Reputation: 17551

In case you create job from cronjob it works like this: kubectl -n "namespace" logs jobs.batch/<resource_name> --tail 4

Upvotes: 0

Arghya Sadhu
Arghya Sadhu

Reputation: 44697

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.

Upvotes: 4

Related Questions