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?
7 Answers
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.
![](../../users/profiles/2884309.webp)
- 3,668
- 8
- 47
- 63
![](../../users/profiles/220814.webp)
- 2,917
- 1
- 14
- 16
-
3Be 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 https://kubernetes.io/docs/tasks/tools/install-kubectl/ – Scott Jungwirth May 15 '18 at 17:24
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
![](../../users/profiles/5940484.webp)
- 5,833
- 2
- 18
- 27
-
1Just 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.yaml` – Aleksandr Podkutin Sep 18 '18 at 11:05
-
Assumption that there are already jobs in the history often does not hold. – Andrew Savinykh Jan 13 '19 at 20:37
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
![](../../users/profiles/4229593.webp)
- 670
- 9
- 20
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>
![](../../users/profiles/1029822.webp)
- 1,180
- 4
- 9
- 21
![](../../users/profiles/564157.webp)
- 17,180
- 7
- 70
- 72
There is an option to trigger the cron job manually whithin this tab in k8s dashboard
![](../../users/profiles/4370109.webp)
- 5,195
- 62
- 50
- 120
![](../../users/profiles/10712044.webp)
- 81
- 1
- 3
-
Great! This is helpful espeically for Minikube users who want to debug and play with cronjobs (like me) :) – ch271828n Oct 08 '20 at 12:43
I've created a small cmd utility for convenience to do just that and also suspend and unsuspend cronjobs.
![](../../users/profiles/1755956.webp)
- 41
- 4