What is CronJob in Kubernetes?
IT Tricks Random

What is CronJob in Kubernetes?

What is CronJob in Kubernetes? – Cuongquach.com | You want to run some automated tasks in the system Kubernetes Cluster, the same as the way used crontab on Linux / UNIX. Then Kubernetes can support you: CronJob. Find out how to use it CronJob in Kubernetes Please.

What is CronJob in Kubernetes?

Like Cronjob in Linux, CronJob in Kubernetes Used to run tasks at a specific time or at specified intervals. A pretty good choice for automated tasks such as:

  • Backup data
  • synthesis report
  • Send email
  • Cleaning tasks

CronJob will use the Job object to configure so creating, managing, extending and deleting CronJob is like Job in Kubernetes. CronJob will be managed by CronJob Controller.

Manage CronJob in Kubernetes

Create CronJob Kubernetes

I created one CronJob The content is: print every one minute date and output message. In fact, usually one will build one Docker Image specifically for the desired task automation activities. Then just specify CMD or ENTRYPOINT to run the script command in Docker Image there.


+ Schedule

Configuration spec.schedule Used to specify the time frame that CronJob will run, the syntax is identical to Unix crontab. The default time for CronJob is UTC. There are a total of 5 columns, separated by spaces, each representing the following information values:

  • Minutes (from 0 to 59)
  • Hours (from 0 to 23)
  • Day of the month (from 1 to 31)
  • Month (from 1 to 12)
  • Day of the week (from 0 to 6)

+ Job Template

Configuration .spec.jobTemplate Specifies the configuration to run one Job. Having the same structure as at configuration Kubernetes Job, from here help you specify: container is run; commands like CMD, ENTRYPOINT, ARGS, .. is used to run the task.

+ Starting Deadline

CronJob controller will check every 10 seconds the list of CronJobs created by Kubernetes Client. Deadline will set the time CronJob Controller will check in [n] Time has passed, how many failed Scheduled Jobs will restart those Scheduled Jobs.

For example:

  • You set Cronjob to execute at 9:30 and ten:30 (1 time per hour). startingDeadlineSeconds is 300 seconds.
  • At 9:29 , Cronjob Controller or system failure, downtime. So Scheduled Job at 9:30 could not be executed. The system registers miss scheduled job (failed).
  • At 9:34, system restore, check within the previous 300s from 9:299:34 , there was a miss scheduled job.
  • Then immediately start missing that scheduled job again.

– If more than 100 scheduled jobs are failed for a CronJob, the Cronjob Controller will report the following error:

+ Policy to run concurrently

When you use the configuration .spec.concurrencyPolicy will help you define policies for instances where Job runs are simultaneously created by CronJob Controller. By default, multiple Jobs are created by one CronJob will be allowed to run concurrently. Note that, Jobs in the same 1 CronJob offline. And different CronJobs will still create different Jobs running normally simultaneously.

When did this happen? : for example, you run 1 cronjob every minute, but a job takes 2 minutes to complete then of course the next minute will start a new Job pod to run while the old Job pod has not finished running.

Some supported configurations are as follows:

  • Allow: allow Jobs in 1 CronJob to run concurrently.
  • Forbid: do not allow Jobs in 1 CronJob to run concurrently.
  • Replace: If the old Job hasn’t finished running, it will replace the old one.

+ CronJob History

CronJob will initialize one Pod every time the task job runs. To facilitate the review of the status information of the CronJob which have been executed, as well as the logs of those executable Pods, will be specified in the “CronJob History”(History of running CronJob).

You can configure the maximum number CronJob Successful execution and failure were cached by 2 values. Assuming your CronJob archive successfully executes 5 and fails 2, then when you list CronJob related Pods, you’ll see 5 Pods retained:

  • spec.successfulJobsHistoryLimit: 5 (default is 3)
  • spec.failedJobsHistoryLimit: 2 (default is 1)

See information CronJob Kubernetes

You can list the whole cronjob in Kubernetes:

Or specific listing cronjob named .

Next you can see the specific configuration information of CronJob there.

You can sit and watch Job information created by CronJob cuongquachcronjobnotify .

Now I need to see the output of CronJob was created and run. Then I need to find the Pod created by Scheduled Job before that, then look at that Pod’s log.

Delete CronJob Kubernetes

Delete one CronJob in Kubernetes is also easy:

Pretty easy to understand, right? Wish you understand how to use it CronJob in Kubernetes Please.

Source: https://cuongquach.com/

Leave a Reply

Your email address will not be published. Required fields are marked *