How can I trigger a Kubernetes Scheduled Job manually?

Scheduled TasksKubernetesGoogle Kubernetes-Engine

Scheduled Tasks Problem Overview


I've created a Kubernetes Scheduled Job, which runs twice a day according to its schedule. However, I would like to trigger it manually for testing purposes. How can I do this?

Scheduled Tasks Solutions


Solution 1 - Scheduled Tasks

The issue #47538 that @jdf mentioned is now closed and this is now possible. The original implementation can be found here but the syntax has changed.

With kubectl v1.10.1+ the command is:

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

It seems to be backwardly compatible with older clusters as it worked for me on v0.8.x.

Solution 2 - Scheduled Tasks

You can create a simple job based on your ScheduledJob. If you already run a ScheduledJob, there are jobs in history.

kubectl get jobs

NAME               DESIRED   SUCCESSFUL   AGE
hello-1477281595   1         1            11m
hello-1553106750   1         1            12m
hello-1553237822   1         1            9m

Export one of these jobs:

kubectl get job hello-1477281595 -o yaml > my_job.yaml

Then edit the yaml a little bit, erasing some unnecessary fields and run it manually:

kubectl create -f my_job.yaml
kubectl delete -f my_job.yaml

Solution 3 - Scheduled Tasks

Unfortunately, none of the example syntaxes above works in Google Kubernetes Engine (GCP). Also, the GKE docs themselves are wrong.

In Kubernetes 1.10.6.gke-2, the working syntax is:

kubectl create job <your-new-job-name> --from=cronjob/<name-of-deployed-cron-job> -n <target namespace>

Solution 4 - Scheduled Tasks

EDIT - July 2018: see @pedro_sland's answer as this feature has now been implemented

My original answer below will remain correct for older versions of kubectl less than v1.10.1

========================================================================

Aside from creating a new job (as the other answers have suggested), there is no current way to do this. It is a feature request in with kubernetes now that can be tracked here: https://github.com/kubernetes/kubernetes/issues/47538

Solution 5 - Scheduled Tasks

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

you can use the to delete job execution at any time kubectl delete job <job-name> -n <namespace>

if you want to see the list of corn jobs available use kubectl get cronjobs -n <namespace>

Solution 6 - Scheduled Tasks

There is an option to trigger the cron job manually whithin this tab in k8s dashboard

See image

Solution 7 - Scheduled Tasks

I've created a small cmd utility for convenience to do just that and also suspend and unsuspend cronjobs.

https://github.com/iJanki/kubecron

Solution 8 - Scheduled Tasks

If you want to test the job, create a Job config from your Cron Job (ScheduledJob) config and run it manually using the following command:

kubectl create -f ./job.yaml

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionaknuds1View Question on Stackoverflow
Solution 1 - Scheduled Taskspedro_slandView Answer on Stackoverflow
Solution 2 - Scheduled TasksCamilView Answer on Stackoverflow
Solution 3 - Scheduled TasksJoseph LustView Answer on Stackoverflow
Solution 4 - Scheduled TasksjdfView Answer on Stackoverflow
Solution 5 - Scheduled TasksredView Answer on Stackoverflow
Solution 6 - Scheduled Tasksborja garciaView Answer on Stackoverflow
Solution 7 - Scheduled TasksiJankiView Answer on Stackoverflow
Solution 8 - Scheduled TasksKamranView Answer on Stackoverflow