Chapter 7. Managing Specialized Workloads

In Chapter 4, we explored how to launch applications that are supposed to run forever, such as a web server or an app server. In this chapter, we will discuss workloads that are somewhat more specialized—for example, ones that launch terminating processes such as batch jobs, run pods on certain nodes, or manage stateful and noncloud native apps.

7.1 Running a Batch Job

Problem

You want to run a process that runs for a certain time to completion, such as a batch conversion, backup operation, or database schema upgrade.

Solution

Use a Kubernetes job resource to launch and supervise the pod(s) that will carry out the batch process.1

First, define the Kubernetes manifest for the job in a file called counter-batch-job.yaml:

apiVersion:          batch/v1
kind:                Job
metadata:
  name:              counter
spec:
  template:
    metadata:
      name:          counter
    spec:
      containers:
      - name:        counter
        image:       busybox
        command:
         - "sh"
         - "-c"
         - "for i in 1 2 3 ; do echo $i ; done"
      restartPolicy: Never

Then launch the job and have a look at its status:

$ kubectl create -f counter-batch-job.yaml
job "counter" created

$ kubectl get jobs
NAME      DESIRED   SUCCESSFUL   AGE
counter   1         1            22s

$ kubectl describe jobs/counter Name: counter Namespace: default Selector: controller-uid=634b9015-7f58-11e7-b58a-080027390640 Labels: controller-uid=634b9015-7f58-11e7-b58a-080027390640 job-name=counter Annotations: <none> Parallelism: 1 Completions: 1 Start Time: Sat, 12 Aug 2017 13:18:45 +0100 Pods Statuses: 0 Running ...

Get Kubernetes Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.