Difficulty: Intermediate
Estimated Time: 25 minutes

Jobs

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.

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

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 6

Your Kubernetes Cluster

For this scenario, Katacoda has just started a fresh Kubernetes cluster for you. Verify it's ready for your use.

kubectl version --short && kubectl cluster-info && kubectl get nodes

The Helm package manager used for installing applications on Kubernetes is also available.

helm version --short

Kubernetes Dashboard

You can control the cluster with the kubectl CLI tool or through the Kubernetes Dashboard. Because the dashboard can be accessed publicly, it is protected and requires the secret access token to sign in. Copy the token from this secret.

export TOKEN=$(kubectl describe secret $(kubectl get secret | awk '/^dashboard-token-/{print $1}') | awk '$1=="token:"{print $2}') && echo -e "\n--- Copy and paste this token for dashboard access --\n$TOKEN\n---"

To access the dashboard, click on the Kubernetes Dashboard tab above the command line or from this link: https://[[HOST_SUBDOMAIN]]-30000-[[KATACODA_HOST]].environments.katacoda.com/. At the sign in prompt select Token and paste in the token, you copied a moment ago.

For Kubernetes clusters exposed to the public, always lock administration access including access to the dashboard.

Terminal
Kubernetes Dashboard
Queue Portal