cronjob

 CronJobs help you to schedule and run job at specific time.

1. How to Schedule a Job

cat cronjobs.yaml

apiVersion: batch/v1
kind: CronJob
metadata:
  name: jobdemo
spec:
  schedule: "*/2 * * * *"  # this will run every two minutes
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: simplejob
            image: busybox
            command: ["echo", "Welcome to my Blog"]
          restartPolicy: OnFailure

kubectl get all
NAME                         READY   STATUS      RESTARTS   AGE
pod/jobdemo-28396432-bdbl5   0/1     Completed   0          4m44s
pod/jobdemo-28396434-s87sg   0/1     Completed   0          2m44s
pod/jobdemo-28396436-n4bxz   0/1     Completed   0          44s
NAME                    SCHEDULE      SUSPEND   ACTIVE   LAST SCHEDULE   AGE
cronjob.batch/jobdemo   */2 * * * *   False     0        44s             6m40s

2. JOBS HISTORY LIMITS

 successfulJobsHistoryLimit: 3 # 3 is default, old success success will be cleaned
failedJobsHistoryLimit:1 # 1 is the default, old failed jobs will be cleaned

3. Concurrency policy

concurrencyPolicy: Allow/Forbid/Replace

Allow - Will allow to run a new job even if the previous job is still running
Forbid - Will restrict to run only one at a time
Replace - If a previous job is running for a long time, and next job is in the queue, then the old job is terminated and new one will be replaced.