Reputation: 1597
I have scheduled an application to run as a CronJob
in Kubernetes. When there is a code change, I'm also changing the image of the CronJob
.
I'm looking for an option where I can disable the currently running CronJob
and deploy a new CronJob
with the latest image version.
How can I disable a CronJob
in Kubernetes without deleting its Deployment
?
Upvotes: 146
Views: 167086
Reputation: 6331
If you want to suspend cronjob via patch, use:
kubectl patch cronjobs <job-name> -p '{"spec" : {"suspend" : true }}'
You may need to escape " and/or specify namespace
kubectl patch cronjob <job-name> [-n namespace] -p '{\"spec\" : {\"suspend\" : true }}'
Upvotes: 266
Reputation: 317
kubectl patch cronjobs job-name -p '{"spec" : {"suspend" : true }}'
Upvotes: 30
Reputation: 683
You can either use patch command like below -
kubectl patch cronjobs <cron-jon-name> -n <namespace> -p '{"spec" : {"suspend" : true }}'
or you can first get the cronjob resource yaml -
kubectl get cronjobs <cron-jon-name> -n <namespace> -o yaml > cronjob.yaml
Edit yaml file under spec section with suspend to true.
Upvotes: 1
Reputation: 6841
Here's arguably the simplest way you can patch
multiple CronJobs
(and other patch-able objects like Deployments
or Pods
):
kubectl patch $(kubectl get cronjob -o name | grep my-filter) -p '{"spec" : {"suspend" : true }}'
Notice the use of -o name
which simplifies getting a list of objects (here: CronJobs
) names to process (without the need to parse a table with awk
).
You can patch all of them at once or just a subset of names restricted to those meeting filtering criteria (here: containing my-filter
).
Upvotes: 2
Reputation: 71
Option 1 with command line
$ kubectl patch cronjobs $(kubectl get cronjobs | awk '{ print $1 }' | tail -n +2) -p '{"spec" : {"suspend" : true }}'
Option 2 with command line:
$ kubectl get cronjobs | grep False | cut -d' ' -f 1 | xargs kubectl patch cronjobs -p '{"spec" : {"suspend" : true }}'
Option 3 creating resource quotas. I believe that is the cleaner option.
cat <<EOF | kubectl apply -f -
# https://kubernetes.io/docs/concepts/policy/resource-quotas/#object-count-quota
apiVersion: v1
kind: ResourceQuota
metadata:
name: limit-generic-resources
spec:
hard:
pods: "0"
count/persistentvolumeclaims : "0"
count/services : "0"
count/secrets : "0"
count/configmaps : "0"
count/replicationcontrollers : "0"
count/deployments.apps : "0"
count/replicasets.apps : "0"
count/statefulsets.apps : "0"
count/jobs.batch : "0"
count/cronjobs.batch : "0"
EOF
Upvotes: 7
Reputation: 4909
Edit your current cronjob resource to include the .spec.suspend field and set it to true. Any currently running jobs will complete but future jobs will be suspended.
If you also need to stop currently running jobs, you'll have to delete them
Upvotes: 26
Reputation: 377
You can use something which will be valid with respect to Cron Job format but actually that date should not appear anytime in calendar date like 31 Feb.
* * 31 2 *
Upvotes: 22