18 Batch Processing with Jobs and CronJobs

 

This chapter covers

  • Running finite tasks with Jobs
  • Handling Job failures
  • Parameterizing Pods created through a Job
  • Processing items in a work queue
  • Enabling communication between a Job’s Pods
  • Using CronJobs to run Jobs at a specific time or at regular intervals

As you learned in the previous chapters, a Pod created via a Deployment, StatefulSet, or DaemonSet, runs continuously. When the process running in one of the Pod’s containers terminates, the Kubelet restarts the container. The Pod never stops on its own, but only when you delete the Pod object. Although this is ideal for running web servers, databases, system services, and similar workloads, it’s not suitable for finite workloads that only need to perform a single task.

A finite workload doesn’t run continuously, but lets a task run to completion. In Kubernetes, you run this type of workload using the Job resource. However, a Job always runs its Pods immediately, so you can’t use it for scheduling tasks. For that, you need to wrap the Job in a CronJob object. This allows you to schedule the task to run at a specific time in the future or at regular intervals.

In this chapter you’ll learn everything about Jobs and CronJobs. Before you begin, create the kiada Namespace, change to the Chapter18/ directory, and apply all the manifests in the SETUP/ directory by running the following commands:

18.1 Running tasks with the Job resource

18.1.1 Introducing the Job resource

18.1.2 Running a task multiple times

18.1.3 Understanding how Job failures are handled

18.1.4 Parameterizing Pods in a Job

18.1.5 Running Jobs with a work queue

18.1.6 Communication between Job Pods

18.1.7 Sidecar containers in Job pods

18.2 Scheduling Jobs with CronJobs

18.2.1 Creating a CronJob

18.2.2 Configuring the schedule

18.2.3 Suspending and resuming a CronJob

18.2.4 Automatically removing finished Jobs

18.2.5 Setting a start deadline

18.2.6 Handling Job concurrency

18.2.7 Deleting a CronJob and its Jobs

18.3 Summary