How to run a Kubernetes cron job manually as a one time job

Craig Newton
The Startup
Published in
2 min readMay 28, 2020

--

Photo by Thanos Pal on Unsplash

I recently had to create a cron job in Kubernetes that would run a data archive script that would archive the immutable MySQL table data for transactions for the previous day and upload the result to AWS S3.

The job failed today as there was a network connectivity issue to the database and my code tried 3 times and then gave up for today. I then needed to have the job execute so I could have it process yesterdays data and archive it. So I did what I normally do, I take the YAML for the cronjob and modify it to become a standard type: Job and then I just apply the YAML to run the job and once completed I just run kubectl delete -f my-onetime-job.yaml.

It works but seems a bit tedious. Recently I discovered how to do this without needing a YAML file, I could just run it from the command line directly.

Here is the command I used:

kubectl create job --from=cronjob/s3-transactions-data-archiver s3-transactions-data-archiver-otj

Where with placeholders:

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

This will create a job and run it, you can verify it’s completion by running the following command and inspecting the output:

kubectl get jobs

The output will show you the jobs you currently have running and my job for s3-transactions-data-archiver-otj was listed there as completed.

To cleanup after all we now need to do is delete the job by running the command:

kubectl delete job s3-transactions-data-archiver-otj

Where with placeholders:

kubectl delete job <name of job>

This is a great way to quickly run a failed cron job, or even if you create your cron job and it does not immediately run, you can trigger this manual run.

Hope this helps you and thank you for reading this snippet.

More Resources

Want to learn more about Kubernetes. Please check out this great book that I helped review:

The Kubernetes Workshop
The Kubernetes Workshop

https://www.packtpub.com/product/the-kubernetes-workshop/9781838820756

--

--