Skip to content
tezvyn:

Kubernetes Jobs: For Tasks That Need to Finish

Source: kubernetes.ioEasyHow cards are made

Kubernetes Jobs: For Tasks That Need to Finish

A Kubernetes Job runs a task to completion, unlike a Deployment which runs forever. Use it for one-off operations like database migrations or batch processing. The footgun is forgetting to set a retry limit, causing failed jobs to loop indefinitely.

Why it exists

Not all work is a long-running service. Some tasks, like processing a file, running a database migration, or creating a backup, need to run once and then stop. Kubernetes needed a way to manage these finite tasks, ensuring they complete successfully without needing to be manually supervised.

The mental model

A Job is a supervisor for a temporary task. You give it a Pod template and say, "Run this until it exits with a success code." The Job controller's purpose is to ensure that a specified number of Pods successfully terminate. It's a fire-and-forget mechanism for work that has a clear end.

How it works

When you create a Job object, the Kubernetes control plane creates one or more Pods based on your template. The Job is considered complete when a specified number of these Pods have terminated with a status of 'Success'. If a Pod fails (e.g., the node it's on reboots or the process crashes), the Job controller will create a new Pod to replace it, up to a configurable limit. This makes your one-off tasks resilient to transient cluster failures.

When to use it

Use a Job for any task that is expected to terminate. Three common scenarios are: first, running a database schema migration before deploying a new version of an application; second, processing a batch of items from a queue until it's empty; third, performing a one-off administrative task across your infrastructure.

When not to use it

Do not use a Job for long-running services like a web server or an API; use a Deployment or StatefulSet for that. For tasks that need to run on a recurring schedule (e.g., nightly backups), use a CronJob, which creates Jobs based on a cron schedule.

One canonical example

A classic example is a Job that calculates pi to a certain number of decimal places. The Job manifest would specify a container image (like perl or python), a command to run the calculation, and a restartPolicy of OnFailure or Never. The Job creates a Pod, the Pod runs the calculation, prints the result, and exits with a success code. The Job then marks itself as complete.

Interview question

Which task is best suited for a Kubernetes Job?

  • a.Running a data processing script every hour on a fixed schedule.
  • b.Hosting a stateless API service that handles user requests.
  • c.Ensuring a set of pods are always running to serve traffic.
  • d.Performing a one-time database schema upgrade for an application.Correct
Why?

Kubernetes Jobs are designed for tasks that run to completion, such as a one-off database migration. Long-running services (options A and C) are managed by Deployments, and recurring scheduled tasks (option A) are handled by CronJobs.

Just read this? Test yourself on what you have been reading.

Read the original → kubernetes.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kubernetes — each one lists the topics its interview covers.

See open roles