Skip to content

Kubernetes Batch Jobs & Cron Jobs

Kubernetes Batch Processing with Jobs & Cronjobs.

A pod deployed via a deployment, statefulset or daemonset run continously.

For running finite tasks, you can use Kubernetes Jobs.

To schedule tasks, CronJobs are used.

creating a namespace :

kubectl create ns kiada

Setting the namespace to the context

kubectl config set-context --current --namespace kiada

Running Tasks with the Job Resource

When all containers in a pod terminate with success, the pod is considered completed. When all the pods have completed, the Job itself is also completed.

  • A Job can contain a single pod.
  • A job can also run its pods in parallel.
  • A job can be configured to run a set number of times.

Defining a Job Resource

Manifest file for a Job

apiVersion: batch/v1
kind: Job
metadata:
    name: job-name
    labels:
        app: myjob
        task: init

spec:
    template:
        metadata:
            labels:
                app: quiz
                task: init
        spec:
            restartPolicy: OnFailure
            initContainers:
                - name: init
                  image: mongo:5
                  command: 
                    - sh
                    - c 
                    - |
                        mongosh mongodb://url \ --quiet --file /dev/stdin/ <<EOF

                  EOF
            containers:
                - name: import
                  image: mongo:5
                  command: 
                    - mongoimport
                    - mongodb+srv://url
                    - --collection
                    - questions
                    - --file
                    - /questions.json
                    - --drop
                    volumeMounts:
                        - name: data
                          mountPath: /questions.json
                          subPath: questions.json
                          readOnly: true
            volumes:
                - name: data
                 configMap: 
                    name: data

The manifest in the listing defines a Job object that runs a single pod to completion. Jobs belong to the batch API group.

The pods restartPolicy is set to OnFailure.

In a jobs pod template, you must explicitly set the restart policy to either OnFailure or Never.

Running a Job

Displaying a brief job status

$ kubectl get jobs

Displaying a detailed job status

$ kubectl describe job job-name

Examining the pods that belong to a job.

kubectl get pods -l job-name=myjob

Suspending active jobs and creating jobs in a suspended state.

apiVersion: batch/v1
kind: Job
metadata:
    name: myjob
spec:
    suspend: true
    template:
        spec:
            restartPolicy: OnFailure
            containers:
                -name: demo
                image: busybox
                command:
                    - sleep
                    - "60"

CronJobs

Scheduling Jobs with Cronjobs

Creating a Cronjob

CronJob that runs a job every minute.

apiVersion: batch/v1
kind: CronJob

metadata:
    name: mycronjob
spec:
    schedule: "* * * * *"
    jobTemplate: 
        metadata: 
            labels:
                app: mycronjob

        spec:
            template:
                metadata:
                    labels:
                        app: mycronjob



                spec:
                    restartPolicy: OnFailure
                    containers:
                    - name: updater
                      image: mongo:5
                      command:      
                      - mongosh


                      volumeMounts:
                        - name: script
                          subPath: script.js
                          mountPath: /script.js

                    volumes:
                    - name: script
                      configMap:
                        name: mycronjob

Running a CronJob

kubectl get cj
OR
kubectl get cronjob