Jobs

A Job allows running a pod that is not restarted once it completes successfully.

  • If the node that runs a job fails, the job pod will be rescheduled to another node

  • If the job pod fails, you can choose whether to restart the job or not

Job descriptor

Note that the only required .spec field is template.

apiVersion: batch/v1
kind: Job
metadata:
  name: batch-job
spec:
  # need this many completions (default: 1)
  completions: 5
  # e.g. 2 pods can be parallel (default: 1, 0 means pause the job)
  parallelism: 2
  # number of retries before considering the job failed (default: 6)
  backoffLimit: 5
  # hard time limit before consideting the job failed
  # (will consider as failure even before backoffLimit is reached)
  activeDeadlineSeconds: 100
  # description about the pod to create
  template:
    metadata:
      labels:
        app: batch-job
    spec:
      # The pod can't use the "Always" restartPolicy;
      # you can choose "OnFailure" or "Never"
      restartPolicy: OnFailure
      containers:
        - name: main
          image: luksa/batch-job

Creating a CronJob

You can create a job that runs every now and then:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: batch-job-every-fifteen-minutes
spec:
  # use cron notation:
  # "minute hour dayOfMonth month dayOfWeek"
  schedule: '0,15,30,45 * * * *'
  # schedule the job to start at the latest this many seconds
  # after the schedule, or threat it as Failed (optional)
  startingDeadlineSeconds: 15
  # template for the Job resource
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: periodic-batch-job
        spec:
          restartPolicy: OnFailure
          containers:
            - name: main
              image: luksa/batch-job

Working with Jobs

Creating a Job

kubectl create -f <filename>

Scaling a Job

kubectl scale job <jobName> --replicas <number>

Listing Jobs

kubectl get job
# NAME        DESIRED   SUCCESSFUL   AGE
# batch-job   1         1            9m

Describing a Job

kubectl describe job <jobName>

Seeing Job's log

kubectl logs <jobName>
# Fri Apr 29 09:58:22 UTC 2016 Batch job starting
# Fri Apr 29 10:00:22 UTC 2016 Finished succesfully

Deleting a Job

kubectl delete <jobNane>

References

  • Kubernetes in Action (Marko Lukša)Chapter 4. Replication and other controllers: deploying managed pods
  • Jobhttps://kubernetes.io/docs/concepts/workloads/controllers/job/