How can I trigger a Kubernetes Scheduled Job manually?
Asked Answered
B

9

306

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?

Ballocks answered 3/11, 2016 at 12:42 Comment(0)
O
475

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> -n <namespace-name>

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

Ondometer answered 26/4, 2018 at 11:2 Comment(8)
Be sure to check your version of kubectl, as of 5/10/18 the version installed via Google Cloud SDK doesn't have this command available. gcloud components install kubectl then kubernetes.io/docs/tasks/tools/install-kubectlBeslobber
It works also in Openshift, even as old as 3.11 (just replace kubectl with oc).Saari
.. and the inverse operation is: kubectl [oc] delete job <job-name>Saari
it's not working with an error error: unknown object type *v1beta1.CronJob ` > kubectl version Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.4", GitCommit:"b695d79d4f967c403a96986f1750a35eb75e75f1", GitTreeState:"clean", BuildDate:"2021-11-17T15:48:33Z", GoVersion:"go1.16.10", Compiler:"gc", Platform:"darwin/amd64"} `Sunda
If you getting an error like this one --> "no kind "CronJob" is registered for version "batch/v1" in scheme "k8s.io/kubectl/pkg/scheme/scheme.go:28" <-- make sure your kubectl version is up to date (21+)Rollicking
does this run the job, or just create it as a thing that can be run via another command?Pulsometer
its stupid that you need delete job and recreate it to trigger 🤦‍♂️Maidenhead
This works but is it possible to modify the configuration? For example the cron's restart policy might be "OnFailure" but I want the manually triggered job to never restart.Kibler
H
29

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
Highwrought answered 4/11, 2016 at 1:51 Comment(2)
Just small addition about "erasing some unnecessary fields". Kubernetes is complaining about *uid fields and you can remove them easily by this command sed -i '/uid:.*/d' my_job.yamlWeathered
Assumption that there are already jobs in the history often does not hold.Noctambulism
E
22

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>
Enzymology answered 25/9, 2018 at 18:46 Comment(1)
awesome to troubleshoot problems with cron jobs resiliency, thanks!Blowtorch
M
16
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>

Myo answered 30/7, 2021 at 5:27 Comment(0)
D
9

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

Dhahran answered 5/9, 2017 at 19:51 Comment(0)
H
9

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

See image

Halfhearted answered 2/7, 2020 at 17:19 Comment(1)
Great! This is helpful espeically for Minikube users who want to debug and play with cronjobs (like me) :)Sclerophyll
E
7

If you can use tools beyond kubectl, the K9s CLI is a wonderful tool that has, among other features, the trigger command that allow you to trigger cronjobs.

To do that, enter the K9s interface, search for your cronjobs using the command :cronjobs, select the one you want to trigger and type t.

Under the hood it probably creates a Job using the CronJob configuration, just like this answer suggested.

Eserine answered 20/12, 2022 at 18:35 Comment(0)
I
3

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

https://github.com/iJanki/kubecron

Immortelle answered 13/12, 2017 at 15:14 Comment(0)
B
0

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
Bainite answered 12/1, 2017 at 3:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.