[SOLVED]: Kubectl Create job from cronjob Kubernetes

  • Jobs in kubernetes are pods which will be created until a specified number of pods successfully terminate. Jobs are most commonly used for one time executable operation like bootstrapping or applying configuration once microservice is up etc.
  • CronJobs in kubernetes are almost similar to Jobs with one difference, cronjobs can be scheduled and run on specific intervals. Unlike jobs who will execute as soon as the job manifest file is applied. some of the commonly used cronjob operations are log forwarding, log rotation, backup etc.
  • Since Cronjobs run on scheduled time, (once in a month, daily midnight). It is imperative to test these jobs before it is released to production . For testing in ideal scenario, we have to wait till the cronjob triggers by its own when it  reaches its scheduled time.
  • In this Article, we will see how Kubectl Create job from cronjob so that we can test the cronjob without waiting for the schedule time

Create Cronjob in Kubernetes

  • Cronjob in Kubernetes can be created by declarative and imperative ways.
  • Cronjob will require cron schedule, and it follows unix cron format.

Create Cronjob: Imperative way

Below kubectl command syntax can be used for creating cronjob in imperative way

kubectl create cronjob <NAME>  --image=<image> --schedule="<cron schedule>" -n <namespace> -- '<command>'

Below example shows cronjob created from centos image, which will list the directory contents every minute

[root@c1 ~]# kubectl create cronjob linux-data-hub-log-list --image=centos --schedule="*/1 * * * *" -n ldh -- 'ls' 
cronjob.batch/linux-data-hub-log-list created

Since the job is scheduled to run on every minute, we can easily verify the changes

[root@c1 ~]# kubectl get pods -n ldh
linux-data-hub-log-list-27862963-2f9c8 0/1 Completed 0 2m2s
linux-data-hub-log-list-27862964-j5q8h 0/1 Completed 0 62s

Create CronJob: Declarative way

Below yaml manifest can be used to create a cronjob

apiVersion: batch/v1
kind: CronJob
  generation: 1
  name: linux-data-hub-log-list
  namespace: ldh
  concurrencyPolicy: Allow
  failedJobsHistoryLimit: 1
      name: linux-data-hub-log-list
          creationTimestamp: null
          - command:
            - ls
            image: centos
            imagePullPolicy: Always
            name: linux-data-hub-log-list
            resources: {}
            terminationMessagePath: /dev/termination-log
            terminationMessagePolicy: File
          dnsPolicy: ClusterFirst
          restartPolicy: OnFailure
          schedulerName: default-scheduler
          securityContext: {}
          terminationGracePeriodSeconds: 30
  schedule: '*/1 * * * *'
  successfulJobsHistoryLimit: 3
  suspend: false
[root@c1 ~]# kubectl apply -f cronjob.yaml 
cronjob.batch/linux-data-hub-log-list created

Kubectl Create job from cronjob

  • We have seen how to create a cronjob. For testing a cronjob, we cant wait for the job to start according to the schedule time, as some of the popular schedule is midnight, once in a week, once in a year etc.
  • In order to avoid this difficulty in testing a cronjob, we can create a job from the cronjob, and test the job.
  • This job testing can be considered as testing cronjob. As the difference between cronjob and job is the cronscheduler.

Below syntax can be used to create job from cronjob

kubectl create job <job-name> --from=cronjob/<name-of-cron-job> -n <namespace>

Below shows the job creation from the cronjob which we have created above in the article.

[root@c1 ~]# kubectl create job log-crontest -n ldh --from=cronjob/linux-data-hub-log-list
job.batch/log-crontest created
[root@c1 ~]# kubectl get jobs -n ldh
log-crontest 0/1 7s 7s

Additional way: Kubectl Create job from cronjob

Inorder cover the topic of job creation from cronjob, we will discuss one more unorthodox way of creating job. One drawback with this approach is, one job should have been executed from the cronjob and the job is available in the job history  And kubectl get jobs should list this job.

  • Below code snippet shows the jobs created from the example cronjob.
[root@c1 ~]# kubectl get pods -n ldh
linux-data-hub-log-list-27864821-qn5h5 0/1 Completed 0 2m41s
linux-data-hub-log-list-27864822-xrspp 0/1 Completed 0 101s
  • Create manifest file from one of the completed jobs.
kubectl get jobs -n ldh linux-data-hub-log-list-27864821-qn5h5 -o yaml > job.yaml
  • create the kubernetes object after removing additional entries like controller-uid, ownerReferences, resourceVersion from the created manifest file
[root@c1 ~]# kubectl apply -f job.yaml -n ldh 
job.batch/linux-data-hub-log-list-27864852 created

Also Read:

[SOLVED]: Execute Commands on Kubernetes Pods with root access

Search on LinuxDataHub

Leave a Comment