How to trigger a Kubernetes cronjob manually

How to trigger a Kubernetes cronjob manually

What should you do when you’ve developed and installed a cron job for your Kubernetes application, and you need to test it? When writing classic cron jobs in Unix, it’s obvious how to test the job- just manually run the command specified in the cron file. However, it’s not as obvious how to do this in Kubernetes.

We encountered this problem not long ago while setting up a daily PostgreSQL backup job for one of our projects. Triggering a CronJob manually was difficult or impossible in older versions of Kubernetes, but since K8S 1.10 it can be done like this:

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

For example, if the name of your cronjob is “pgdump”, then you might run:

kubectl create job --from=cronjob/pgdump pgdump-manual-001

To see a list of cron jobs, run “kubectl get cronjob”.

The job creates a pod that runs to completion. You can review the command output by using kubectl to show pod logs.

Further reading

The official Kubernetes docs are here: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#-em-job-em-

The thread discussing this Kubernetes feature is here: https://github.com/kubernetes/kubernetes/issues/47538

Categories: DevOps

By Matt Fox

November 11, 2018

Matt Fox
Author: Matt Fox

VP TopLeft

PREVIOUS

DevOps Journey Part 1 – “Works for Me”

NEXT

Helm – Sane App Management in Kubernetes