Cyberithub

How to Disable or Suspend CronJobs in Kubernetes

Advertisements

In this article, we will see how to disable or suspend cronjobs in Kubernetes. You might have come across a scenario where you cannot delete a cronjob in Kubernetes cluster, instead you need to put it in suspend state for now so that it cannot create any pod to perform its scheduled job. Although there are few ways through which you can put a cronjob in disable or suspend state, on those the best way to do is through patching where you can just set the suspend field to true in .spec of a Job manifest.

We will understand this through a real example in below section but before that let's first understand what is a cronjob and when do we need to put these cronjobs in suspend or disable state.

What is a CronJob

A cronjob is a job in cron format created to run pod into completion state. It is mostly used in parallel batch processing. A job can be used to perform number of tasks ranging from taking backup to scientific computation in a periodic manner.

Why Suspend a CronJob

When it comes to running multiple cronjob, cluster resources really matters a lot and hence a priority is required to run the important jobs first. This is true for a cluster with limited resources which cannot be scaled as per the job requirement over a period of time. Since Kubernetes API lacked the ability to suspend and resume Jobs in automated manner based on priority, it boils down to manual effort to set the priority of the job which needs to be run first.

Now it comes the really interesting part - how to set the job priority ? Well there is not much of efficient way available in Kubernetes like setting the nice value you might have seen in Linux Operating systems. Here you only have the option to either delete the lower priority job so that the high priority job can run or just keep all the low priority jobs in suspend state till all the other higher priority job completes. As deleting a job is usually considered a poor workaround so we need to rely on suspending the job as and when required. More on Kubernetes official website.

How to Disable or Suspend CronJobs in Kubernetes

How to Disable or Suspend CronJobs in Kubernetes

Also Read: How to Upgrade Kubernetes Cluster to Some Specific Version [Step by Step]

In our case, we have below cronjobs currently scheduled in our two node Kubernetes Cluster.

[node1 ~]$ kubectl get cronjobs
NAME  SCHEDULE     SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * *  False     5    20s           4m31s
test  */3 * * * *  False     2    10s           5m12s

If you want to suspend let's say hello cronjob through patch then you need to use kubectl patch cronjobs hello -p '{"spec" : {"suspend" : true }}' command as shown below.

[node1 ~]$ kubectl patch cronjobs hello -p '{"spec" : {"suspend" : true }}'
cronjob.batch/hello patched

If you again the status of cronjobs using kubectl get cronjobs then it should look like below.

[node1 ~]$ kubectl get cronjobs
NAME  SCHEDULE     SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * *  True      5    2m20s         6m51s
test  */3 * * * *  False     2    2m10s         7m22s

If you need to suspend all the cronjobs due to certain reason then you need to use kubectl get cronjobs | grep False | cut -d' ' -f 1 | xargs kubectl patch cronjobs -p '{"spec" : {"suspend" : true }}' command as shown below.

[node1 ~]$ kubectl get cronjobs | grep False | cut -d' ' -f 1 | xargs kubectl patch cronjobs -p '{"spec" : {"suspend" : true }}' 
cronjob.batch/hello patched
cronjob.batch/test patched

If you now check the status using kubectl get cronjobs command, then you will see both of the jobs in suspend state.

[node1 ~]$ kubectl get cronjobs
NAME  SCHEDULE     SUSPEND ACTIVE LAST   SCHEDULE  AGE
hello */1 * * * *  True      5    15m30s           20m12s
test  */3 * * * *  True      5    15m40s           17m52s

Similarly, if you want bring back the jobs in active state then you can set the suspend field back to False in .spec of a Job manifest.

[node1 ~]$ kubectl patch cronjobs hello -p '{"spec" : {"suspend" : false }}'
cronjob.batch/hello patched

Leave a Comment