How to change the schedule of a Kubernetes cronjob or how to start it manually?

3/15/2018

Is there a simple way to change the schedule of a kubernetes cronjob like kubectl change cronjob my-cronjob "10 10 * * *"? Or any other way without needing to do kubectl apply -f deployment.yml? The latter can be extremely cumbersome in a complex CI/CD setting because manually editing the deployment yaml is often not desired, especially not if the file is created from a template in the build process.

Alternatively, is there a way to start a cronjob manually? For instance, a job is scheduled to start in 22 hours, but I want to trigger it manually once now without changing the cron schedule for good (for testing or an initial run)?

-- SmCaterpillar
cron
kubectl
kubernetes

3 Answers

7/23/2018

I have a friend who developed a kubectl plugin that answers exactly that !
It takes an existing cronjob and just create a job out of it.
See https://github.com/vic3lord/cronjobjob
Look into the README for installation instructions.

-- Shushu
Source: StackOverflow

3/15/2018

You can update only the selected field of resourse by patching it

patch -h                     
Update field(s) of a resource using strategic merge patch, a JSON merge patch, or a JSON patch.           

JSON and YAML formats are accepted.

Please refer to the models in
https://htmlpreview.github.io/?https://github.com/kubernetes/kubernetes/blob/HEAD/docs/api-reference/v1/definitions.html
to find if a field is mutable.

As provided in comment for ref :

kubectl patch cronjob my-cronjob -p '{"spec":{"schedule": "42 11 * * *"}}'

Also, in current kubectl versions, to launch a onetime execution of a declared cronjob, you can manualy create a job that adheres to the cronjob spec with

kubectl create job --from=cronjob/mycron
-- Radek 'Goblin' Pieczonka
Source: StackOverflow

7/23/2018

The more recent versions of k8s (from 1.10 on) support the following command:

$ kubectl create job my-one-time-job --from=cronjobs/my-cronjob

Source is this solved k8s github issue.

-- SmCaterpillar
Source: StackOverflow