Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 459
SmCaterpillar Avatar asked Mar 15 '18 07:03

SmCaterpillar


People also ask

How are cron jobs scheduled?

Cron jobs are scheduled at recurring intervals, specified using a format based on unix-cron. You can define a schedule so that your job runs multiple times a day, or runs on specific days and months. (Although we no longer recommend its use, the legacy App Engine cron syntax is still supported for existing jobs.)

How Kubernetes CronJob works?

When a CronJob resource is created, what Kubernetes actually does is to register a schedule. Every 10 seconds the CronJob Controller checks if there are matching schedules to take care of. When the proper time arrives a new Job resource is created to handle the task for that specific run.


2 Answers

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
like image 112
Radek 'Goblin' Pieczonka Avatar answered Nov 03 '22 08:11

Radek 'Goblin' Pieczonka


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.

like image 36
SmCaterpillar Avatar answered Nov 03 '22 08:11

SmCaterpillar