Difficulty: Intermediate
Estimated Time: 25 minutes

Jobs

As a prerequisite, we assume you have an introductory working knowledge of Minikube covered in the Minikube scenario of this course.

This scenario takes you through the basics of Job resources on Kubernetes. Chapter 10 of the 2017 O'Reilly Kubernetes Up & Running book from Kelsey Hightower, Brendan Burns, and Joe Beda has a very nice explanation of Jobs. Therefore, this scenario is simply a manifestation of their explanation in Katacoda form.

From the Safari online promotion for the book:

A Job creates Pods that run until successful termination (i.e., exit with 0). In contrast, a regular Pod will continually restart regardless of its exit code. Jobs are useful for things you only want to do once, such as database migrations or batch jobs. If run as a regular Pod, your database migration task would run in a loop, continually repopulating the database after every exit.

The Job object is responsible for creating and managing pods defined in a template in the Job specification. These pods generally run until successful completion. The Job object coordinates running a number of pods in parallel.

In the following steps you will learn:

  • how Jobs are defined and work in Kubernetes,
  • how Kubernetes resilience recovers failed jobs,
  • how Job can serially or in parallel,
  • why it's more efficient to run Jobs in parallel,
  • how Jobs can process a work queue.

The Jobs feature is described in the Kubernetes documentation. More references to documentation is listed at the end of this scenario.

Conclusion

Job are much like Pods, but differ since they terminate once the task is completed. The Job feature ensures the job complete successfully and can optionally rerun the tasks until a success is reported.

Job efficiently run in parallel. Where once you may have been inclined to multi-thread the application in the container, you can instead keep the container a simple task runner and rely on Kubernetes resource management and Job parallelism to a achieve threading with a more effect pattern.

When combined with a queuing or messaging mechanism, Jobs can asynchronously process any tasks you decide to design.

To further this thinking consider investigating serverless or function implementations on Kubernetes. In many ways Jobs are a primitive form of what various serverless solution achieve as functions. Take a look at Knative based solutions to understand how you may someday want to evolve your Jobs into Functions.

Lessons Learned

With these steps you have learned:

  • how Jobs are defined and work in Kubernetes,
  • how Kubernetes resilience recovers failed jobs,
  • how Job can serially or in parallel,
  • why it's more efficient to run Jobs in parallel,
  • how Jobs can process a work queue.

References


For a deeper understanding of these topics and more join me, Jonathan Johnson, for a transcendent experience on the No Fluff Just Stuff Software Symposium Tour.

Jobs

Step 1 of 5

Run Single Job

Ensure Minikube is running and ready to accept your upcoming declarations.

minikube status

Use the Kubectl run command as a simple way to create a Job. This command runs the Job in the form of a Pod that contains a single container. The app in the container simply generates security keys.

kubectl run -i oneshot --image=gcr.io/kuar-demo/kuard-amd64:1 --restart=OnFailure -- --keygen-enable --keygen-exit-on-complete --keygen-num-to-gen 10

Once the job is complete the Pod and container are no longer running, but are still present and its state and logs can be inspected.

Use the get command to list the current jobs.

kubectl get jobs

Take a peek at the Kubernetes Dashboard (tab above the command line) and see how the Job is created as a resource. In the dashboard, explore the details and logs of the Job and Pod that just ran.

Once you are done inspecting the job, use the delete command to remove it.

kubectl delete jobs oneshot

This steps explored starting a job with just a command. The next step will run the same job, but from a resource defined in a YAML.

Terminal
Kubernetes Dashboard
Queue Portal