K8s tips: Manually launch a Job from CronJob

You just applied your new CronJob to Kubernetes and now would you like to try it?

You need to run your CronJob one time out of schedule?

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: my-cj
spec:
schedule: "0 08 * * SAT"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-cj-pod
image: "my-cj-image:latest"

The first thing that could come to your mind is to temporarily change the schedule to make it run ASAP, but there is a smarter way to create a Job defined in a CronJob.

Solution

Kubectl v1.10.1+ introduced the kubectl create job command with a --from option that actually accepts only a CronJob.

1
kubectl -n my-ns create job --from=cronjob/my-cj my-job-name

This command will instantly create a Job named my-job-name from the jobTemplate of the CronJob named my-cj.