chapter seventeen

17 Running finite workloads 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 Chapter17/ directory, and apply all the manifests in the SETUP/ directory by running the following commands:

17.1 Running tasks with the Job resource

17.1.1 Introducing the Job resource

17.1.2 Running a task multiple times

17.1.3 Understanding how Job failures are handled

17.1.4 Parameterizing Pods in a Job

17.1.5 Running Jobs with a work queue

17.1.6 Communication between Job Pods

17.2 Scheduling Jobs with CronJobs

17.2.1 Creating a CronJob

17.2.2 Configuring the schedule

17.2.3 Suspending and resuming a CronJob

17.2.4 Automatically removing finished Jobs

17.2.5 Setting a start deadline

17.2.6 Handling Job concurrency

17.2.7 Deleting a CronJob and its Jobs

17.3 Summary